Reputation: 13
I do not know if this is a duplicate question but when I run my java swing GUI code nothing shows up even with all the visibility's set as true. I am new to java swing gui creation so any help would be greatly appreciated. here is my pages class
class Pages {
public static void Welcome_Screen() {
// Create the main screen for adding the other panels and buttons
JPanel Main_Screen = new JPanel(new GridBagLayout());
GridBagConstraints Main_Screen_Constraints = new GridBagConstraints();
// Set the size of the Main_Screen
Main_Screen.setSize(1280, 720);
// Create the three panels needed to make this work
// Set the layouts as GridBagLayout and create the constraints needed
JPanel Side_Panel = new JPanel();
Side_Panel.setLayout(new GridBagLayout());
GridBagConstraints Side_Panel_Constraints = new GridBagConstraints();
JPanel Profile_Panel = new JPanel();
Profile_Panel.setLayout(new GridBagLayout());
GridBagConstraints Profile_Panel_Constraints = new GridBagConstraints();
JPanel Button_Panel = new JPanel();
Button_Panel.setLayout(new GridBagLayout());
GridBagConstraints Button_Panel_Constraints = new GridBagConstraints();
JPanel About_Panel = new JPanel();
About_Panel.setLayout(new GridBagLayout());
GridBagConstraints About_Panel_Constraints = new GridBagConstraints();
// Change the sides of the panels to fit them correctly
Side_Panel.setPreferredSize(new Dimension(100, 720));
Side_Panel.setMinimumSize(new Dimension(100, 720));
Side_Panel.setMaximumSize(new Dimension(150, 1080));
Profile_Panel.setPreferredSize(new Dimension(100, 100));
Button_Panel.setPreferredSize(new Dimension(100, 620));
About_Panel.setPreferredSize(new Dimension(1180, 720));
// Change the border types to null
Side_Panel.setBorder(null);
Profile_Panel.setBorder(null);
Button_Panel.setBorder(null);
About_Panel.setBorder(null);
// Create the Profile_Panel items
ImageIcon Profile_Icon = new ImageIcon("Images/Profile_Icon.png");
JButton Profile_Button = new JButton(Profile_Icon);
Profile_Button.setSize(100, 100);
Profile_Panel_Constraints.gridx = 0;
Profile_Panel_Constraints.gridy = 0;
Profile_Panel.add(Profile_Button, Profile_Panel_Constraints);
// Create the Buttons for the Button_Panel
ImageIcon Java_Icon = new ImageIcon("Images/Java_Icon.png");
JButton Java_Button = new JButton(Java_Icon);
Java_Button.setSize(100, 155);
Button_Panel_Constraints.gridx = 0;
Button_Panel_Constraints.gridy = 0;
Button_Panel.add(Java_Button, Button_Panel_Constraints);
ImageIcon Python_Icon = new ImageIcon("Images/Python_Icon.png");
JButton Python_Button = new JButton(Python_Icon);
Python_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 1;
Button_Panel.add(Python_Button, Button_Panel_Constraints);
ImageIcon CPP_Icon = new ImageIcon("Images/CPP_Icon.png");
JButton CPP_Button = new JButton(CPP_Icon);
CPP_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 2;
Button_Panel.add(CPP_Button);
ImageIcon CS_Icon = new ImageIcon("Images/CS_Icon.png");
JButton CS_Button = new JButton(CS_Icon);
CS_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 3;
Button_Panel.add(CS_Button);
// About panel set up
About_Panel.setLayout(new BorderLayout());
JButton About_Panel_PlaceHolder = new JButton();
About_Panel_PlaceHolder.setSize(1180, 720);
About_Panel_Constraints.gridx = 0;
About_Panel_Constraints.gridy = 0;
About_Panel.add(About_Panel_PlaceHolder);
// Add the panels to the Main_Screen
Side_Panel_Constraints.gridx = 0;
Side_Panel_Constraints.gridy = 0;
Side_Panel.add(Profile_Panel, Side_Panel_Constraints);
Side_Panel_Constraints.gridy = 1;
Side_Panel.add(Button_Panel, Side_Panel_Constraints);
Main_Screen_Constraints.gridx = 0;
Main_Screen_Constraints.gridy = 0;
Main_Screen.add(Side_Panel, Main_Screen_Constraints);
Main_Screen_Constraints.gridx = 1;
Main_Screen.add(About_Panel, Main_Screen_Constraints);
// Set the Main_Screen visibility to true
Side_Panel.setVisible(true);
About_Panel.setVisible(true);
Main_Screen.setVisible(true);
}
}
I understand that the code probably isn't orthodox programming as I am still learning how to do it. But any help with the UI not showing up would be greatly appreciated!
Upvotes: 0
Views: 127
Reputation: 1526
Your Problem is that all java swing applications are in a Frame. Panel is inside a frame.
Your solution would be :
class Pages extends JFrame {
public Pages() {
// Create the main screen for adding the other panels and buttons
JPanel Main_Screen = new JPanel(new GridBagLayout());
GridBagConstraints Main_Screen_Constraints = new GridBagConstraints();
// Set the size of the Main_Screen
Main_Screen.setSize(1280, 720);
// Create the three panels needed to make this work
// Set the layouts as GridBagLayout and create the constraints needed
JPanel Side_Panel = new JPanel();
Side_Panel.setLayout(new GridBagLayout());
GridBagConstraints Side_Panel_Constraints = new GridBagConstraints();
JPanel Profile_Panel = new JPanel();
Profile_Panel.setLayout(new GridBagLayout());
GridBagConstraints Profile_Panel_Constraints = new GridBagConstraints();
JPanel Button_Panel = new JPanel();
Button_Panel.setLayout(new GridBagLayout());
GridBagConstraints Button_Panel_Constraints = new GridBagConstraints();
JPanel About_Panel = new JPanel();
About_Panel.setLayout(new GridBagLayout());
GridBagConstraints About_Panel_Constraints = new GridBagConstraints();
// Change the sides of the panels to fit them correctly
Side_Panel.setPreferredSize(new Dimension(100, 720));
Side_Panel.setMinimumSize(new Dimension(100, 720));
Side_Panel.setMaximumSize(new Dimension(150, 1080));
Profile_Panel.setPreferredSize(new Dimension(100, 100));
Button_Panel.setPreferredSize(new Dimension(100, 620));
About_Panel.setPreferredSize(new Dimension(1180, 720));
// Change the border types to null
Side_Panel.setBorder(null);
Profile_Panel.setBorder(null);
Button_Panel.setBorder(null);
About_Panel.setBorder(null);
// Create the Profile_Panel items
ImageIcon Profile_Icon = new ImageIcon("Images/Profile_Icon.png");
JButton Profile_Button = new JButton(Profile_Icon);
Profile_Button.setSize(100, 100);
Profile_Panel_Constraints.gridx = 0;
Profile_Panel_Constraints.gridy = 0;
Profile_Panel.add(Profile_Button, Profile_Panel_Constraints);
// Create the Buttons for the Button_Panel
ImageIcon Java_Icon = new ImageIcon("Images/Java_Icon.png");
JButton Java_Button = new JButton(Java_Icon);
Java_Button.setSize(100, 155);
Button_Panel_Constraints.gridx = 0;
Button_Panel_Constraints.gridy = 0;
Button_Panel.add(Java_Button, Button_Panel_Constraints);
ImageIcon Python_Icon = new ImageIcon("Images/Python_Icon.png");
JButton Python_Button = new JButton(Python_Icon);
Python_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 1;
Button_Panel.add(Python_Button, Button_Panel_Constraints);
ImageIcon CPP_Icon = new ImageIcon("Images/CPP_Icon.png");
JButton CPP_Button = new JButton(CPP_Icon);
CPP_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 2;
Button_Panel.add(CPP_Button);
ImageIcon CS_Icon = new ImageIcon("Images/CS_Icon.png");
JButton CS_Button = new JButton(CS_Icon);
CS_Button.setSize(100, 155);
Button_Panel_Constraints.gridy = 3;
Button_Panel.add(CS_Button);
// About panel set up
About_Panel.setLayout(new BorderLayout());
JButton About_Panel_PlaceHolder = new JButton();
About_Panel_PlaceHolder.setSize(1180, 720);
About_Panel_Constraints.gridx = 0;
About_Panel_Constraints.gridy = 0;
About_Panel.add(About_Panel_PlaceHolder);
// Add the panels to the Main_Screen
Side_Panel_Constraints.gridx = 0;
Side_Panel_Constraints.gridy = 0;
Side_Panel.add(Profile_Panel, Side_Panel_Constraints);
Side_Panel_Constraints.gridy = 1;
Side_Panel.add(Button_Panel, Side_Panel_Constraints);
Main_Screen_Constraints.gridx = 0;
Main_Screen_Constraints.gridy = 0;
Main_Screen.add(Side_Panel, Main_Screen_Constraints);
Main_Screen_Constraints.gridx = 1;
Main_Screen.add(About_Panel, Main_Screen_Constraints);
// Set the Main_Screen visibility to true
this.setLayout(new FlowLayout())//use te layout you want;
this.add(Main_Screen);
this.setTitle("My Java Swing App");
this.setSize(500, 500);//you can also use .pack() to automatically set the size required
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this one is very important
this.setVisible(true);
}
public static openMainWindow(){
Pages frame = new Pages();
}
public static void main(String args[]){
openMainWindow();
}
}
Upvotes: 2