我有一个类,它是扩展的JPanel
,具有不同的摆动组件,例如文本字段,复选框等。
我正在尝试从已实例化并添加到secondPanel
JPannel的类对象访问构造函数的参数。
例如费用,名称等。
secondPanel.add(new ProductDesign("GPU: RTX 2070",649.99,"src/resources/rtx_card_2070.png"));
secondPanel.add(new ProductDesign("CPU: Intel i7-8700k",469.99,"src/resources/i7-8700k.png"));
secondPanel.add(new ProductDesign("CPU: Intel i5-9600k",309.99,"src/resources/i5_9600k.png"));
我遍历第二个面板,并且能够获取某些挥杆组件的状态,在这种情况下,我可以使用方法
getAccessibleChild(3)
获取selectBox的值。for (Component secondPanel : secondPanel.getComponents()) {
ProductDesign.detect(secondPanel.getAccessibleContext().getAccessibleChild(3));
}
但是,我也希望能够获得每个类对象具有的值。例如,来自类构造函数的成本或名称。有没有办法通过此设置来做到这一点?
package main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import javax.accessibility.Accessible;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ProductDesign extends JPanel{
private static final long serialVersionUID = -8719763672439672064L;
final static int checkX = 130;
final static int checkY = 58;
ProductDesign(String name, double cost, String img){
JLabel productIcon = new JLabel();
productIcon.setBounds(10, 10, 100, 80);
productIcon.setIcon(new ImageIcon(img));
this.add(productIcon);
JLabel nameLabel = new JLabel(name);
nameLabel.setBounds(130,-30,400,100);
nameLabel.setForeground(Color.WHITE);
nameLabel.setFont(new Font(name, Font.BOLD, 20));
this.add(nameLabel);
JLabel priceTag = new JLabel("$"+cost);
priceTag.setBounds(130,28,100,40);
priceTag.setForeground(Color.white);
this.add(priceTag);
JCheckBox confirmItem = new JCheckBox();
confirmItem.setBounds(130,58,25,25);
this.add(confirmItem);
JLabel quantityText = new JLabel("Qty:");
quantityText.setForeground(Color.LIGHT_GRAY);
quantityText.setBounds(160,60,60,20);
this.add(quantityText);
JTextArea productQuantity = new JTextArea();
productQuantity.setBounds(190, 60, 60, 20);
this.add(productQuantity);
this.setLayout(null);
this.setBackground(new Color(100,100,110));
this.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(3.0f), new Color(110,110,120)));
}
public static void detect(Accessible accessible) {
if (((AbstractButton) accessible).isSelected()) {
((AbstractButton) accessible).setLocation(checkX-5, checkY);
}
else {
((AbstractButton) accessible).setLocation(checkX, checkY);
}
}
}
编辑:
我添加了一个动作监听器,该监听器现在位于
ProductDesign
类中package main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.Accessible;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ProductDesign extends JPanel{
private static final long serialVersionUID = -8719763672439672064L;
final static int checkX = 130;
final static int checkY = 58;
String name;
Double cost;
int count;
ProductDesign(String name, double cost, String img){
JLabel productIcon = new JLabel();
productIcon.setBounds(10, 10, 100, 80);
productIcon.setIcon(new ImageIcon(img));
this.add(productIcon);
JLabel nameLabel = new JLabel(name);
nameLabel.setBounds(130,-30,400,100);
nameLabel.setForeground(Color.WHITE);
nameLabel.setFont(new Font(name, Font.BOLD, 20));
this.add(nameLabel);
JLabel priceTag = new JLabel("$"+cost);
priceTag.setBounds(130,28,100,40);
priceTag.setForeground(Color.white);
this.add(priceTag);
JCheckBox confirmItem = new JCheckBox();
confirmItem.setBounds(130,58,25,25);
this.add(confirmItem);
confirmItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (confirmItem.isSelected()) {
confirmItem.setLocation(checkX-5, checkY);
}
else {
confirmItem.setLocation(checkX, checkY);
}
}
});
JLabel quantityText = new JLabel("Qty:");
quantityText.setForeground(Color.LIGHT_GRAY);
quantityText.setBounds(160,60,60,20);
this.add(quantityText);
JTextArea productQuantity = new JTextArea();
productQuantity.setBounds(190, 60, 60, 20);
this.add(productQuantity);
this.setLayout(null);
this.setBackground(new Color(100,100,110));
this.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(3.0f), new Color(110,110,120)));
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
}
从此动作侦听器中,使用那些非静态方法来获取所选复选框的特定实例的成本/名称和计数的正确语法是什么?
最佳答案
为参数创建实例变量
将参数保存在实例变量中
根据需要创建getName()
,getCost()
方法以访问数据。
在课程开始时:
private String name;
private int cost;
...
在构造函数中:
this.name = name;
this.cost = cost;
...
类中的自定义方法:
public String getName()
{
return name;
}
...
编辑:
ActionListener中“ confirmItem”复选框的代码可能类似于:
JCheckBox checkBox = (JCheckBoox)event.getSource();
ProductDesign pd = (ProductDesign)checkbox.getParent();
String name = pd.getName();
关于java - 从JPanel之外的类对象访问变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58239281/