JFrame不会显示面板/按钮(Separate Classes) - java

时间:2011-05-14 01:48:04

标签: java swing jframe

所以我需要为我的GUI使用两个类,一个类称为“UI”并且有一个main方法,另一个是UIControls,它将包含所有JPanels / Buttons并最终包含GridLayout。唯一的问题是我的JFrame不会显示任何按钮或JPanels,但在我为他们创建一个单独的类之前一切正常。

无论如何,提前谢谢=]

问候-SKENG -

//编辑:我已经有了“application.add(MP);”在我在此网站上发布之前删除了它的代码。 不记得我为什么只是尝试一些事情我现在又重新添加了它,它仍然无法正常工作。此外,gridlayout只是一个实验,我稍后会为此创建一个不同的面板;)

用户界面 - 主要

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); 
}
}

2 个答案:

答案 0 :(得分:3)

您不是要将JPanel添加到JFrame,也不是将任何JLabel或JButton添加到JPanel。 试试:

add(MP); //somewhere in your UIControls constructor

然后,这是另一个问题,你为什么这样做:

MP.setLayout(null);

首先你应该避免使用null布局,然后这个方法调用完全没用,因为在你的代码之后你将MP布局设置为GridLayout(MP.setLayout(experimentLayout);)

除此之外,你没有在任何地方构建UIControls类。

答案 1 :(得分:2)

除了0bbose回答,我不明白为什么UIControl扩展了UI类。

你应该做的是以更好的方式组织你的课程。一个“启动”并创建JFrame的主类。对于UIControl类,您可以将其添加为UI类的属性,然后在创建UI对象时将其实例化,并将其添加到面板/

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);
    }
}

顺便说一句,您可能想要使用比“UI”更明确的名称命名类,将它们称为“MainFrame”,“MainFramePanel”等等,这样您就可以猜测组件的类型,只需读取其名称即可。< / p>

我认为你应该再次浏览(Sun)Oracle的Swing教程。

相关问题