gpt4 book ai didi

java - 如何用 Java 制作多组件 Swing 类

转载 作者:行者123 更新时间:2023-12-01 11:52:12 25 4
gpt4 key购买 nike

所以我试图在 JPanel 中创建多个 JLabel,然后将其添加到 JFrame,我正在使用该结构来设置 JLabels 的文本(其中 16 个),但文本未设置。为什么是这样?谢谢:)

package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;

public class Window extends JFrame {
/**
* Create the frame.
*/
GridBagConstraints layout = new GridBagConstraints();

public Window()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setLayout(new GridBagLayout());

InfoPane stats = new InfoPane();
layout.gridx = 0;
layout.gridy = 0;
layout.ipadx = 50;
add(stats.InfoPaneBox("John", "100", "UNSP", "Ukraine", "9\" Knife", "05", "£123", "18"), layout);

setVisible(true);
}
}

这是我想要添加到 JFrame 的多组件 jpanel。另外,在类中制作自定义组件然后将它们添加到主 JFrame 中的最佳方法是什么?

package GUI;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;



public class InfoPane
{

JPanel container = new JPanel();

JLabel one = new JLabel("Name:");
JLabel two = new JLabel("Health:");
JLabel three = new JLabel("Gang:");
JLabel four = new JLabel("Location:");
JLabel five = new JLabel("Weapon:");
JLabel six = new JLabel("Police Noriety:");
JLabel seven = new JLabel("Money:");
JLabel eight = new JLabel("Gang Noriety:");

JLabel nameJL = new JLabel();
JLabel healthJL = new JLabel();
JLabel gangJL = new JLabel();
JLabel locationJL = new JLabel();
JLabel weaponJL = new JLabel();
JLabel policeNorietyJL = new JLabel();
JLabel moneyJL = new JLabel();
JLabel gangNorietyJL = new JLabel();


public JPanel InfoPaneBox(String name, String health, String gang, String location, String weapon, String police, String money, String gangn)
{
nameJL.setText(name);
healthJL.setText(health);
gangJL.setText(gang);
locationJL.setText(location);
weaponJL.setText(weapon);
policeNorietyJL.setText(police);
moneyJL.setText(money);
gangNorietyJL.setText(gangn);

container.add(one);
container.add(two);
container.add(three);
container.add(four);
container.add(five);
container.add(six);
container.add(seven);
container.add(eight);
container.add(nameJL);
container.add(healthJL);
container.add(gangJL);
container.add(locationJL);
container.add(weaponJL);
container.add(policeNorietyJL);
container.add(moneyJL);
container.add(gangNorietyJL);
one.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredSoftBevelBorder(), "Stats", TitledBorder.LEFT, TitledBorder.TOP));
one.setPreferredSize(new Dimension(150,400));

return container;
}
}

最佳答案

在将框架设置为可见之前添加 pack();。所以 Window 的构造函数应该如下所示:

 public Window() 
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setLayout(new GridBagLayout());

InfoPane stats = new InfoPane();
layout.gridx = 0;
layout.gridy = 0;
layout.ipadx = 50;
add(stats.InfoPaneBox("John", "100", "UNSP", "Ukraine", "9\" Knife", "05", "£123", "18"), layout);

pack();

setVisible(true);
}

仍然存在很多布局问题,但这解决了标签不显示的问题。

关于java - 如何用 Java 制作多组件 Swing 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707392/

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