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

发布于 2024-11-07 20:02:56 字数 3047 浏览 2 评论 0原文

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

无论如何,提前感谢 =]

问候 -SKENG-

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-11-14 20:02:56

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

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

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

MP.setLayout(null);

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

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

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.

○愚か者の日 2024-11-14 20:02:56

除了 0verbose 答案之外,我不明白为什么 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”等,这样您只需阅读其名称就可以猜测组件的类型。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文