gpt4 book ai didi

java - 从 JPanel 之外的类对象访问变量

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

我有一个类,它是一个扩展的 JPanel ,具有不同的 swing 组件,如文本字段、复选框等。

我正在尝试从已实例化并添加到 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"));

我迭代第二个面板,并且能够获取一些 swing 组件的状态,在本例中,我可以使用方法 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);
}

}
}

编辑:

我添加了一个 Action 监听器,它现在位于 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;
}
}

从此操作监听器中,使用这些非静态方法获取所选复选框的特定实例的成本/名称和计数的正确语法是什么?

最佳答案

  1. 为参数创建实例变量
  2. 将参数保存在实例变量中
  3. 根据需要创建 getName()getCost() 方法来访问数据。

类(class)开始时:

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/

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