开发者社区> 问答> 正文

Java GUI:我的面板上都没有显示面板,但是背景颜色和标题可见吗?

2

我正在创建一个基于GUI的计算器程序。我将这些区域分成几个面板,这些面板具有以下目的:

  • inputPanel:用户将在其中输入数字的位置;
  • numericPanel:保留数字0-9的面板
  • operatorPanel:包含运算符符号(+,-,*,/)的面板
  • formatPanel:面板上将包含CLEAR,ENTER和EXIT按钮

我在将所有这些面板添加到框架之前将其显示出来,但是当我按下play时,除了背景色之外什么都没有显示。我应该以其他方式格式化它们吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Flow;

/**
 * @author Graeven Yuri Videz
 * This is a program that simulates a simple calculator with arithmetic functions.
 */
public class calculator extends Frame implements ActionListener{

    private static JTextField firstField= new JTextField(25);
    private static JTextField num2 = new JTextField(25);

    //------------------------------------------------------------------------------------
    private static JButton b0 = new JButton("0");
    private static JButton b1 = new JButton("1");
    private static JButton b2 = new JButton("2");
    private static JButton b3 = new JButton("3");
    private static JButton b4 = new JButton("4");
    private static JButton b5 = new JButton("5");
    private static JButton b6 = new JButton("6");
    private static JButton b7 = new JButton("7");
    private static JButton b8 = new JButton("8");
    private static JButton b9 = new JButton("9");
    //-----------------------------------------------------------------------------------
    private static JButton enterButton = new JButton("Enter");
    private static JButton clearButton = new JButton("Clear");
    private static JButton exitButton = new JButton("Close");

    private static JButton addB = new JButton("+");
    private static JButton subB = new JButton("-");
    private static JButton multiB = new JButton("x");
    private static JButton divideB = new JButton("÷");


    private static JPanel inputPanel;
    private static JPanel numericPanel;
    private static JPanel operatorPanel;
    private static JPanel formatPanel;

    private static JLabel resultLabel;
    private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");

    //-----------------------------------------------------------------------------------
    public void setInputPanel(JPanel panel) {
        panel.setLayout(new FlowLayout());
        JLabel firstNum = new JLabel("Enter your number:");
        panel.add(firstNum);
        panel.add(firstField);


    }

    public void setNumericPanel(JPanel panel) {
        panel.setLayout(new GridLayout(6, 3));
        panel.add(b0);
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
    }

    public void setOperatorPanel(JPanel panel) {
        JPanel operatorPanel = new JPanel();
        //operatorPanel.setLayout(new FlowLayout());
        panel.add(addB);
        panel.add(subB);
        panel.add(multiB);
        panel.add(divideB);

    }

    public void setFormatPanel(JPanel panel) {
        JPanel formatPanel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(enterButton);
        panel.add(clearButton);
    }


    public calculator() {
        inputPanel = new JPanel();
        setInputPanel(inputPanel);
        numericPanel = new JPanel();
        setNumericPanel(numericPanel);
        operatorPanel = new JPanel();
        setOperatorPanel(operatorPanel);
        formatPanel = new JPanel();
        setFormatPanel(formatPanel);

        setTitle("Calculator Program");
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new GridLayout(3, 3));
        frame.add(nameLabel);
        frame.add(inputPanel);
        frame.add(numericPanel);
        frame.add(operatorPanel);
        frame.add(formatPanel);
        setBackground(Color.gray);
        setSize(350, 500);
        frame.pack();
        setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args){
        calculator calcuObject = new calculator();
    }
    @Override
    public void actionPerformed(ActionEvent ac) {


    }

}

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 15:26:05 806 0
1 条回答
写回答
取消 提交回答
  • 您JFrame的代码中有两个不同的实例。 您向中添加了所有组件,JFrame frame = new JFrame();但setVisible(true); 解决方案使其他组件可见:更改setVisible(true); 为frame.setVisible(true); 无需扩展计算器JFrame。

    您JFrame的代码中有两个不同的实例。 您向中添加了所有组件,JFrame frame = new JFrame();但setVisible(true); 解决方案使其他组件可见:更改setVisible(true); 为frame.setVisible(true); 无需扩展计算器JFrame。

    注意事项: 1.请参阅Java命名约定 2.单击此链接可获得代码的重构版本。

    回答来源:Stack Overflow

    2020-03-22 15:27:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载