I am trying to create a grid of different items that are displayed on a JPanel as shown here:
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. 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.
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:
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
. So, I'm looking for a way I can have a class of different swing components and then add them to the secondPanel
panel.
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
. Extending JPanel
is the obvious choice because in this case you need "panel" behavior.
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.
Other points:
Class names should always start with an uppercase letter. No exceptions.
Class names should be chosen carefully:
ItemPanel
is a panel that displays an "item".Class
at the end of the name is not idiomatic. You don't call your dog "Fido Dog"... and you don't see names like that in the Java SE libraries 1 .Class
suffix is actually misleading, since it suggests that an instance of (say) ItemClass
is a representation of a 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.