开发者

JFrame will not display panel/buttons (Separate Classes) - java

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);
开发者_Go百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜