[现在正在工作!]我有这个代码应该计算百分比(使用一些GUI)。但计算部分不起作用......也许有些人知道答案?我认为问题是当我从一个字符串音乐会做一个双倍。我来自瑞典,所以我的英语很糟糕。所有变量名都是瑞典语,所以我只是用googles翻译它们。
package Java;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class java {
public static void main (String []args){
int frameSizeX = 500;
int frameSizeY = 135;
JPanel p = new JPanel();
p.setPreferredSize( new Dimension(frameSizeX,frameSizeY) );
JFrame f = new JFrame ("Percentage");
f.setSize(frameSizeX,frameSizeY);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.getContentPane().add(p);
JLabel cardiNum = new JLabel ("Cardinal number");
JTextField cardiNumField = new JTextField();
cardiNumField.setPreferredSize( new Dimension( 500, 24 ) );
final String talValue = (cardiNumField.getText());
JLabel perc = new JLabel("Percentage");
JTextField percField = new JTextField();
percField.setPreferredSize( new Dimension( 500, 24 ) );
final String proValue = (percField.getText());
double D = 0;
final JLabel answer = new JLabel("The answer is = " + D);
JButton butt = new JButton("Calculate");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double A = Double.parseDouble(talValue);
double B = Double.parseDouble(proValue);
double C = A/100;
double D = C*B;
answer.setText("The answer is" + D);
}
});
p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
p.add(perc, BorderLayout.AFTER_LINE_ENDS);
p.add(percField, BorderLayout.AFTER_LINE_ENDS);
p.add(butt);
p.add(answer);
f.pack();
f.setVisible(true);
}
}
答案 0 :(得分:1)
您正在获取NullPointerException,因为只需在ActionListener中单击JButton后设置这两个字符串。
把这两行:
final String talValue = (cardiNumField.getText());
final String proValue = (percField.getText());
在JButton的ActionListener中,并使cardiNumField和percField成为最终版,它应该可以工作。或者,您可以删除这两个变量并具有:
double A = Double.parseDouble(cardiNumField.getText());
double B = Double.parseDouble(percField.getText());
答案 1 :(得分:0)
正如Martin Dinov所提到的,问题是你在用户能够写入文本框之前阅读文本框的内容并单击按钮。这会导致NumberFormatException,因为您正在尝试将空字符串“”转换为double。因此,您需要将该代码放在按钮单击事件处理程序中。修复此问题的额外更改是您需要将文本字段设为最终。即。
final JTextField cardiNumField = new JTextField();
final JTextField percField = new JTextField();
那是因为按钮点击处理程序是一个匿名内部类,所以它只能读取最终变量。
以下代码修复了我认为您已经看到的问题。
public static void main(String[] args) {
int frameSizeX = 500;
int frameSizeY = 135;
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(frameSizeX, frameSizeY));
JFrame f = new JFrame("Percentage");
f.setSize(frameSizeX, frameSizeY);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.getContentPane().add(p);
JLabel cardiNum = new JLabel("Cardinal number");
final JTextField cardiNumField = new JTextField();
cardiNumField.setPreferredSize(new Dimension(500, 24));
JLabel perc = new JLabel("Percentage");
final JTextField percField = new JTextField();
percField.setPreferredSize(new Dimension(500, 24));
double D = 0;
final JLabel answer = new JLabel("The answer is = " + D);
JButton butt = new JButton("Calculate");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double cardinal = Double.parseDouble(cardiNumField.getText());
double percentage = Double.parseDouble(percField.getText());
double C = cardinal / 100;
double D = C * percentage;
answer.setText("The answer is " + D);
}
});
p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
p.add(perc, BorderLayout.AFTER_LINE_ENDS);
p.add(percField, BorderLayout.AFTER_LINE_ENDS);
p.add(butt);
p.add(answer);
f.pack();
f.setVisible(true);
}
学习Java祝你好运。你显然很新! :)