如何用Java创建一个多组件Swing类

时间:2015-02-24 22:13:08

标签: java swing oop

所以我试图在JPanel中创建多个JLabel,然后将其添加到JFrame,我使用构造来设置JLabel的文本(其中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。另外,在Classes中制作自定义组件然后将它们添加到主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;
    }
}

1 个答案:

答案 0 :(得分:1)

在将帧设置为可见之前添加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);
}

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