我用eclipse做了一个简单的java swing程序,但是有一个问题;该代码仅在第一次按下计算按钮时才能正常工作,第一次出现故障后。我不知道为什么,我试图找到问题但找不到问题。这是代码:
import java.awt.BorderLayout;
public class Cgpa extends JFrame {
private JPanel contentPane;
private JTextField textFieldGpa1;
private JTextField textFieldGpa2;
private JTextField textFieldGpa3;
private JTextField textFieldGpa4;
private JTextField textFieldGpa5;
private JTextField textFieldGpa6;
private JTextField textFieldGpa7;
private JTextField textFieldGpa8;
private JTextField textFieldcr1;
private JTextField textFieldcr2;
private JTextField textFieldcr3;
private JTextField textFieldcr4;
private JTextField textFieldcr5;
private JTextField textFieldcr6;
private JTextField textFieldcr7;
private JTextField textFieldcr8;
private JLabel lblSemesterSgpa;
private JLabel lblSemesterSgpa_1;
private JLabel lblSemesterSgpa_2;
private JLabel lblSemesterSgpa_3;
private JLabel lblSemesterSgpa_4;
private JLabel lblSemesterSgpa_5;
private JLabel lblSemesterSgpa_6;
private JLabel lblSemesterSgpa_7;
private JLabel lblSemesterCredit;
private JLabel lblSemesterCredit_1;
private JLabel lblSemesterCredit_2;
private JLabel lblSemesterCredit_3;
private JLabel lblSemesterCredit_4;
private JLabel lblSemesterCredit_5;
private JLabel lblSemesterCredit_6;
private JLabel lblSemesterCredit_7;
private JButton btnNewButton;
double creditHoursTotalDegree=0;
String CGPA;
double temp =0;
String sgpaInput[] = new String[8];
String crHrInput[] = new String[8];
double Sgpa[] = new double[8];
double crHr[] = new double[8];
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cgpa frame = new Cgpa();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Cgpa() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 580, 357);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textFieldGpa1 = new JTextField();
textFieldGpa1.setColumns(10);
textFieldGpa2 = new JTextField();
textFieldGpa2.setColumns(10);
textFieldGpa3 = new JTextField();
textFieldGpa3.setColumns(10);
textFieldGpa4 = new JTextField();
textFieldGpa4.setColumns(10);
textFieldGpa5 = new JTextField();
textFieldGpa5.setColumns(10);
textFieldGpa6 = new JTextField();
textFieldGpa6.setColumns(10);
textFieldGpa7 = new JTextField();
textFieldGpa7.setColumns(10);
textFieldGpa8 = new JTextField();
textFieldGpa8.setColumns(10);
textFieldcr1 = new JTextField();
textFieldcr1.setColumns(10);
textFieldcr2 = new JTextField();
textFieldcr2.setColumns(10);
textFieldcr3 = new JTextField();
textFieldcr3.setColumns(10);
textFieldcr4 = new JTextField();
textFieldcr4.setColumns(10);
textFieldcr5 = new JTextField();
textFieldcr5.setColumns(10);
textFieldcr6 = new JTextField();
textFieldcr6.setColumns(10);
textFieldcr7 = new JTextField();
textFieldcr7.setColumns(10);
textFieldcr8 = new JTextField();
textFieldcr8.setColumns(10);
lblSemesterSgpa = new JLabel("Semester 1 SGpa");
lblSemesterSgpa_1 = new JLabel("Semester 2 SGpa");
lblSemesterSgpa_2 = new JLabel("Semester 3 SGpa");
lblSemesterSgpa_3 = new JLabel("Semester 4 SGpa");
lblSemesterSgpa_4 = new JLabel("Semester 5 SGpa");
lblSemesterSgpa_5 = new JLabel("Semester 6 SGpa");
lblSemesterSgpa_6 = new JLabel("Semester 7 SGpa");
lblSemesterSgpa_7 = new JLabel("Semester 8 SGpa");
lblSemesterCredit = new JLabel("Semester 1 Credit Hours");
lblSemesterCredit_1 = new JLabel("Semester 2 Credit Hours");
lblSemesterCredit_2 = new JLabel("Semester 3 Credit Hours");
lblSemesterCredit_3 = new JLabel("Semester 4 Credit Hours");
lblSemesterCredit_4 = new JLabel("Semester 5 Credit Hours");
lblSemesterCredit_5 = new JLabel("Semester 6 Credit Hours");
lblSemesterCredit_6 = new JLabel("Semester 7 Credit Hours");
lblSemesterCredit_7 = new JLabel("Semester 8 Credit Hours");
btnNewButton = new JButton("Calculate CGpa");
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
temp=0;
for(int i=0; i<8 ; i++)
{
Sgpa[i] = 0;
crHr[i] = 0;
}
sgpaInput[0] = textFieldGpa1.getText();
sgpaInput[1] = textFieldGpa2.getText();
if(textFieldGpa3.getText().equals(""))
{
sgpaInput[2] = "0";
}
else
{
sgpaInput[2] = textFieldGpa3.getText();
}
if(textFieldGpa4.getText().equals(""))
{
sgpaInput[3] = "0";
}
else
{
sgpaInput[3] = textFieldGpa4.getText();
}
if(textFieldGpa5.getText().equals(""))
{
sgpaInput[4] = "0";
}
else
{
sgpaInput[4] = textFieldGpa5.getText();
}
if(textFieldGpa6.getText().equals(""))
{
sgpaInput[5] = "0";
}
else
{
sgpaInput[5] = textFieldGpa6.getText();
}
if(textFieldGpa7.getText().equals(""))
{
sgpaInput[6] = "0";
}
else
{
sgpaInput[6] = textFieldGpa7.getText();
}
if(textFieldGpa8.getText().equals(""))
{
sgpaInput[7] = "0";
}
else
{
sgpaInput[7] = textFieldGpa8.getText();
}
crHrInput[0] = textFieldcr1.getText();
crHrInput[1] = textFieldcr2.getText();
if(textFieldcr3.getText().equals(""))
{
crHrInput[2] = "0";
}
else
{
crHrInput[2] = textFieldcr3.getText();
}
if(textFieldcr4.getText().equals(""))
{
crHrInput[3] = "0";
}
else
{
crHrInput[3] = textFieldcr4.getText();
}
if(textFieldcr5.getText().equals(""))
{
crHrInput[4] = "0";
}
else
{
crHrInput[4] = textFieldcr5.getText();
}
if(textFieldcr6.getText().equals(""))
{
crHrInput[5] = "0";
}
else
{
crHrInput[5] = textFieldcr6.getText();
}
if(textFieldcr7.getText().equals(""))
{
crHrInput[6] = "0";
}
else
{
crHrInput[6] = textFieldcr7.getText();
}
if(textFieldcr8.getText().equals(""))
{
crHrInput[7] = "0";
}
else
{
crHrInput[7] = textFieldcr8.getText();
}
Sgpa[0] = Double.parseDouble(sgpaInput[0]);
Sgpa[1] = Double.parseDouble(sgpaInput[1]);
Sgpa[2] = Double.parseDouble(sgpaInput[2]);
Sgpa[3] = Double.parseDouble(sgpaInput[3]);
Sgpa[4] = Double.parseDouble(sgpaInput[4]);
Sgpa[5] = Double.parseDouble(sgpaInput[5]);
Sgpa[6] = Double.parseDouble(sgpaInput[6]);
Sgpa[7] = Double.parseDouble(sgpaInput[7]);
crHr[0] = Double.parseDouble(crHrInput[0]);
crHr[1] = Double.parseDouble(crHrInput[1]);
crHr[2] = Double.parseDouble(crHrInput[2]);
crHr[3] = Double.parseDouble(crHrInput[3]);
crHr[4] = Double.parseDouble(crHrInput[4]);
crHr[5] = Double.parseDouble(crHrInput[5]);
crHr[6] = Double.parseDouble(crHrInput[6]);
crHr[7] = Double.parseDouble(crHrInput[7]);
for(int j=0; j<8; j++)
{
creditHoursTotalDegree=creditHoursTotalDegree +crHr[j];
//System.out.println(Sgpa[j]);
//System.out.println(crHr[j]);
temp = temp + (Sgpa[j] * crHr[j]);
//System.out.println(temp);
//JOptionPane.showMessageDialog(null,SGPA);
}
temp = temp/creditHoursTotalDegree;
CGPA =String.valueOf(temp);
textArea.setText(CGPA);
}
}
);
textArea = new JTextArea();
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblSemesterSgpa_7)
.addComponent(lblSemesterSgpa_6)
.addComponent(lblSemesterSgpa_5)
.addComponent(lblSemesterSgpa_4)
.addComponent(lblSemesterSgpa_3)
.addComponent(lblSemesterSgpa_2)
.addComponent(lblSemesterSgpa_1)
.addComponent(lblSemesterSgpa))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(textFieldGpa8)
.addComponent(textFieldGpa7)
.addComponent(textFieldGpa6)
.addComponent(textFieldGpa5)
.addComponent(textFieldGpa4)
.addComponent(textFieldGpa3)
.addComponent(textFieldGpa2)
.addComponent(textFieldGpa1))
.addGap(31)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(lblSemesterCredit)
.addComponent(lblSemesterCredit_1)
.addComponent(lblSemesterCredit_2)
.addComponent(lblSemesterCredit_3)
.addComponent(lblSemesterCredit_4)
.addComponent(lblSemesterCredit_5)
.addComponent(lblSemesterCredit_6)
.addComponent(lblSemesterCredit_7))
.addGap(5)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(textFieldcr3)
.addComponent(textFieldcr2, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(textFieldcr4, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldcr5, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldcr6, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldcr7, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldcr8, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldcr1)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(131)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(28)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 495, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(3)
.addComponent(lblSemesterCredit))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_2))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_3))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_4))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_5))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_6))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldGpa8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterSgpa_7)))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textFieldcr1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textFieldcr2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblSemesterCredit_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_2)
.addComponent(textFieldcr3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_3)
.addComponent(textFieldcr4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_4)
.addComponent(textFieldcr5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_5)
.addComponent(textFieldcr6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_6)
.addComponent(textFieldcr7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSemesterCredit_7)
.addComponent(textFieldcr8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
setVisible(true);
}
}
所有计算都在 temp 变量中执行,每次单击按钮时都会重新初始化,但它不起作用。请帮忙,因为它非常令人沮丧。