So I need to used two classes for my GUI, one class is called “UI” and has a main method and the other is UIControls which will contain all the JPanels/Buttons and eventually the GridLayout. The only issue is my JFrame will not show any buttons or JPanels but before I made a seperate class for them everything worked fine.
Anyway thanks in advance =]
Regards -SKENG-
//EDIT: I already had “application.add(MP);” in the code I removed it before I posted it on this site. Cant remeber why I was just trying some things I’ve re-added it again now and It still doesnt work. Also the gridlayout was just a experiment I’m creating a different panel for that later 😉
UI – Main
import java.awt.*;
import javax.swing.*;
public class UI {
public static JFrame application;
public static void main(String []args) {
//=================FRAME SETTINGS=================
application = new JFrame("Despatch Depot Simulator");
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocation(500,200);
application.setSize(640,480);
application.setLayout(null);
application.setBackground(Color.WHITE);
application.setVisible(true);
application.setResizable(false);
}
}
UIControls
import java.awt.*;
import javax.swing.*;
public class UIControls extends UI {
public UIControls() {
//=================PANEL LAYOUT=================
JPanel MP = new JPanel(); //Main UI's Panel (MP = MainPanel)
MP.setBounds(1,1,640,480);
MP.setBackground(Color.WHITE);
MP.setLayout(null);
application.add(MP);
//=================JBUTTONS=================
JButton AddBox = new JButton("Add Box"); {MP.add(AddBox);}
AddBox.setBounds(505,2,125,30);
JButton AddTube = new JButton("Add Tube"); {MP.add(AddTube);}
AddTube.setBounds(505,34,125,30);
JButton AddEnvelope = new JButton("Add Envelope"); {MP.add(AddEnvelope);}
AddEnvelope.setBounds(505,66,125,30);
JButton ClearAll = new JButton("Clear All"); {MP.add(ClearAll);}
ClearAll.setBounds(505,98,125,30);
JButton CurrentCharge = new JButton("Current Charge"); {MP.add(CurrentCharge);}
CurrentCharge.setBounds(505,418,125,30);
JButton TotalCharge = new JButton("Total Charge"); {MP.add(TotalCharge);}
TotalCharge.setBounds(378,418,125,30);
JButton Save = new JButton("Save"); {MP.add(Save);}
Save.setBounds(2,418,125,30);
JButton Load = new JButton("Load"); {MP.add(Load);}
Load.setBounds(130,418,125,30);
//=================IMAGES=================
ImageIcon imageB = new ImageIcon ("..\\Images\\box.png");
ImageIcon imageBL = new ImageIcon ("..\\Images\\box-large.png");
ImageIcon imageEL = new ImageIcon ("..\\Images\\envelope-large.png");
ImageIcon imageEM = new ImageIcon ("..\\Images\\envelope-medium.png");
ImageIcon imageES = new ImageIcon ("..\\Images\\envelope-small.png");
ImageIcon imageT = new ImageIcon ("..\\Images\\tube.png");
ImageIcon imageTL = new ImageIcon ("..\\Images\\tube-large.png");
//=================LABELS=================
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
JLabel label5 = new JLabel();
JLabel label6 = new JLabel();
JLabel label7 = new JLabel();
GridLayout experimentLayout = new GridLayout(4,3);
MP.setLayout(experimentLayout);
}
}
You are not adding your JPanel to your JFrame nor adding any JLabel or JButton to your JPanel.
Try :
Then, but this is another problem, why are you doing :
First of all you should avoid null layout, then this method call is completely unuseful because after in your code you are setting MP layout to be a GridLayout (MP.setLayout(experimentLayout);)
In addition to that you are not constructing anywhere UIControls class.