2
我正在创建一个基于GUI的计算器程序。我将这些区域分成几个面板,这些面板具有以下目的:
我在将所有这些面板添加到框架之前将其显示出来,但是当我按下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
您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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。