为什么运行java程序时窗口是空的?
我正在尝试用java创建GUI。我正在添加按钮、标签、文本字段和组合框。当我运行程序时,弹出的窗口是完全空白的。知道为什么会这样吗为什么运行java程序时窗口是空的?,java,swing,Java,Swing,我正在尝试用java创建GUI。我正在添加按钮、标签、文本字段和组合框。当我运行程序时,弹出的窗口是完全空白的。知道为什么会这样吗 public Project3() { JPanel leftPanel = new JPanel(); leftPanel.setLayout(new GridLayout(7, 2)); leftPanel.setBounds(20, 10, 5, 5); leftPanel.add(shapeLbl); leftPane
public Project3() {
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(7, 2));
leftPanel.setBounds(20, 10, 5, 5);
leftPanel.add(shapeLbl);
leftPanel.add(shapeBox);
leftPanel.add(fillLbl);
leftPanel.add(fillBox);
leftPanel.add(colorLbl);
leftPanel.add(colorBox);
leftPanel.add(widthLbl);
leftPanel.add(widthTxt);
leftPanel.add(heightLbl);
leftPanel.add(heightTxt);
leftPanel.add(xLbl);
leftPanel.add(xTxt);
leftPanel.add(yLbl);
leftPanel.add(yTxt);
add(leftPanel);
JPanel rightPanel = new JPanel();
rightPanel.setBounds(270, 10, 5, 5);
rightPanel.setBorder(BorderFactory.createTitledBorder("Shape Drawing"));
add(rightPanel);
JPanel botPanel = new JPanel();
botPanel.setBounds(240, 250, 5, 5);
botPanel.add(drawBtn);
add(botPanel);
drawBtn.setToolTipText("Using the information above, this button " +
"draws a shape in the area titled \"Shape Drawing.\"");
drawBtn.setMnemonic('d');
}
public static void main(String[] args) {
Project3 p3 = new Project3();
p3.setTitle("Geometric Drawing");
p3.setLayout(null);
p3.setSize(500, 301);
p3.setLocationRelativeTo(null);
p3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p3.setVisible(true);
}
}添加什么功能?您在哪里添加这些面板?Project3从哪个类扩展而来?很可能是布局问题。请记住,默认情况下,
JFrame
为自己提供了一个BorderLayout
。因此,设置边界没有效果。改为使用setPreferredSize
,并在添加面板时指定BorderLayout
约束。不要使用空布局!!!另外,请提供足够的代码来编译它。一半的面板根本没有定义。