1

so I'm trying to create a java program that you chose a version and clicks a button.

in my java program, I'm trying to use JPanels to make a type of "next page button" for my program. but whenever I run the program, and I click the button, it instantly crashes/freezes.

button code:

package main;

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings({ "rawtypes", "unused" })
public class Mainframe implements ActionListener {
    
    private static JFrame f;
    private static JPanel p1;
    private static JComboBox option;
    private static JButton launch;
    private static JPanel p2;
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        
        f=new JFrame("game");
        
        p1=new JPanel();
        f.add(p1);
        
        String s1[]={"Alpha_V1"};
        
        option=new JComboBox(s1);
        option.setBounds(10, 10, 100, 35);
        p1.add(option);
        
        launch=new JButton("Launch");
        launch.setBounds(10, 50, 100, 35);
        launch.addActionListener(new mainframe());
        p1.add(launch);
        
        p1.setLayout(null);
        f.setVisible(true);
        f.setSize(700,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        p2=new JPanel();
        p2.setLayout(null);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if (option.getSelectedItem().toString() == "V1") {
            
            // close p1 and open p2
            f.remove(p1);
            f.add(p2);
            
        } else if (option.getSelectedItem().toString() == "") {
            
            // do nothing lol
            
        } else {
            
            // do nothing 
            
        }
        
    }

}
jxon
  • 53
  • 6
  • Class names should begin in uppercase, so they don't get confused with variable names, which start in lowercase. – Progman Feb 11 '23 at 17:28
  • i dont understand why but ok – jxon Feb 11 '23 at 17:29
  • 1
    Check the java naming conventions on https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html – Progman Feb 11 '23 at 17:30
  • `String s1[] = {"Alpha_V1"}` is added to `option=new JComboBox(s1)` – jxon Feb 11 '23 at 17:36
  • 2
    Using the `null`-layout in Swing is not advisable. Check https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html for the different layout managers you can use and combine to get the GUI layout you want. – Progman Feb 11 '23 at 17:41
  • 1
    Check https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java on how to compare strings, do not use `==`. – Progman Feb 11 '23 at 17:41

1 Answers1

1

Your application isn't "crashed" or "freezes", it just haven't update the drawing. When you add or remove components to your JFrame (or other components), you have to call repaint() to inform the component that the content has been changed drastically and should be redrawn (now, or as soon as possible). Otherwise it would only redraw the GUI on other occasions like changing the size or when displayed initially. In fact, if you press the button and then change the size of the frame you will see that the GUI gets updated (with an empty space).

You can call the repaint() method like this:

 if (option.getSelectedItem().toString() == "V1") {
        
     // close p1 and open p2
     f.remove(p1);
     f.add(p2);
     f.repaint();
 }
Progman
  • 16,827
  • 6
  • 33
  • 48