从actionperformed方法中检索值并在全局范围内使用它们

时间:2017-10-06 22:02:37

标签: java swing setpropertyactionlistener

 public class QuickStart4f extends JFrame {
 JPanel myjpdraw = new JPanel();
 JButton myjbdrawnow = new JButton("DRAW NOW");
 JLabel j1 = new JLabel("Point 1 : ");
 JTextField mytf1 = new JTextField("", 5);
 JTextField mytf2 = new JTextField("", 5);
 JLabel j2 = new JLabel("Point 2 : ");
 JTextField mytf3 = new JTextField("", 5);
 JTextField mytf4 = new JTextField("", 5);
 public double pt11;
 public double pt12;
 public double pt21;
 public double pt22;

myjpdraw.add(j1);
myjpdraw.add(mytf1);
myjpdraw.add(mytf2);
myjpdraw.add(j2 );
myjpdraw.add(mytf3);
myjpdraw.add(mytf4);
myjpdraw.add(myjbdrawnow);

getContentPane().add(myjpdraw,BorderLayout.SOUTH);

myjbdrawnow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        pt11 = Double.parseDouble(mytf1.getText());
        pt12 = Double.parseDouble(mytf2.getText());
        pt21 = Double.parseDouble(mytf3.getText());
        pt22 = Double.parseDouble(mytf4.getText());
    }
});
        System.out.print(pt11);
  }
 } 

当我从actionperformed方法中检索值pt11并尝试打印它时,它会给我一个空值。如何获取已在文本字段中输入的值并在按钮actionperformed方法之外访问它?

0 个答案:

没有答案