我可以让clear
和exit
工作得很好,但是为了我的生活能够弄清楚为什么submit
无法正常工作结果不会出现。这可能是我失踪的显而易见的事情。我之前有过工作,但一定是搞砸了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ApplicationPanel extends JPanel
{
private JLabel headLabel, firstLabel, lastLabel, addressLabel, emailLabel, gpaLabel, gmatLabel;
private JTextField firstname, lastname, addressbox, email, GPA, Gmat;
private JTextArea results;
private JButton submit, clear, exit;
public ApplicationPanel()
{
headLabel = new JLabel ("Catskill College Application");
firstLabel = new JLabel ("Enter your first name: ");
lastLabel = new JLabel ("Enter your last name");
addressLabel = new JLabel ("Enter your address: ");
emailLabel = new JLabel ("Enter your email: ");
gpaLabel = new JLabel ("Enter your GPA: ");
gmatLabel = new JLabel ("Select your GMAT score: ");
results = new JTextArea(7, 30);
firstname = new JTextField (12);
lastname = new JTextField (12);
addressbox = new JTextField (20);
email = new JTextField (20);
GPA = new JTextField (5);
Gmat = new JTextField (5);
submit = new JButton ("SUBMIT");
clear = new JButton ("CLEAR");
exit = new JButton ("EXIT");
ButtonListener listener = new ButtonListener();
submit.addActionListener (listener);
clear.addActionListener (listener);
exit.addActionListener (listener);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 20, 5);
constraints.anchor = GridBagConstraints.CENTER;
add (headLabel, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (firstLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (firstname, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (lastLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (lastname, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (addressLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (addressbox, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (emailLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (email, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (gpaLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 5;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (GPA, constraints);
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.EAST;
add (gmatLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 6;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (Gmat, constraints);
constraints.gridx = 0;
constraints.gridy = 8;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.insets = new Insets (25, 85, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (submit, constraints);
constraints.gridx = 0;
constraints.gridy = 8;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.insets = new Insets (25, 225, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (clear, constraints);
constraints.gridx = 0;
constraints.gridy = 8;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.insets = new Insets (25, 355, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add (exit, constraints);
constraints.gridx = 0;
constraints.gridy = 9;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.insets = new Insets (5, 5, 5, 5);
constraints.anchor = GridBagConstraints.CENTER;
add (results, constraints);
setPreferredSize (new Dimension(700, 500));
setBackground (Color.GRAY);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == submit)
if (firstname.getText().length() == 0)
{
JOptionPane.showMessageDialog(submit,"You didn't enter your name!!", "Message", JOptionPane.WARNING_MESSAGE);
firstname.requestFocus();
firstname.setBackground(Color.YELLOW);
}
else if (lastname.getText().length() == 0)
{
JOptionPane.showMessageDialog(submit,"You didn't enter your address!!", "Message", JOptionPane.WARNING_MESSAGE);
lastname.requestFocus();
lastname.setBackground(Color.YELLOW);
}
else
{
String Acceptance, name1;
if ((Integer.parseInt(GPA.getText()) >2.99) && (Integer.parseInt(Gmat.getText()) > 400) )
{
Acceptance = "Unconditional Acceptance";
name1 = firstname.getText()+ lastname.getText();
}
else if ((Integer.parseInt(GPA.getText()) > 2.49 ) && (Integer.parseInt(Gmat.getText()) > 350) )
{
Acceptance = "Conditional Acceptance";
name1 = firstname.getText()+ lastname.getText();
}
else
{
Acceptance = "Application Rejected";
name1 = firstname.getText()+ lastname.getText();
}
results.setText("Name : " + name1 + "\n" + "Address: " + addressbox.getText() + "\n" + "Application Status: " + Acceptance);
}
if (event.getSource() == clear)
{
firstname.setText("");
lastname.setText("");
addressbox.setText("");
email.setText("");
GPA.setText("");
Gmat.setText("");
}
if (event.getSource() == exit)
{
System.exit(0);
}
}
}
}
这是控制台的内容:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "3.95"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ApplicationPanel$ButtonListener.actionPerformed(ApplicationPanel.java:231)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
答案 0 :(得分:0)
将它从Int更改为Double修复它(呃)谢谢!