Home > OS >  Frame Not Showing Components Unless Resized (JAVA AWT). Cant Fix
Frame Not Showing Components Unless Resized (JAVA AWT). Cant Fix

Time:11-17

I am Working on a Calculator. I added some buttons and a textField. The problem I am facing is that when I run my project it does not show the components that are the buttons, but when I resized the frame the buttons appear instantly. This is not always sometimes the buttons appear and sometimes not until the frame is resized. Tried increasing the frame width and height but this didn't help me. I am attaching the code and the image of the calculator when the buttons are not showing up

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();
    }
}

IMPORTANT FOR REFFERENCE FOR THIS ISSUE

CodePudding user response:

The problem here is that the GUI has not had components added and been validated (which is called during a pack() operation) before being set visible.

  1. frame.setLayout(null); Remove that. Lay the components out. See this answer for one way to layout a calculator.

    Actually to convert that code to all using layout instead of the null, use a BorderLayout. Put the TextField in the BorderLayout.PAGE_START and the GridLayout with the buttons in the BorderLayout.CENTER - and the layout is done!
  2. frame.setVisible(true); move that to the end, and put it immediately after frame.pack();. This will make the GUI the exact size it needs to be, in order to accommodate the components in the GUI.
  • Related