框架不显示组件,除非调整大小 (JAVA AWT)。无法修复
Frame Not Showing Components Unless Resized (JAVA AWT). Cant Fix
我正在研究计算器。我添加了一些按钮和一个文本字段。我面临的问题是,当我 运行 我的项目时,它不显示作为按钮的组件,但是当我调整框架大小时,按钮立即出现。这并不总是有时会出现按钮,有时直到调整框架大小后才会出现。尝试增加框架的宽度和高度,但这对我没有帮助。当按钮未显示时,我附上了计算器的代码和图像
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorUsingAWT {
CalculatorUsingAWT(){
Frame frame = new Frame("CALCKY");
Panel panel_1 = new Panel();
panel_1.setBounds(0,0,400,100);
panel_1.setBackground(new Color(43, 11, 51));
frame.add(panel_1);
frame.setLayout(null);
frame.setBounds(550,100,400,549);
// frame.setResizable(false);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TextField textField = new TextField();
textField.setBounds(25,45,350,40);
textField.setBackground(new Color(43, 11, 51));
textField.setForeground(Color.WHITE);
textField.setFont(new Font("SAN_SERIF",Font.BOLD,30));
textField.setEditable(false);
panel_1.setLayout(null);
panel_1.add(textField);
Panel panel_2 = new Panel();
panel_2.setLayout(new GridLayout(6,4));
panel_2.setBounds(0,100,401,450);
panel_2.setBackground(new Color(43, 11, 51));
frame.add(panel_2);
Button button_1 = new Button("1");
button_1.setBackground(new Color(43, 11, 51));
button_1.setFont(new Font("ARIAL",Font.BOLD,30));
button_1.setForeground(Color.WHITE);
Button button_2 = new Button("2");
button_2.setBackground(new Color(43, 11, 51));
button_2.setFont(new Font("ARIAL",Font.BOLD,30));
button_2.setForeground(Color.WHITE);
Button button_3 = new Button("3");
button_3.setBackground(new Color(43, 11, 51));
button_3.setFont(new Font("ARIAL",Font.BOLD,30));
button_3.setForeground(Color.WHITE);
Button button_4 = new Button("4");
button_4.setBackground(new Color(43, 11, 51));
button_4.setFont(new Font("ARIAL",Font.BOLD,30));
button_4.setForeground(Color.WHITE);
Button button_5 = new Button("5");
button_5.setBackground(new Color(43, 11, 51));
button_5.setFont(new Font("ARIAL",Font.BOLD,30));
button_5.setForeground(Color.WHITE);
Button button_6 = new Button("6");
button_6.setBackground(new Color(43, 11, 51));
button_6.setFont(new Font("ARIAL",Font.BOLD,30));
button_6.setForeground(Color.WHITE);
Button button_7 = new Button("7");
button_7.setBackground(new Color(43, 11, 51));
button_7.setFont(new Font("ARIAL",Font.BOLD,30));
button_7.setForeground(Color.WHITE);
Button button_8 = new Button("8");
button_8.setBackground(new Color(43, 11, 51));
button_8.setFont(new Font("ARIAL",Font.BOLD,30));
button_8.setForeground(Color.WHITE);
Button button_9 = new Button("9");
button_9.setBackground(new Color(43, 11, 51));
button_9.setFont(new Font("ARIAL",Font.BOLD,30));
button_9.setForeground(Color.WHITE);
Button button_10 = new Button("+");
button_10.setBackground(new Color(43, 11, 51));
button_10.setFont(new Font("ARIAL",Font.BOLD,30));
button_10.setForeground(Color.WHITE);
Button button_11 = new Button("-");
button_11.setBackground(new Color(43, 11, 51));
button_11.setFont(new Font("ARIAL",Font.BOLD,30));
button_11.setForeground(Color.WHITE);
Button button_12 = new Button("*");
button_12.setBackground(new Color(43, 11, 51));
button_12.setFont(new Font("ARIAL",Font.BOLD,30));
button_12.setForeground(Color.WHITE);
Button button_13 = new Button("/");
button_13.setBackground(new Color(43, 11, 51));
button_13.setFont(new Font("ARIAL",Font.BOLD,30));
button_13.setForeground(Color.WHITE);
Button button_14 = new Button("=");
button_14.setBackground(Color.BLUE);
button_14.setFont(new Font("ARIAL",Font.BOLD,30));
button_14.setForeground(Color.WHITE);
Button button_15 = new Button("%");
button_15.setBackground(new Color(43, 11, 51));
button_15.setFont(new Font("ARIAL",Font.BOLD,30));
button_15.setForeground(Color.WHITE);
Button button_16 = new Button("CE");
button_16.setBackground(new Color(43, 11, 51));
button_16.setFont(new Font("ARIAL",Font.BOLD,30));
button_16.setForeground(Color.WHITE);
Button button_17 = new Button("C");
button_17.setBackground(new Color(43, 11, 51));
button_17.setFont(new Font("ARIAL",Font.BOLD,30));
button_17.setForeground(Color.WHITE);
Button button_18 = new Button("x^2");
button_18.setBackground(new Color(43, 11, 51));
button_18.setFont(new Font("ARIAL",Font.BOLD,30));
button_18.setForeground(Color.WHITE);
panel_2.add(button_1);
panel_2.add(button_2);
panel_2.add(button_3);
panel_2.add(button_4);
panel_2.add(button_5);
panel_2.add(button_6);
panel_2.add(button_7);
panel_2.add(button_8);
panel_2.add(button_9);
panel_2.add(button_10);
panel_2.add(button_11);
panel_2.add(button_12);
panel_2.add(button_13);
panel_2.add(button_14);
panel_2.add(button_15);
panel_2.add(button_16);
panel_2.add(button_17);
panel_2.add(button_18);
}
public static void main(String[] args) {
new CalculatorUsingAWT();
}
}
这里的问题是 GUI 在设置为可见之前没有添加和验证组件(在 pack()
操作期间调用)。
frame.setLayout(null);
删除它。布置组件。有关布局计算器的一种方法,请参阅 this answer。
实际上要使用布局而不是 null
将该代码转换为 all,请使用 BorderLayout
。将 TextField
放入 BorderLayout.PAGE_START
并将 GridLayout
与按钮放入 BorderLayout.CENTER
- 布局完成!
frame.setVisible(true);
把它移到最后,紧跟在 frame.pack();
之后。这将使 GUI 具有所需的确切大小,以容纳 GUI 中的组件。
我正在研究计算器。我添加了一些按钮和一个文本字段。我面临的问题是,当我 运行 我的项目时,它不显示作为按钮的组件,但是当我调整框架大小时,按钮立即出现。这并不总是有时会出现按钮,有时直到调整框架大小后才会出现。尝试增加框架的宽度和高度,但这对我没有帮助。当按钮未显示时,我附上了计算器的代码和图像
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorUsingAWT {
CalculatorUsingAWT(){
Frame frame = new Frame("CALCKY");
Panel panel_1 = new Panel();
panel_1.setBounds(0,0,400,100);
panel_1.setBackground(new Color(43, 11, 51));
frame.add(panel_1);
frame.setLayout(null);
frame.setBounds(550,100,400,549);
// frame.setResizable(false);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TextField textField = new TextField();
textField.setBounds(25,45,350,40);
textField.setBackground(new Color(43, 11, 51));
textField.setForeground(Color.WHITE);
textField.setFont(new Font("SAN_SERIF",Font.BOLD,30));
textField.setEditable(false);
panel_1.setLayout(null);
panel_1.add(textField);
Panel panel_2 = new Panel();
panel_2.setLayout(new GridLayout(6,4));
panel_2.setBounds(0,100,401,450);
panel_2.setBackground(new Color(43, 11, 51));
frame.add(panel_2);
Button button_1 = new Button("1");
button_1.setBackground(new Color(43, 11, 51));
button_1.setFont(new Font("ARIAL",Font.BOLD,30));
button_1.setForeground(Color.WHITE);
Button button_2 = new Button("2");
button_2.setBackground(new Color(43, 11, 51));
button_2.setFont(new Font("ARIAL",Font.BOLD,30));
button_2.setForeground(Color.WHITE);
Button button_3 = new Button("3");
button_3.setBackground(new Color(43, 11, 51));
button_3.setFont(new Font("ARIAL",Font.BOLD,30));
button_3.setForeground(Color.WHITE);
Button button_4 = new Button("4");
button_4.setBackground(new Color(43, 11, 51));
button_4.setFont(new Font("ARIAL",Font.BOLD,30));
button_4.setForeground(Color.WHITE);
Button button_5 = new Button("5");
button_5.setBackground(new Color(43, 11, 51));
button_5.setFont(new Font("ARIAL",Font.BOLD,30));
button_5.setForeground(Color.WHITE);
Button button_6 = new Button("6");
button_6.setBackground(new Color(43, 11, 51));
button_6.setFont(new Font("ARIAL",Font.BOLD,30));
button_6.setForeground(Color.WHITE);
Button button_7 = new Button("7");
button_7.setBackground(new Color(43, 11, 51));
button_7.setFont(new Font("ARIAL",Font.BOLD,30));
button_7.setForeground(Color.WHITE);
Button button_8 = new Button("8");
button_8.setBackground(new Color(43, 11, 51));
button_8.setFont(new Font("ARIAL",Font.BOLD,30));
button_8.setForeground(Color.WHITE);
Button button_9 = new Button("9");
button_9.setBackground(new Color(43, 11, 51));
button_9.setFont(new Font("ARIAL",Font.BOLD,30));
button_9.setForeground(Color.WHITE);
Button button_10 = new Button("+");
button_10.setBackground(new Color(43, 11, 51));
button_10.setFont(new Font("ARIAL",Font.BOLD,30));
button_10.setForeground(Color.WHITE);
Button button_11 = new Button("-");
button_11.setBackground(new Color(43, 11, 51));
button_11.setFont(new Font("ARIAL",Font.BOLD,30));
button_11.setForeground(Color.WHITE);
Button button_12 = new Button("*");
button_12.setBackground(new Color(43, 11, 51));
button_12.setFont(new Font("ARIAL",Font.BOLD,30));
button_12.setForeground(Color.WHITE);
Button button_13 = new Button("/");
button_13.setBackground(new Color(43, 11, 51));
button_13.setFont(new Font("ARIAL",Font.BOLD,30));
button_13.setForeground(Color.WHITE);
Button button_14 = new Button("=");
button_14.setBackground(Color.BLUE);
button_14.setFont(new Font("ARIAL",Font.BOLD,30));
button_14.setForeground(Color.WHITE);
Button button_15 = new Button("%");
button_15.setBackground(new Color(43, 11, 51));
button_15.setFont(new Font("ARIAL",Font.BOLD,30));
button_15.setForeground(Color.WHITE);
Button button_16 = new Button("CE");
button_16.setBackground(new Color(43, 11, 51));
button_16.setFont(new Font("ARIAL",Font.BOLD,30));
button_16.setForeground(Color.WHITE);
Button button_17 = new Button("C");
button_17.setBackground(new Color(43, 11, 51));
button_17.setFont(new Font("ARIAL",Font.BOLD,30));
button_17.setForeground(Color.WHITE);
Button button_18 = new Button("x^2");
button_18.setBackground(new Color(43, 11, 51));
button_18.setFont(new Font("ARIAL",Font.BOLD,30));
button_18.setForeground(Color.WHITE);
panel_2.add(button_1);
panel_2.add(button_2);
panel_2.add(button_3);
panel_2.add(button_4);
panel_2.add(button_5);
panel_2.add(button_6);
panel_2.add(button_7);
panel_2.add(button_8);
panel_2.add(button_9);
panel_2.add(button_10);
panel_2.add(button_11);
panel_2.add(button_12);
panel_2.add(button_13);
panel_2.add(button_14);
panel_2.add(button_15);
panel_2.add(button_16);
panel_2.add(button_17);
panel_2.add(button_18);
}
public static void main(String[] args) {
new CalculatorUsingAWT();
}
}
这里的问题是 GUI 在设置为可见之前没有添加和验证组件(在 pack()
操作期间调用)。
frame.setLayout(null);
删除它。布置组件。有关布局计算器的一种方法,请参阅 this answer。
实际上要使用布局而不是null
将该代码转换为 all,请使用BorderLayout
。将TextField
放入BorderLayout.PAGE_START
并将GridLayout
与按钮放入BorderLayout.CENTER
- 布局完成!frame.setVisible(true);
把它移到最后,紧跟在frame.pack();
之后。这将使 GUI 具有所需的确切大小,以容纳 GUI 中的组件。