【问题标题】:gui help... with an actionListenergui 帮助...与 actionListener
【发布时间】:2013-11-22 23:06:58
【问题描述】:

我正在编写我的第一个 GUI 程序,它实际上做了一些事情,但我遇到了动作侦听器的问题。完成后的程序将采用双重输入,并根据我尚未添加的一些单选按钮选择从一个单位转换为另一个单位。现在的问题是 Action 侦听器无法识别我的文本字段。

我在单独的面板中有一个输入文本字段和一个输出文本字段。我创建了一个动作侦听器,并将输入文本字段添加到侦听器。

ActionListener handler = new HandlerClass(); textField.addActionListener(handler); 然后我为处理程序类创建了一个类内定义,但是当我编写操作执行方法 textField 时,程序无法解析输出。谁能看到我做错了什么?

public class conversionDisplay extends JFrame{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private JPanel northPanel;
private JPanel southPanel;
private JPanel eastPanel;
private JPanel westPanel;

public conversionDisplay() {
    super("Temperature Conversion");


    northPanel = new JPanel();                          //create northPanel
    northPanel.setLayout(new GridLayout(1,2,5,5));
    northPanel.add(new JPanel());
    JPanel northLabelPanel = new JPanel(new BorderLayout()) ;
    northLabelPanel.add(new JLabel("Input"), BorderLayout.EAST);
    northPanel.add(northLabelPanel);
    JTextField textField =new JTextField(10);
    northPanel.add(textField);
    northPanel.add(new JPanel());


    southPanel = new JPanel();                          //create southPanel
    southPanel.setLayout(new GridLayout(1,2));
    southPanel.add(new JPanel());
    JPanel southLabelPanel = new JPanel(new BorderLayout());
    southLabelPanel.add(new JLabel("Output "), BorderLayout.EAST);
    southPanel.add(southLabelPanel);
    JTextField output;
    southPanel.add(output = new JTextField( 10));
    output.setEditable(false);
    southPanel.add(new JPanel());





    add(northPanel,BorderLayout.NORTH);                 //add north panel
    add(southPanel,BorderLayout.SOUTH);                 //add north panel


    ActionListener handler = new HandlerClass();
    textField.addActionListener(handler);

    setSize(350, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);
}
private class HandlerClass implements ActionListener{


public void actionPerformed(ActionEvent e) {
    double input = textField.getText();
    if (input != 0)
    {

        output.setText(input);  //Perform conversion
    }

}



}

}

【问题讨论】:

    标签: java swing user-interface event-handling jtextfield


    【解决方案1】:

    您的 textField JTextField 在构造函数中声明,因此仅在该块中可见(同样是构造函数)。您需要将其设为类的实例字段。

    即,

    public class Foo {
    
      private Bar bar = new Bar();  // this field is visible throughout the object
    
      public Foo() {
        Baz baz = new Baz(); // this is only visible within this constructor
      } 
    

    因此,就像上面的 bar 变量是可见的,而在构造函数中声明的 baz 变量不可见一样,您需要将 JTextField 变量声明移出构造函数

    【讨论】:

      【解决方案2】:

      您必须将 String 解析为 Double 值

      public void actionPerformed(ActionEvent e) {
          double input = Double.parseDouble(textField.getText());
          if (input != 0)
          {
              output.setText(input+"");  //Perform conversion
          }
      
      }
      

      并将JTextField output,textField 声明为 Globel。

      【讨论】:

      • 这是一个很好的建议,但问题是 OP“如何”引用文本字段 ;)
      • 我找不到你。什么意思??
      • 基本问题是,OP 很难解决对他们想要访问的文本字段的引用...如果您尝试编译 OPs 代码,它不会,因为textFieldoutput 是未知变量...
      • 并不是你的建议是错误的,它只是没有解决核心问题;)
      【解决方案3】:
      public class conversionDisplay extends JFrame{
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      
      private JPanel northPanel;
      private JPanel southPanel;
      private JPanel eastPanel;
      private JPanel westPanel;
      JTextField output = new JTextField(10);    
      
      public conversionDisplay() {
          super("Temperature Conversion");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2015-06-30
        • 2012-02-07
        • 2015-12-02
        相关资源
        最近更新 更多