你如何在 Java 面板中使用网格布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20013916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do you use Grid layout in panels in Java?
提问by user2997509
Im trying to create a JFrame with two panels. the top panel will contain a text field and the bottom panel will contain a grid of buttons. Ive used gridLayout to to arrange the buttons and added them into a panel and added the panel to the JFrame but according to the editor the value of panel is NULL. The same is true of the second panel. Could someone help direct me to the problem?
我正在尝试创建一个带有两个面板的 JFrame。顶部面板将包含一个文本字段,底部面板将包含一个按钮网格。我使用 gridLayout 来排列按钮并将它们添加到面板中并将面板添加到 JFrame 但根据编辑器面板的值为 NULL。第二个面板也是如此。有人可以帮助指导我解决问题吗?
import java.awt.*;
import javax.swing.*;
public class frameClass extends JFrame{
private static final long serialVersionUID = 1L;
private JFrame frame;
private JPanel panel;
private JPanel panel2;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
public frameClass() {
panel = new JPanel(new GridLayout(4,4,5,5));
panel.setBackground(Color.BLACK);
Font font1 = new Font("SanSerif",Font.BOLD, 16);
button0 = new JButton("0");
button0.setFont(font1);
button0.setBackground(Color.BLACK);
button0.setForeground(Color.WHITE);
panel.add(button0);
button1 = new JButton("1");
button1.setFont(font1);
button1.setBackground(Color.WHITE);
panel.add(button1);
button2 = new JButton("2");
button2.setFont(font1);
button2.setBackground(Color.BLACK);
button2.setForeground(Color.WHITE);
panel.add(button2);
button3 = new JButton("3");
button3.setFont(font1);
button3.setBackground(Color.WHITE);
panel.add(button3);
frame.add(panel);
panel2 = new JPanel(new BorderLayout());
panel2.add(new JTextField(21), BorderLayout.CENTER);
frame.add(panel2);
frame.setVisible(true);
}
public static void main(String[] args) {
new calculator();
}
}
采纳答案by MadProgrammer
The "core" problem isn't that the panel's are null, but frame
is null...
“核心”问题不在于面板为空,而是frame
为空...
// You extend from JFrame, which isn't highly recommended
// but you seem to ignore...
public class FrameClass extends JFrame {
private static final long serialVersionUID = 1L;
// Instance variable of frame, but it's never initialised...
private JFrame frame;
private JPanel panel;
private JPanel panel2;
//...
public FrameClass() {
// Create panel...
panel = new JPanel(new GridLayout(4, 4, 5, 5));
panel.setBackground(Color.BLACK);
//...
// Add it to frame, but frame is null...
frame.add(panel);
//...
}
Instead, try removing the extends JFrame
and instantiate an instance of JFrame
相反,尝试删除extends JFrame
并实例化JFrame
public class FrameClass {
private static final long serialVersionUID = 1L;
private JFrame frame;
private JPanel panel;
private JPanel panel2;
//...
public FrameClass() {
panel = new JPanel(new GridLayout(4, 4, 5, 5));
//...
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel2 = new JPanel(new BorderLayout());
panel2.add(new JTextField(21), BorderLayout.CENTER);
frame.add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}