0

My JLabel will not show immediately after I press the button until I resize the frame I think i have put frame.setVisible(true) in the right place I have looked at similar post and everyone has said the same thing about setvisible and to put it at the end have i put it in the right place can anyone help

 import java.awt.*;
 import java.awt.event.*;

 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JPasswordField;
 import javax.swing.JTextField;
 import javax.swing.plaf.basic.BasicButtonListener;
 import javax.swing.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JTextField;




public class GUI_login implements ActionListener{

private static JLabel label;
private static JTextField userText;
private static JLabel passwordLabel;
private static JPasswordField passwordText;
public static JButton button;
private static JLabel success;
private static JFrame frame;
private static JPanel panel;
private static JTextField search ;
private static JFrame frame2;
private static JTextField heightw1;
private static JTextField Weightw1;
private static JPanel panel2;
public JButton Done;
public static JTextField diff;
public JPanel f;




public static void main(String[] args) {
    
    panel = new JPanel();
    panel.setBackground(Color.cyan);
    frame = new JFrame("");
    frame.setSize(500,500);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    panel.setLayout(null);
        
    label = new JLabel("Username");
    label.setBounds(525, 250, 70, 20);
    panel.add(label); 
    
    userText = new JTextField();
    userText.setBounds(525, 270, 193, 28);
    panel.add(userText);
    
    passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(525, 300, 70, 20);
    panel.add(passwordLabel);
    
    passwordText = new JPasswordField();
    passwordText.setBounds(525, 320, 193, 28);
    panel.add(passwordText);
    
    button = new JButton("Login");
    button.setBounds(575, 350, 90, 25);
    button.setForeground(Color.WHITE);
    button.setBackground(Color.BLACK);
    button.addActionListener(new GUI_login());
    panel.add(button);
    
    success = new JLabel("");
    success.setBounds(10, 110, 300,25);
    panel.add(success);
    
    
    frame.setVisible(true);
}

void JavaExamples() {
    panel.setVisible(false);
    JPanel f = new JPanel();
    f.setBackground(Color.cyan);
    f.setBounds(0,0,1400,700);
    f.setLayout(null);
    frame.add(f);

    JLabel lb1 = new JLabel("Height(Inches):");
    lb1.setBounds(20, 20, 100, 40);//x axis, y axis, width, height
    f.add(lb1); //adding component in JFrame

    JLabel lb2 = new JLabel("Weight(Pounds):");
    lb2.setBounds(20, 60, 100, 40);//x axis, y axis, width, height
    f.add(lb2); //adding component in JFrame 
    
    JLabel lbResult = new JLabel("BMI Calculator in JAVA GUI");
    lbResult.setBounds(20, 90, 300, 40);//x axis, y axis, width, height
    f.add(lbResult); //adding component in JFrame 
    
    JTextField txtHeight = new JTextField("");
    txtHeight.setBounds(120, 20, 200, 40);//x axis, y axis, width, height
    f.add(txtHeight); //adding component in JFrame 

    JTextField txtWeight = new JTextField("");
    txtWeight.setBounds(120, 60, 200, 40);//x axis, y axis, width, height
    f.add(txtWeight); //adding component in JFrame 

    JButton btn = new JButton("BMI Calculator in JAVA GUI(CALCULATE)");//creating instance of JButton  
    btn.setBounds(20, 130, 300, 40);//x axis, y axis, width, height
    
    //Event
    btn.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            
            double weight=Double.parseDouble(txtWeight.getText());
            double height=Double.parseDouble(txtHeight.getText());
            
            double bmi = weight / Math.pow(height, 2) * 703;

             if (bmi < 18.5) {
                 lbResult.setText("underweight - BMI : "+bmi);
             } else if (bmi < 25) {
                 lbResult.setText("normal - BMI : "+bmi);
             } else if (bmi < 30) {
                 lbResult.setText("overweight - BMI : "+bmi);
             } else {
                 lbResult.setText("obese - BMI : "+bmi);
                 JLabel ob = new JLabel("NutriGYM");
                 ob.setBounds(600, 0, 100, 25);
                 f.add(ob);
             }
        }
    });
    
    JTextField Weighttxt = new JTextField();
    Weighttxt.setBounds(525, 300, 200, 25);
    f.add(Weighttxt);
    
    JTextField wweighttxt = new JTextField();
    wweighttxt.setBounds(525, 250, 200, 25);
    f.add(wweighttxt);
    
    JLabel W = new JLabel("Weight Wanted");
    W.setBounds(525, 275, 100, 25);
    f.add(W);
    
    JLabel H = new JLabel("Weight");
    H.setBounds(525, 225, 100, 25);
    f.add(H);
    
    JButton button1 = new JButton("Done");
    button1.setBounds(575, 325, 90, 25);
    button1.setForeground(Color.WHITE);
    button1.setBackground(Color.BLACK);
    f.add(button1);
    
    button1.addActionListener(new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent e) {                
            int one = Integer.parseInt(wweighttxt.getText());
            int two = Integer.parseInt(Weighttxt.getText());
            
            int total = (two - one);
            
            if(total>0) {
                String diff = Integer.toString(total);
                JLabel yes = new JLabel(diff);
                yes.setBounds(600,50,100,25);
                f.add(yes);
            } else {
                String diff = Integer.toString(total);
                JLabel yes1 = new JLabel(diff);
                yes1.setBounds(600,50,100,25);
                f.add(yes1);
            }
            
            
        }
    
    });

    f.add(btn);//adding button in JFrame  
    f.setLayout(null);
    f.setVisible(true);


    
    
}


    

public void actionPerformed(ActionEvent e) {
    int count1 = 0;
    System.out.println("button clicked");
    String user = userText.getText();
    @SuppressWarnings("deprecation")
    String password = passwordText.getText();
    System.out.println(user + ", " + password);
    int count = 0;
    do {
        if(user.equals("") && password.equals("")) {
             JavaExamples();
                
             
            
            count = 1;
            
            
        
    } else { 
        success.setText("login Failed");
        count1 = count1 + 1;
    }
    } while(count == 0);
    
    

    
    
    
    }

private LayoutManager BoxLayout() {
    // TODO Auto-generated method stub
    return null;
}

}

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    1) When first creating the frame you should add all components to the frame before making it visible. You have two setVisible() statements. Get rid of the first one. 2) When adding components dynamically to a visible frame you need to invoke `revalidate()` and `repaint()` on the panel you add the component to. 3) `Don't use a null layout`. Don't use setBounds() Swing was designed to be used with layout managers. – camickr Jun 30 '23 at 13:33

0 Answers0