所以我一直在研究这个程序而且我无法从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);
}
}
}
答案 0 :(得分:0)
问题基本上是这样的:
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如果您想了解更多内容。
作为旁注,你的变量也不需要是静态的(也许不应该是因为你正在创建一个类的实例来引用它们。)
答案 1 :(得分:0)
您必须在actionPerformed方法中获取TextFields的值,而不是在构造函数中。当你在构造函数中获得这些文本字段的值时,它们是空的,因为它们刚刚被添加到接口中。所以在方法的开头添加:
public void actionPerformed(ActionEvent e){
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
并从构造函数中删除这些行:
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
aa1 = aa;
bb1 = bb;
cc1 = cc;