Hi though i did complete the basics of java which im fairly new of, i keep getting errors when i try to add buttons on a new Frame/Panel. Care to educate me on what the problem might be?
import javax.swing.*;
import java.awt.*;
class MainClass {
String cont_orders;
private JFrame frame1;
private JPanel mainpanel;
JButton bt1, bt2, bt3, bt4, bt5;
private JButton btotal = new JButton("Order");
private JButton clearOr = new JButton("Clear");
private JTextField pricetotal = new JTextField();
private JTextField list_of_orders = new JTextField();
public MainClass(){
gui();
}
private void gui(){
frame1 = new JFrame("Order");
frame1.setSize(500,430);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setResizable(false);
mainpanel = new JPanel();
mainpanel.setBackground(Color.BLUE);
mainpanel.add(bt1);
bt1 = new JButton("M-Item 1 [Soda]");
frame1.add(mainpanel,BorderLayout.CENTER);
}
public static void main (String[]args){
new MainClass();
}
}
im trying to practice on coding normally instead of relying on that automatic one in NetBeans [JFrame Form/JPanel Form]
care to help?
Now this cannot be done in java
mainpanel.add(bt1);
bt1 = new JButton("M-Item 1 [Soda]");
Turn it around.
Explanation: the field bt1 is at that time a variable holding the null object.
That object (value) is added, not some variable address as in other languages.
Reverse it bt1 = new JButton("M-Item 1 [Soda]"
mainpanel.add(bt1);
Because if not the value of bt1 would be null so you must fill it first then use it .
Or
mainpanel.add(new JButton("..."));
Related
I've got probably trivial problem but I've spent hours in looking for answer.
I would like to create a button (ENTER button) that once clicked, removes certain components on the GUI (like numpad). The problem is that the class that defines instructions to do once button clicked doesn't see the components. I've tried to add implements ATM to this class but then the console returned very weird errors (when executing). Is there any 'clean' way to do this?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ATM extends JFrame{
// Container
int state = 0; // PIN screen
// ELEMENTS
JPanel container = new JPanel();
JTextArea display = new JTextArea("Please enter your PIN", 10, 50);
JTextField inputArea = new JTextField("");
JPanel buttons = new JPanel();
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton clear = new JButton("Clear");
JButton enter = new JButton("Enter");
JButton quit = new JButton("Quit");
// EVENTS
ButtonPresser buttonPress = new ButtonPresser(inputArea, display);
EnterPresser enterPress = new EnterPresser(inputArea, display, state, buttons);
ATM(){
super("ATM Cash Machine");
buildGUI();
pack();
setVisible(true);
}
private void buildGUI(){
// EVENT BINDINGS
one.addActionListener(buttonPress);
two.addActionListener(buttonPress);
three.addActionListener(buttonPress);
four.addActionListener(buttonPress);
five.addActionListener(buttonPress);
six.addActionListener(buttonPress);
seven.addActionListener(buttonPress);
eight.addActionListener(buttonPress);
nine.addActionListener(buttonPress);
zero.addActionListener(buttonPress);
clear.addActionListener(buttonPress);
quit.addActionListener(buttonPress);
enter.addActionListener(enterPress);
// ELEMENT SETTINGS
inputArea.setEditable(false);
display.setEditable(false);
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(display);
container.add(inputArea);
// Numeric pad
buttons.setLayout(new GridLayout(5,3));
buttons.add(one);
buttons.add(two);
buttons.add(three);
buttons.add(four);
buttons.add(five);
buttons.add(six);
buttons.add(seven);
buttons.add(eight);
buttons.add(nine);
buttons.add(clear);
buttons.add(zero);
buttons.add(enter);
buttons.add(quit);
container.add(buttons);
add(container, BorderLayout.NORTH);
}
// Main method
public static void main(String[] args){
ATM atm = new ATM();
}
}
class ButtonPresser implements ActionListener{
private JTextField iField;
private JTextArea oArea;
ButtonPresser(JTextField in, JTextArea out){
iField = in;
oArea = out;
}
public void actionPerformed(ActionEvent e){
switch(e.getActionCommand()){
case "Quit":
System.exit(0);
break;
case "Clear":
iField.setText("");
break;
default:
String fieldText = iField.getText();
if(fieldText.length() < 4){
iField.setText(fieldText+e.getActionCommand());
}
break;
}
}
}
class EnterPresser implements ActionListener{
private JTextField iField;
private JTextArea oArea;
private int state;
private JPanel buttons;
private final String PIN = "1234";
EnterPresser(JTextField in, JTextArea out, int st, JPanel but){
iField = in;
oArea = out;
state = st;
buttons = but;
}
public void actionPerformed(ActionEvent e){
if(state == 0){
String fieldText = iField.getText();
if(fieldText.equals(PIN)){
iField.setText("");
state = 1;
uiState0To1();
}
}
}
public void uiState0To1(){
buttons.remove(one);
}
}
The solution to your problem is simple. You need some way for your ButtonPresser class to talk with your ATM class, this is a classic example of an Observer Pattern
The idea is, you would provide some kind of event notification that your ButtonPresser will trigger under certain conditions, then your ATM class would listen for those events, it would then decide what it should do based on those events.
It is not the responsibility of the ButtonPresser to modify the state of ATM, just so we're clear.
You're now moving into the realm of Model-View-Controller, which could provide you a means to utilise CardLayout, which will further reduce the overall complexity of your problem, but also isolate responsibility and decouple your code
I am not sure which components you are trying to remove, but your problem is pretty clear. All of the components defined in the ATM class are not public. One way to manipulate these components from other classes would be to set them public.
The simplest way is to declare them as "public static" and reference them statically through the ATM class. Depending on your case you may need multiple instances of ATM, in which case you would not declare them static.
Here is another question with good info: Difference between public static and private static variables
I am a beginner Java-coder and a few days ago I felt confident enough in my skills to start my first "big" project. It was basically a calculator, a GUI(only JFrame, JPanels, JLabels and Buttons) that would display data, accept user input, grab some more data from other classes, then calculate stuff and finally update the GUI with the new JLabel values. However I never managed to get the update part done properly, whenever I would press the 'process'-button it would create a new JFrame with the new values, while the old one was still up.
I tried the obvious stuff (repaint(), revalidate(), etc) but that didn't work at all, then I started to shift things around, put parts of the code into new classes, copied code from the net until it eventually worked. However the code was a total mess and I didn't even really understand what went exactly wrong in the first place, so I trashed the entire thing.
Here is a very simplified version of my code before things went downhill:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_1 extends JFrame {
public static class clicks{
static int clicks = 0;
public int getclicks(){
return clicks;
}
public void setclicks(){
clicks = clicks+1;
}
}
public Test_1(){
clicks getNumber = new clicks();
int x = getNumber.getclicks();
//FRAME AND LAYOUT
JFrame window = new JFrame();
window.getContentPane().setBackground(Color.darkGray);
window.getContentPane().setLayout(new BorderLayout(20,10));
window.setTitle("Test Frame 1");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
// Top JPanel
JPanel northpanel = new JPanel();
LayoutManager northlayout = new FlowLayout();
northpanel.setLayout(northlayout);
// Top JPanel content
JLabel nlabel1 = new JLabel("Hello North");
nlabel1.setPreferredSize(new Dimension(100,20));
northpanel.add(nlabel1);
JPanel westpanel = new JPanel();
LayoutManager westlayout = new BoxLayout(westpanel, BoxLayout.Y_AXIS);
westpanel.setLayout(westlayout);
JLabel wlabel1 = new JLabel("Hello West");
wlabel1.setPreferredSize(new Dimension(100,20));
westpanel.add(wlabel1);
JPanel eastpanel = new JPanel();
LayoutManager eastlayout = new BoxLayout(eastpanel, BoxLayout.Y_AXIS);
eastpanel.setLayout(eastlayout);
JLabel elabel1 = new JLabel ("Hello East");
elabel1.setPreferredSize(new Dimension(100,20));
eastpanel.add(elabel1);
JButton southbutton = new JButton("start");
southbutton.setPreferredSize(new Dimension(400,50));
southbutton.addActionListener(new Action());
JPanel centralpanel = new JPanel();
JLabel clabel1 = new JLabel("Clicks: " + x);
centralpanel.add(clabel1);
window.add(centralpanel, BorderLayout.CENTER);
window.add(southbutton, BorderLayout.SOUTH);
window.add(eastpanel, BorderLayout.EAST);
window.add(westpanel, BorderLayout.WEST);
window.add(northpanel, BorderLayout.NORTH);
window.pack();
window.setVisible(true);
}
public static void main(String[] args) {
Test_1 window_start = new Test_1();
}
static class Action implements ActionListener{
#Override
public void actionPerformed (ActionEvent e){
clicks Numbers = new clicks();
Numbers.setclicks();
int test = Numbers.getclicks();
System.out.println("Button works, Number of clicks: "+test);
Test_1 updateData = new Test_1();
}
}
}
I know that the ActionListener creates a new instance of my JFrame, however that was the closest I ever came to "updating the JFrame" before I turned the code into Spaghetti. I assume that the way I build my code is the cause of my problem but creating the Frame and its content it different classes didn't work at all.
So my questions are:
Is there something really obvious I missing? Would it be possible to make this run the way I want to without completely changing it?
Is there a more efficient way to create a GUI? I get the feeling that the way I made this is total garbage.
I read other questions that dealt with similar problems but maybe it's because I am still pretty bad at Java but I couldn't really tell if they were related to my problem. Also I really want to understand this, so copying someone elses code wouldn't help at all.
Any help or comments are appreciated.
btw, the class click is something I just put there as a placeholder.
Alrighty I managed to get it to work. It's probably against the Etiquette to answer to his own question but I thought it might be useful for some beginners(like me yesterday). So here is my new code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_1 extends JFrame {
public static class clicks{
static int clicks = 0;
public int getclicks(){
return clicks;
}
public void setclicks(){
clicks = clicks+1;
}
}
clicks getNumber = new clicks();
int x = getNumber.getclicks();
JPanel northpanel, westpanel, eastpanel, southpanel, centralpanel;
static JLabel nlabel1, nlabel2, nlabel3, nlabel4, nlabel5;
static JLabel wlabel1, wlabel2, wlabel3, wlabel4, wlabel5;
static JLabel elabel1, elabel2, elabel3, elabel4, elabel5;
static JLabel clabel1;
JButton southbutton;
String TextnL, TextwL, TexteL;
public Test_1(){
setBackground(Color.darkGray);
setLayout(new BorderLayout(20,10));
setTitle("Test Frame 1");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setLocationRelativeTo(null);
setVisible(true);
nlabel1 = new JLabel("North_1");
nlabel2 = new JLabel("North_2");
nlabel3 = new JLabel("North_3");
nlabel4 = new JLabel("North_4");
nlabel5 = new JLabel("North_5");
wlabel1 = new JLabel("West_1 ");
wlabel2 = new JLabel("West_2 ");
wlabel3 = new JLabel("West_3 ");
wlabel4 = new JLabel("West_4 ");
wlabel5 = new JLabel("West_5 ");
elabel1 = new JLabel("East_1");
elabel2 = new JLabel("East_2");
elabel3 = new JLabel("East_3");
elabel4 = new JLabel("East_4");
elabel5 = new JLabel("East_5");
clabel1 = new JLabel("START");
southbutton = new JButton("Process");
southbutton.addActionListener(new Action());
northpanel = new JPanel();
northpanel.add(nlabel1);
northpanel.add(nlabel2);
northpanel.add(nlabel3);
northpanel.add(nlabel4);
northpanel.add(nlabel5);
add(northpanel, BorderLayout.NORTH);
westpanel = new JPanel();
LayoutManager wBox = new BoxLayout(westpanel, BoxLayout.Y_AXIS);
westpanel.setLayout(wBox);
westpanel.add(wlabel1);
westpanel.add(wlabel2);
westpanel.add(wlabel3);
westpanel.add(wlabel4);
westpanel.add(wlabel5);
add(westpanel, BorderLayout.WEST);
eastpanel = new JPanel();
LayoutManager eBox = new BoxLayout(eastpanel, BoxLayout.Y_AXIS);
eastpanel.setLayout(eBox);
eastpanel.add(elabel1);
eastpanel.add(elabel2);
eastpanel.add(elabel3);
eastpanel.add(elabel4);
eastpanel.add(elabel5);
add(eastpanel, BorderLayout.EAST);
centralpanel = new JPanel();
centralpanel.add(clabel1);
add(centralpanel, BorderLayout.CENTER);
add(southbutton, BorderLayout.SOUTH);
}
public static void main(String[] args) {
Test_1 window_start = new Test_1();
}
static class Action implements ActionListener{
#Override
public void actionPerformed (ActionEvent e){
clicks Numbers = new clicks();
Numbers.setclicks();
int test = Numbers.getclicks();
clabel1.setText("clicks: "+test);
}
}
}
And again, any comments/suggestions are welcome.
My Code:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea; //eclipse say Syntax error } expected
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
Cant find the solution, but i think it is embarrassing easy :/
I believe you wanted to put some of the code within a constructor, like this:
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
The problem was that you tried to execute arbitrary code outside any method. Once a field has been declared, you need to access it through a method. It is only possible to initialize it on the same line, so you can do like the following:
public class TAFrame {
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);
}
I recommend the constructor-approach in this case, but either way you will most need a constructor anyways since you probably want to add an actionlistener to the button (for example).
Most of the answers here have correctly pointed out that you can use initial values for initialization, and that you can use constructors. However, the Java tutorial, Initializing Fields, actually describes two non-constructor ways of initializing fields: (i) initial values; and (ii) initialization blocks.
The following code demonstrates all three methods (and shows both instance and static initialization blocks):
public class InitializationExample {
private int field1 = 1; // in-line initializer
private int field2a;
private static int field2b;
private int field3;
{
field2a = 3; // instance initializer
}
static {
field2b = 3; // static initializer
}
public InitializationExample( final int field3 ) {
this.field3 = field3;
}
}
Using the initialization blocks, you can make a very minor change to your code and have it compile:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
{
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
Even though this is possible, it isn't incredibly common, so unless you have some particularly good reason for using this, or it's already common in a codebase that you're working on, initial values or constructors are probably a more readable and maintainable option. It's also important to note that, according to 12.5. Creation of New Class Instances from the Java Language Specification, instance initialization code (and initial values) are processed before constructor code runs.
There is one possible benefit to the initialization block approach over constructor based methods. If you did make this a constructor, along the lines of
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
and then introduced another constructor later that takes some arguments, you'll either need to explicitly call the zero-argument constructor from that constructor (constructor chaining), or include initialization assignments in that constructor, too. Using the initialization blocks, you wouldn't need to do either.
Initialization blocks are also handy when you're creating an instance of an anonymous subclass, because you can keep the initialization code visually "inside" the class. You can read up on double brace initialization for more details, but here's a simple example:
import java.util.HashMap;
import java.util.Map;
public class MapInitializationExample {
public static void main(String[] args) {
// initialization code for this Map is visually "inside" the map
final Map<Integer,String> numberNames = new HashMap<Integer,String>() {{
put(1,"one");
put(2,"two");
put(3,"three");
}};
}
}
Because you are putting code outside of a method that does not belong.
This block in particular:
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
The error is a bit deceiving because it looks like private JTextArea textArea; needs a closing brace. But really, the problem is that the next line does not belong there. The next line, mainFrame = new JFrame("mainFrame"); suggests a method has started and the previous block never closed, thus the reference to a missing }.
You have two options:
Instantiate the objects in-line with their declarations
Instantiate the objects in a constructor
To Instantiate In-line
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);
}
To Instantiate with a Constructor
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
I personally prefer the inline approach as it is far less verbose.
You should do something like this.
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public void initComponents() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
Or you can create and instantiate the control there itself.
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);
So Im trying to create 3 panels. The first panel has the layout set (e.g. the radio buttons and next button) I`m now adding two new panels which have different background colors. But when I execute the code I get an error of Null point exception. How do I fix that?
Here is the code:
import java.awt.Color;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
import javax.swing.*;
public class Wizard {
private JLabel lblPicture;
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JButton btNext;
private JPanel panel;
private JPanel panelFirst;
private JPanel panelSecond;
CardLayout c1 = new CardLayout();
public static void main(String[] args) {
new Wizard();
}
public Wizard() {
JFrame frame = new JFrame("Wizard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,360);
frame.setVisible(true);
MyPanel();
RadioButtons();
Button();
Image();
groupButton();
panel.setLayout(c1);
panelFirst.setBackground(Color.BLUE);
panelSecond.setBackground(Color.GREEN);
panel.add(panelFirst,"1");
panel.add(panelSecond,"2");
c1.show(panel,"panel");
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public void MyPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void RadioButtons() {
btLdap = new JRadioButton ("Ldap");
btLdap.setBounds(60,85,100,20);
panel.add(btLdap);
btKerbegos = new JRadioButton ("Kerbegos");
btKerbegos.setBounds(60,115,100,20);
panel.add(btKerbegos);
btSpnego =new JRadioButton("Spnego");
btSpnego.setBounds(60,145,100,20);
panel.add(btSpnego);
btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60,175,100,20);
panel.add(btSaml2);
}
public void Button() {
btNext = new JButton ("Next");
btNext.setBounds(400,260,100,20);
panel.add(btNext);
btNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
c1.show(panel, "2");
}
});
}
public void Image() {
ImageIcon image = new ImageIcon("image.jpg");
lblPicture = new JLabel(image);
lblPicture.setBounds(200,20, 330, 270);
panel.add(lblPicture);
}
private void groupButton() {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(btLdap);
bg1.add(btKerbegos);
bg1.add(btSpnego);
bg1.add(btSaml2);
}
}
When I go to run your code I get the null pointer exception you mentioned:
Exception in thread "main" java.lang.NullPointerException
at Wizard.<init>(Wizard.java:35)
at Wizard.main(Wizard.java:20)
So, I looked up the line that produced it, in the constructor for Wizard:
panelFirst.setBackground(Color.BLUE);
I see you are setting a property on panelFirst, which is an instance data member of the Wizard class.
I don't see anywhere where you declared panelFirst = new JPanel();, which is what created your NullPointerException. It also looks like you haven't initialized many of the other variables as well (for instance, panel is the only JPanel I see that has been initialized).
Please look up the constructors for JPanel in the Java API and see how you want to create them for your app. You may also consider using an IDE to generate the GUI code for you.
JPanel API (as of jdk 1.7): http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html
Thanks
panelFirst and panelSecond objects are never created.
panelFirst and panelSecond variable is null it is not declared.
before setting background of panel you need to create it:
panelFirst= new JPanel();
same thing with panelSecond:
panelSecond = new JPanel();
When you have a null pointer exception (also known as NPE): you should try to find an uninitialized variable. When a variable is declared but not initialized its pointer is pointing to null (i.e it is a null pointer!)
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 :
add(MP); //somewhere in your UIControls constructor
Then, but this is another problem, why are you doing :
MP.setLayout(null);
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.
In addition to 0verbose answer, I don't see why UIControl extends the UI class.
What you should do is organize your classes in a better way. One main class that "launch" and create the JFrame. For the UIControl class, you can add it as an attribute of the UI class, then instantiate it when the UI object is created, and add it to the panel/
public class Launcher {
public static void main(String[] args)
{
UI uiFrame = new UI();
}
}
public class UI extends JFrame {
// the panel that will be added to the JFrame
private UIControl uiControlPanel;
public UI()
{
super("JFrame title");
// set size, layout, and other stuffs here
//
this.uiControlPanel = new UIControl();
this.add(this.uiControlPanel);
// make the window visible
this.setVisible(true);
}
}
By the way, you might wanna name your classes with more explicit names than "UI", call them "MainFrame", "MainFramePanel", and so on so you can guess the type of the component just reading its name.
I think you should go through Swing tutorials of (Sun) Oracle again.