gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:35:32 25 4
gpt4 key购买 nike

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

无论如何提前感谢=]

问候-SKENG-

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

用户界面 - 主要

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 中。尝试:

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

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

MP.setLayout(null);

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

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

关于java - JFrame 不会显示面板/按钮(单独的类)- java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5999327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com