JFrame不会显示面板/按钮(单独的类)-java

JFrame不会显示面板/按钮(单独的类)-java,java,swing,jframe,Java,Swing,Jframe,因此,我需要为我的GUI使用两个类,一个类称为“UI”并有一个主方法,另一个是uicontrol,它将包含所有JPanel/按钮,最终是GridLayout。唯一的问题是我的JFrame不会显示任何按钮或JPanel,但在我为它们创建单独的类之前,一切都很好 无论如何,提前谢谢你=] 问候-斯京- //编辑:我已经在代码中添加了“application.add(MP);”,在我将其发布到此网站之前,我已将其删除。我想不起来为什么我只是尝试了一些东西,现在我又添加了一次,但仍然不起作用。另外,gr

因此,我需要为我的GUI使用两个类,一个类称为“UI”并有一个主方法,另一个是uicontrol,它将包含所有JPanel/按钮,最终是GridLayout。唯一的问题是我的JFrame不会显示任何按钮或JPanel,但在我为它们创建单独的类之前,一切都很好

无论如何,提前谢谢你=]

问候-斯京-

//编辑:我已经在代码中添加了“application.add(MP);”,在我将其发布到此网站之前,我已将其删除。我想不起来为什么我只是尝试了一些东西,现在我又添加了一次,但仍然不起作用。另外,gridlayout只是一个实验,我稍后会为它创建一个不同的面板;)

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

您没有将JPanel添加到JFrame中,也没有将任何JLabel或JButton添加到JPanel中。 尝试:

那么,但这是另一个问题,你为什么要这样做:

MP.setLayout(null);
首先,您应该避免空布局,然后这个方法调用是完全无用的,因为在您的代码中,您将MP layout设置为GridLayout(MP.setLayout(experimentLayout);)


除此之外,您没有构建anywhere UIControl S类。

除了0详细的答案之外,我不明白为什么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”更显式的名称命名类,称它们为“大型机”、“MainFramePanel”等等,这样您就可以通过读取其名称猜出组件的类型


我认为您应该再次阅读(Sun)Oracle的Swing教程。

Opse忘记重新应用代码“application.add(MP);”我已经这样做了,但仍然不起作用。您在哪里实例化UIControls类?
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);
    }
}