【问题标题】:Storing text from JTextField从 JTextField 存储文本
【发布时间】:2014-02-24 22:04:48
【问题描述】:

所以我一直在研究这个程序,我无法从 3 个字段(a、b、c)中获取输入以存储为变量。任何帮助将不胜感激。一切似乎都正常,如果按下按钮,它会关闭窗口,但它不会继续,因为输入没有被存储。

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;

public class Herons extends JFrame implements ActionListener {
 public static JTextField a;
 public static JTextField b;
 public static JTextField c;
 public static String aa1;
 public static String bb1;
 public static String cc1;
 public static JFrame main = new JFrame("Herons Formula");
 public static JPanel myPanel = new JPanel(new GridLayout (0,1));
public static void main(String args[]){
     Herons object = new Herons();
    }
Herons(){
    //JFrame main = new JFrame("Herons Formula");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JPanel myPanel = new JPanel(new GridLayout (0,1));
    //JPanel pane = new JPanel(new GridLayout(0,1));

    myPanel.setPreferredSize(new Dimension(250,300));

    JTextField a = new JTextField(3);
    JTextField b = new JTextField(3);
    JTextField c = new JTextField(3);
    JButton find = new JButton("Calculate!");

    main.add(myPanel);
    myPanel.add(new JLabel ("Input the lengh of each side:"));
    main.add(myPanel);
    myPanel.add(new JLabel ("A:"));
    myPanel.add(a);

    myPanel.add(new JLabel ("B:"));
    myPanel.add(b);

    myPanel.add(new JLabel ("C:"));
    myPanel.add(c);

    myPanel.add(find);
    //find.setActionCommand("Calculate!");
    find.addActionListener(this);
    main.pack();
    main.setVisible(true);

    String aa = a.getText();
    String bb = b.getText();
    String cc = c.getText();
    aa1 = aa;
    bb1 = bb;
    cc1 = cc;

    //JOptionPane.showMessageDialog(null, myPanel);

}
 public void actionPerformed(ActionEvent e) {
    String actionCommand = ((JButton) e.getSource()).getActionCommand();
    //System.out.println("Action command for pressed button: " + actionCommand);
    if (actionCommand == "Calculate!") {



     main.setVisible(false);
     myPanel.setVisible(false);
     main.dispose();
   //String aa = a.getText();
   //String bb = b.getText();
   //String cc = c.getText();

    double aaa = Double.parseDouble(aa1);
    double bbb = Double.parseDouble(bb1);
    double ccc = Double.parseDouble(cc1);
    double s = 0.5 * (aaa + bbb + ccc);
    double area = Math.sqrt(s*(s-aaa)*(s-bbb)*(s-ccc));
    area = (int)(area*10000+.5)/10000.0;
    if (area == 0){
        area = 0;
    }
    JOptionPane.showMessageDialog(null, "The area of the triangle is: " + area);
    }
}
}

【问题讨论】:

  • 为什么从文本字段中获取文本的行被注释掉了?如果你喜欢double aaa = Double.parseDouble(a.getText());,它似乎应该可以工作。

标签: string jframe jpanel jtextfield bluej


【解决方案1】:

问题基本上是这样的:

class Herons {
    static JTextField a;

    Herons() {
        JTextField a = new JTextField(); // 'a' is shadowed
    }
}

当您在构造函数中说JTextField a = ... 时,它会声明一个名为a不同的局部变量 并隐藏该字段。

应该是这样的:

Herons() {
    a = new JTextField(); // field 'a' is assigned
}

否则您可能已经注意到,当您尝试在您的字段上调用 ​​getText 时,它们在 actionPerformed 中为空。

I found this page about hiding/shadowing如果你想了解更多。

附带说明,您的变量也不需要是静态的(也许不应该是,因为您正在创建类的实例来引用它们)。

【讨论】:

    【解决方案2】:

    您必须在 actionPerformed 方法中获取 TextFields 的值,而不是在构造函数中。当您在构造函数中获取这些文本字段的值时,它们是空的,因为它们刚刚被添加到接口中。所以在方法的开头加上这个:

    public void actionPerformed(ActionEvent e) {

    字符串 aa = a.getText();

    字符串 bb = b.getText();

    String cc = c.getText();

    并从构造函数中删除这些行:

    字符串 aa = a.getText();

    字符串 bb = b.getText();

    String cc = c.getText();

    aa1 = aa;

    bb1 = bb;

    cc1 = cc;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2021-02-16
      • 2015-08-30
      相关资源
      最近更新 更多