当我按下一个添加两个JTextField
的按钮时,如果文本字段中没有文本,则控制台会发送错误信息。我想知道如果文本字段中有一些文本并且只有在运行代码时我才能创建一个if语句。这是我的代码!
public class java {
public static void main (String []args){
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Calculator");
int frameSizeX = 500;
int frameSizeY = 184;
final JPanel p = new JPanel();
p.setPreferredSize( new Dimension(frameSizeX,frameSizeY) );
final JFrame f = new JFrame ("Calculator");
f.setSize(frameSizeX,frameSizeY);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//f.setResizable(false);
f.getContentPane().add(p);
/*JMenuBar bar = new JMenuBar ();
JMenu menu = new JMenu("Test");
menu.add(new JMenuItem("Hej"));
menu.add(bar);*/
final JLabel cardi = new JLabel ("First number");
final JTextField firstField = new JTextField();
firstField.setPreferredSize( new Dimension( 495, 24 ) );
final JLabel perc = new JLabel("Second number");
final JTextField secField = new JTextField();
secField.setPreferredSize( new Dimension( 495, 24 ) );
final JLabel answer = new JLabel("---");
final JLabel answer2 = new JLabel();
answer2.setForeground(Color.red);
final JButton percButt = new JButton("Percentage");
percButt.setPreferredSize(new Dimension(120, 24));
percButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
p.getRootPane().setDefaultButton(percButt);
double A = Double.parseDouble(firstField.getText());
double B = Double.parseDouble(secField.getText());
double C = A/100;
double D = C*B;
firstField.setText("" + D);
secField.setText("");
answer.setText(A + " % " + B + " = ");
answer2.setText("" + D);
}
});
final JButton subButt = new JButton("Subtraction");
subButt.setPreferredSize(new Dimension(120, 24));
subButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
p.getRootPane().setDefaultButton(subButt);
double A = Double.parseDouble(firstField.getText());
double B = Double.parseDouble(secField.getText());
double C = A - B;
double D = C;
firstField.setText("" + D);
secField.setText("");
answer.setText(A + " - " + B + " = ");
answer2.setText("" + D);
}
});
final JButton divButt = new JButton("Division");
divButt.setPreferredSize(new Dimension(120, 24));
divButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
p.getRootPane().setDefaultButton(divButt);
double A = Double.parseDouble(firstField.getText());
double B = Double.parseDouble(secField.getText());
double C = A/B;
double D = C;
firstField.setText("" + D);
secField.setText("");
answer.setText(A + " / " + B + " = ");
answer2.setText("" + D);
}
});
final JButton addButt = new JButton("Addition");
addButt.setPreferredSize(new Dimension(120, 24));
addButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
p.getRootPane().setDefaultButton(addButt);
double A = Double.parseDouble(firstField.getText());
double B = Double.parseDouble(secField.getText());
double C = A + B;
double D = C;
firstField.setText("" + D);
secField.setText("");
answer.setText(A + " + " + B +" = ");
answer2.setText("" + D);
}
});
p.getRootPane().setDefaultButton(addButt);
JButton sweButt = new JButton("Swedish");
sweButt.setPreferredSize(new Dimension(245, 24));
sweButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
addButt.setText("Addition");
subButt.setText("Subtraktion");
divButt.setText("Division");
percButt.setText("Procent");
cardi.setText("Första talet");
perc.setText("Andra talet");
f.setTitle("Kalkulator");
}
});
JButton engButt = new JButton("English");
engButt.setPreferredSize(new Dimension(245, 24));
engButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
addButt.setText("Addition");
subButt.setText("Subtraction");
divButt.setText("Division");
percButt.setText("Percentege");
cardi.setText("First number");
perc.setText("Second number");
f.setTitle("Calculator");
}
});
p.add(sweButt);
p.add(engButt);
p.add(cardi);
p.add(firstField);
p.add(perc);
p.add(secField);
p.add(addButt);
p.add(subButt);
p.add(divButt);
p.add(percButt);
p.add(answer);
p.add(answer2);
//f.setJMenuBar(bar);
f.pack();
f.setVisible(true);
}
}
答案 0 :(得分:0)
double A, B;
try {
A = Double.parseDouble(firstField.getText());
B = Double.parseDouble(secField.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(f, "Error", "Please enter two numbers",
JOptionPane.ERROR_MESSAGE);
return;
}
double C = A/100;
double D = C*B;
答案 1 :(得分:0)
此代码检查您的JTextField是否为空。如果为空,则触发一条消息并返回主框架。
if(jTextField.getText().trim().length()==0 && jTextField1.getText().trim().length()==0){
JOptionPane.showMessageDialog(this,"Please enter two numbers");
return;
}