在类之间发送变量

时间:2015-12-10 16:57:02

标签: java swing jframe jpanel

我有一个JFrame班级和一个JPanel班级。 当我点击JFrame我的变量addIp中的一个按钮时,取出输入的字符串。

如何在我的JPanel课程中访问此变量?

这是我的JFrame课程:

 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变量:

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;

我只是在课程的开头复制了你,因为其余部分对这类问题并不感兴趣。

1 个答案:

答案 0 :(得分:1)

您有几种选择:

  • 让持有信息的对象“推”到另一个对象中。为此,您的Window类需要保存GraphDisplay类的实例,并调用GraphDisplay具有的方法,比如setIPaddress(String ip),将String传入GraphDisplay。请注意,Window保存的实例必须是可视化的GraphDisplay实例,而不是任何旧实例。
  • 或者你可以让GraphDisplay“拉”信息。这里Window会通知任何听众addIP变量已经改变,所以听众,这可能是GraphDisplay实例,可以从Window调用getAddIP()从而获得新的价值。为此,您必须使用Swing的通知机制,例如ChangeListener或PropertyChangeListener。