JAVA AWT Frame窗口未调整大小时不显示组件问题无法修复
问题原因
你的代码存在setVisible调用时机错误的问题:你在仅添加了panel_1的时候就调用了frame.setVisible(true)将窗口设为可见,后续添加的输入框、按钮面板及所有按钮都没有触发窗口的重绘逻辑,因此会出现偶发不显示的问题。手动调整窗口大小时会自动触发界面重绘,所以隐藏的组件会立即显示。
修复方法
将frame.setVisible(true)语句移动到所有组件添加、配置完成的最后一行即可,同时可以额外增加重绘验证语句避免渲染异常。
修改后的完整代码如下:
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.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); // 将setVisible移到所有组件配置完成之后 frame.setVisible(true); // 可选添加验证和重绘逻辑,确保所有组件渲染生效 frame.validate(); frame.repaint(); } public static void main(String[] args) { new CalculatorUsingAWT(); } }
内容的提问来源于stack exchange,提问作者Rudransh Singh Mahra
相关产品推荐
相关产品推荐

