简体   繁体   English

将不同 swing 组件的 class 实例添加到 JPanel

[英]Adding a class instance of different swing components to a JPanel

I am trying to create a grid of different items that are displayed on a JPanel as shown here:我正在尝试创建一个显示在 JPanel 上的不同项目的网格,如下所示:

        JPanel secondPanel = new JPanel();

        secondPanel.setBounds(345,40,640,700);
        secondPanel.setBackground(new java.awt.Color(90,90,100));
        // Here secondPanel is given a gridlayout.  So the items appear in a gridded look.
        secondPanel.setLayout(new GridLayout(3,3,50,50));
        frame.add(secondPanel);

// The following are used as an example of different JPanels.  I am using this to give a demonstration of how the item layout would sort of look like.

        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());
        secondPanel.add(new JPanel());

Each item has the same properties, however.但是,每个项目都具有相同的属性。 They all have a select box.他们都有一个 select 盒子。 They all have a JPanel, they all have a piece of text etc. I thought it would be easier to just create a class that has all these values, and then add those to the JPanel, each one being a separate instance.他们都有一个 JPanel,他们都有一段文本等。我认为创建一个具有所有这些值的 class 会更容易,然后将它们添加到 JPanel,每个都是一个单独的实例。

Class: Class:

import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

public class itemClass {

    itemClass(String name, double cost){

        JPanel box = new JPanel();
        JLabel nameLabel = new JLabel();
        nameLabel.setText(name);
        JCheckBox selectBox = new JCheckBox("$ "+cost);
        box.setForeground(new java.awt.Color(80,80,90));
        box.setSize(50, 50);
        box.add(selectBox);

    }
}

And here I try and create class instances and add them to that panel using the add method:在这里,我尝试创建 class 实例并使用add方法将它们添加到该面板:

for (int i = 0; i < 9; i ++) {
            secondPanel.add(new itemClass("T-Shirt",20));
        }

The problem here is that the add method doesn't take in instances of the itemClass .这里的问题是add方法不接受itemClass的实例。 So, I'm looking for a way I can have a class of different swing components and then add them to the secondPanel panel.所以,我正在寻找一种方法,我可以拥有不同 swing 组件的 class,然后将它们添加到secondPanel面板。

You could write it something like this:你可以这样写:

import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

public class ItemPanel extends JPanel {

    ItemPanel(String name, double cost){
        JLabel nameLabel = new JLabel();
        nameLabel.setText(name);
        this.add(nameLabel);  // added this!
        JCheckBox selectBox = new JCheckBox("$ " + cost);
        this.setForeground(new java.awt.Color(80, 80, 90));
        this.setSize(50, 50);
        this.add(selectBox);
    }
}

The key thing is that your ItemPanel class needs to extend some class that extends JComponent .关键是您的ItemPanel class 需要扩展一些扩展JComponent的 class 。 Extending JPanel is the obvious choice because in this case you need "panel" behavior.扩展JPanel是显而易见的选择,因为在这种情况下,您需要“面板”行为。

The other way to do this would be to turn your class + constructor into a simple method that creates a JPanel , populates it, and then returns it.另一种方法是将您的 class + 构造函数转换为创建JPanel的简单方法,填充它,然后返回它。


Other points:其他要点:

  1. Class names should always start with an uppercase letter. Class 名称应始终以大写字母开头。 No exceptions.没有例外。

  2. Class names should be chosen carefully:应谨慎选择 Class 名称:

    • They should be describe (or at least hint at) the purpose of the class;它们应该描述(或至少暗示)class 的用途; eg ItemPanel is a panel that displays an "item".例如, ItemPanel是一个显示“项目”的面板。
    • Adding Class at the end of the name is not idiomatic.在名称末尾添加Class不是惯用的。 You don't call your dog "Fido Dog"... and you don't see names like that in the Java SE libraries 1 .您不会称您的狗为“Fido Dog”......而且您在 Java SE 库1中看不到这样的名字。
    • Indeed, the Class suffix is actually misleading, since it suggests that an instance of (say) ItemClass is a representation of a Java class.实际上, Class后缀实际上具有误导性,因为它表明(例如) ItemClass实例是 Java class 的表示 (It isn't. It is a representation of a component of a user interface!) (它不是。它是用户界面组件的表示!)

1 - There are one or two exceptions, but that's beside the point. 1 - 有一两个例外,但这不是重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM