[英]JPanels will not show up in JFrame
I'm making a simple GUI calculator. 我正在制作一个简单的GUI计算器。 Before I insert the guts, I'm trying to get the GUI down. 在插入胆量之前,我正在尝试关闭GUI。 The problem that I'm having is that my JPanels
containing the keypad and display will not show up in the main JFrame
for the program. 我遇到的问题是,包含键盘和显示的JPanels
不会显示在程序的主JFrame
中。 What am I missing? 我想念什么?
Main: 主要:
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();
}
});
}
}
Keypad: (Yes, I know. I'm going to add a loop to create an array of buttons.) 键盘:(是的,我知道。我要添加一个循环来创建按钮数组。)
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);
}
}
Display: 显示:
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);
}
}
I'm fairly new to Java, and to programming as a whole. 我对Java以及整个编程还是很陌生。
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!
There are two problems in your code: 您的代码中有两个问题:
main.getContentPane().add(c, BorderLayout.CENTER);
您需要调用main.getContentPane().add(c, BorderLayout.CENTER);
to add JPanel c
to your frame. 将JPanel c
添加到您的框架。 Make sure to do this as one of the first things you do to main
. 确保将其作为维护main
的第一件事。 More discussion on #2 below. 有关下面#2的更多讨论。 But first, your Main
class itself is already a JFrame, so you really don't need to create a separate JFrame called main
. 但是首先,您的Main
类本身已经是一个JFrame,因此您实际上不需要创建一个单独的JFrame称为main
。 Instead, do this: 相反,请执行以下操作:
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);
Now back to those JPanels. 现在回到那些JPanel。 In both cases, you create JPanel objects ( Keypad
and display
) but those are never returned from the classes. 在这两种情况下,您都将创建JPanel对象( Keypad
和display
),但是这些对象永远不会从类中返回。 Instead, make sure you're working with the fact that your classes extend JPanel - in other words, work on this
instead of creating a separate object. 相反,请确保您正在使用类扩展了JPanel的事实-换句话说,请this
而不是创建单独的对象。 For example, your new Display
should look like: 例如,您的新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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.