在 类 之间发送变量

Sending variables between classes

我有一个JFrameclass和一个JPanelclass。 当我单击 JFrame 中的一个按钮时,我的变量 addIp 获取输入的字符串。

如何在我的 JPanel class 中访问这个变量?

这是我的 JFrame class:

 public class Window extends JFrame{

    public static void main(String[] args) {
    new Window();
    }
}

public Window()
{
    this.setSize(1000,400);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setTitle("Assignment2 - CPU temperature");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    container = new JPanel(new BorderLayout());


    north = new JPanel();
    north.setLayout(new BorderLayout());
    ip = new JButton ("New");
    north.add(ip, BorderLayout.WEST);
    print = new JButton ("Print");
    north.add(print,BorderLayout.EAST);

    JPanel centerPanel = new JPanel();

    centerPanel.add(new JLabel("Time Step (in s): "));
    timeStep = new JTextArea("0.1",1,5);
    centerPanel.add(timeStep);
    start = new JButton("OK");
    stop = new JButton("STOP");
    ListenForButton lForButton = new ListenForButton();
    ip.addActionListener(lForButton);
    centerPanel.add(start);
    centerPanel.add(stop);

    north.add(centerPanel, BorderLayout.CENTER);



    west = new JPanel();
    JLabel temp = new JLabel("°C");
    west.add(temp);

    container.add(north, BorderLayout.NORTH);
    container.add(west,BorderLayout.WEST);
    container.add(pan, BorderLayout.CENTER);

    this.setContentPane(container);
    this.setVisible(true);
}

public class ListenForButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ip)
    {
       options.add(address);
       options.add(address_t);
       options.add(port);
       options.add(port_t);
         int result = JOptionPane.showConfirmDialog(null, options, "Please Enter an IP Address and the port wanted", JOptionPane.OK_CANCEL_OPTION);
         if(result==JOptionPane.OK_OPTION)
            {
             if(!address_t.getText().isEmpty())
                {
                    addIp=address_t.getText();
                }
            }

    }

而且我希望在我的 JPanel 中可以访问 addIP 变量 class:

public class GraphDisplay extends JPanel implements ActionListener {

double rand, lastrand, max, min, total, degr, average, temp, length;
ArrayList<Double> randL = new ArrayList<>();
ArrayList<Integer> tL = new ArrayList<>();
ArrayList<String> dateL = new ArrayList<>();
int lastT = 0;
Color red = new Color(255, 0, 0);
Color green = new Color(0, 200, 0);
Color blue = new Color(0, 0, 200);
Color yellow = new Color(200, 200, 0);
int i, k = 0, inc, j, t;


public GraphDisplay() {
    super();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

我只是在 class 的开头复制了你,因为其余的对这类问题来说并不有趣。

您有多种选择:

  • 将保存信息的对象 "push" 放入另一个对象中。为此,您的 Window class 需要持有 GraphDisplay class 的实例,并调用 GraphDisplay 具有的方法,例如 setIPaddress(String ip),传入字符串进入图形显示。请注意,Window 持有的实例必须是可视化的 GraphDisplay 实例,而不是任何旧实例。
  • 或者您可以让 GraphDisplay "pull" 中的信息。这里 Window 会通知任何侦听器 addIP 变量已更改,因此侦听器(这里可能是 GraphDisplay 实例)可以从 Window 调用 getAddIP() 从而获得新值。为此,您必须使用 Swing 的通知机制,例如 ChangeListener 或 PropertyChangeListener。