嗨,我仍然是一名学习 Java 的学生。我应该做这个计算器,但我有一个错误,我不知道如何修复它。
这是我的代码:
**import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Task1 extends JPanel implements ActionListener
{
JLabel label1,label2;
JTextField textField1,textField2;
JButton button1,button2,button3,button4,button5,button6;
double addResult=0;
double subResult=0;
double mulResult=0;
double divResult=0;
public void Task1()
{
setLayout(new GridLayout(5,2,5,5));
label1=new JLabel("First Value: ");
add(label1);
textField1=new JTextField(10);
add(textField1);
label2=new JLabel("Second Value");
add(label2);
textField2=new JTextField(10);
add(textField2);
button1=new JButton("ADD");
add(button1);
button1.setMnemonic(KeyEvent.VK_A);
button1.addActionListener(this);
button2=new JButton("SUBSTRACT");
add(button2);
button2.addActionListener(this);
button3=new JButton("MULTIPLY");
add(button3);
button3.addActionListener(this);
button4=new JButton("DIVIDE");
add(button4);
button4.addActionListener(this);
button5=new JButton("COMPUTE");
add(button5);
button5.addActionListener(this);
button6=new JButton("RESET");
add(button6);
button6.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent evt)
{
try{
if(evt.getSource()==button1)
{
String text=textField1.getText();
String text2=textField2.getText();
if(text.equals("")||text2.equals(""))
{
JOptionPane.showMessageDialog(null,"Please Enter Values");
}
addResult=addResult+Integer.parseInt(text)+Integer.parseInt(text2);
}
if(evt.getSource()==button2)
{
String text=textField1.getText();
String text2=textField2.getText();
if(text.equals("")||text2.equals(""))
{
JOptionPane.showMessageDialog(null,"Please Enter Values");
}
subResult=subResult+(Integer.parseInt(text)-Integer.parseInt(text2));
}
if(evt.getSource()==button3)
{
String text=textField1.getText();
String text2=textField2.getText();
if(text.equals("")||text2.equals(""))
{
JOptionPane.showMessageDialog(null,"Please Enter Values");
}
mulResult=mulResult+(Integer.parseInt(text)*Integer.parseInt(text2));
}
if(evt.getSource()==button4)
{
String text=textField1.getText();
String text2=textField2.getText();
if(text.equals("")||text2.equals(""))
{
JOptionPane.showMessageDialog(null,"Please Enter Values");
}
divResult=divResult+(Integer.parseInt(text)/Integer.parseInt(text2));
}
if(evt.getSource()==button5)
{
JOptionPane.showMessageDialog(null,"Result: "+
(addResult+subResult+mulResult+divResult));
}
if(evt.getSource()==button6)
{
textField1.setText("");
textField2.setText("");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Exception: "+e.getMessage());
}
}
}**
这是错误:
错误:在 Task1 类中找不到主方法,请将主方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application codio@border-florida:~/workspace/swing $