I'm relatively new to programming, and I'm trying to make a gui with buttons that change panel visibility when clicked. I haven't really done anything yet, but I set one button so far (buttontoNA) to change panel visibility when clicked (make panelmain, the one the button is in, invisible and panelNA visible. The button works, except for some reason, most of the time the frame is empty when i run the gui. When the panels, etc do appear, the button that is set to change visibility does work, but the problem is that the panels don't appear most of the time when i run the code. This is completely random. What is wrong?
//first class import javax.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class guitest{ public static void main (String[] args){ countryinfoframe myframe = new countryinfoframe(); } public static void actionPerformed (){ } //2nd class import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.*; import java.awt.*; import java.awt.event.*; //import javax.swing.*; //import java.awt.event.ActionEvent; //import java.awt.event.ActionListener; //import java.awt.event.MouseEvent; //import java.awt.event.WindowAdapter; //import java.awt.event.WindowEvent; public class countryinfoframe implements MouseListener{ JPanel panelNA; JPanel panelmain; public countryinfoframe(){ JFrame framemain = new JFrame("Country Information"); framemain.setVisible(true); framemain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); framemain.setSize(500,500); panelmain = new JPanel(); framemain.add(panelmain); panelmain.setVisible(true); JButton buttontoNA = new JButton("North America"); panelmain.add(buttontoNA); JButton buttontoSA = new JButton("South America"); panelmain.add(buttontoSA); JButton buttontoE = new JButton("Europe"); panelmain.add(buttontoE); JButton buttontoA = new JButton("Asia"); panelmain.add(buttontoA); JButton buttontoAF = new JButton("Africa"); panelmain.add(buttontoAF); JButton buttontoAU = new JButton("Australia"); panelmain.add(buttontoAU); buttontoNA.addMouseListener(this); panelNA = new JPanel(); framemain.add(panelNA); panelNA.setVisible(false); JLabel labelNA = new JLabel("North America"); panelNA.add(labelNA); JButton buttontoNAC1 = new JButton("NAC1"); panelNA.add(buttontoNAC1); JButton buttontoNAC2 = new JButton("NAC2"); panelNA.add(buttontoNAC2); JButton buttontoNAC3 = new JButton("NAC3"); panelNA.add(buttontoNAC3); JButton buttonfromNAtomain = new JButton("Back"); panelNA.add(buttonfromNAtomain); }//end constructor @Override public void mouseClicked(MouseEvent e) { panelmain.setVisible(false); panelNA.setVisible(true); //System.out.println(e); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } } }
BTW I do realize that there is a lot that is unnecessary.