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

问题描述 投票:0回答:1

我有一个扩展的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;
    }
}

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

java swing
1个回答
2
投票
  1. 为参数创建实例变量
  2. 将参数保存在实例变量中
  3. 根据需要创建访问数据的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();
© www.soinside.com 2019 - 2024. All rights reserved.