我正在制作一个简单的GUI计算器。在插入内容之前,我正试图让GUI失效。我遇到的问题是,包含键盘和显示的JPanels
将不会显示在程序的主JFrame
中。我错过了什么?
主要
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public Main() {
calc();
}
public final void calc() {
JFrame main = new JFrame("Alex's Calculator");
Display disp = new Display();
Keypad keyp = new Keypad();
JPanel c = new JPanel();
c.setLayout(new BorderLayout());
c.add(disp, BorderLayout.NORTH);
c.add(keyp, BorderLayout.SOUTH);
main.setDefaultCloseOperation(EXIT_ON_CLOSE);
main.setSize(400, 300);
c.setVisible(true);
main.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}
键盘:(是的,我知道。我将添加一个循环来创建按钮数组。)
import java.awt.*;
import javax.swing.*;
public class Keypad extends JPanel{
public Keypad() {
int num = 1;
JPanel Keypad = new JPanel();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bDec = new JButton(".");
JButton b0 = new JButton("0");
JButton bPlus = new JButton("+");
JButton bMinus = new JButton("-");
JButton bDiv = new JButton("/");
JButton bMult = new JButton("*");
JButton bBOR = new JButton("|");
JButton bBAND = new JButton("&");
JButton bModu = new JButton("%");
JButton bEq = new JButton("=");
JButton bC = new JButton("Clear");
Keypad.add(b1);
b1.setMnemonic('1');
Keypad.add(b2);
b2.setMnemonic('2');
Keypad.add(b3);
b3.setMnemonic('3');
Keypad.add(bPlus);
bPlus.setMnemonic('+');
Keypad.add(bBOR);
bBOR.setMnemonic('|');
Keypad.add(b4);
b4.setMnemonic('4');
Keypad.add(b5);
b5.setMnemonic('5');
Keypad.add(b6);
b6.setMnemonic('6');
Keypad.add(bMinus);
bMinus.setMnemonic('-');
Keypad.add(bBAND);
bBAND.setMnemonic('&');
Keypad.add(b7);
b7.setMnemonic('7');
Keypad.add(b8);
b8.setMnemonic('8');
Keypad.add(b9);
b9.setMnemonic('9');
Keypad.add(bMult);
bMult.setMnemonic('*');
Keypad.add(bModu);
bModu.setMnemonic('%');
Keypad.add(b0);
b0.setMnemonic('0');
Keypad.add(bDec);
bDec.setMnemonic('.');
Keypad.add(bC);
bC.setMnemonic('C');
Keypad.add(bDiv);
bDiv.setMnemonic('/');
Keypad.add(bEq);
bEq.setMnemonic('=');
Keypad.setLayout(new GridLayout(4, 5));
Keypad.setSize(400, 200);
Keypad.setVisible(true);
}
}
显示:
import javax.swing.*;
import java.awt.*;
public class Display extends JPanel{
JTextField result = new JTextField(20);
public Display() {
JPanel display = new JPanel();
display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
display.add(result);
display.setSize(400, 100);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setEditable(false);
display.setVisible(true);
}
}
我对Java和整个编程都很陌生 任何帮助将不胜感激。谢谢!
答案 0 :(得分:2)
您的代码中存在两个问题:
main.getContentPane().add(c, BorderLayout.CENTER);
向您的相框添加JPanel c
。请务必将此作为main
。下面有关#2的更多讨论。但首先,您的Main
类本身已经是JFrame,因此您真的不需要创建一个名为main
的单独JFrame。相反,这样做:
Display disp = new Display();
Keypad keyp = new Keypad();
JPanel c = new JPanel();
c.setLayout(new BorderLayout());
c.add(disp, BorderLayout.NORTH);
c.add(keyp, BorderLayout.SOUTH);
this.setTitle("Alex's Calculator");
this.getContentPane().add(c, BorderLayout.CENTER);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 300);
this.setVisible(true);
现在回到那些JPanels。在这两种情况下,您都可以创建JPanel对象(Keypad
和display
),但这些对象永远不会从类中返回。相反,请确保您正在使用类扩展JPanel的事实 - 换句话说,使用this
而不是创建单独的对象。例如,您的新Display
应如下所示:
public Display() {
this.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
this.add(result);
this.setSize(400, 100);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setEditable(false);
this.setVisible(true);
}