java如何解决构造函数上的错误?
我不知道为什么它会这样写:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The constructor Time(CalculAction, String) is undefine2
这是我的代码:
public class Build extends JFrame{
private JTextField field1;
private JLabel label;
private JComboBox liste;
public Build(){
super();
build();
}
private void build(){
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Sablier");
menuBar.add(menu1);
menuBar.setBackground(Color.GRAY);
setJMenuBar(menuBar);
setTitle("Sablier");
setSize(400,200);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(buildContentPane());
}
private JPanel buildContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
field1 = new JTextField();
field1.setColumns(10);
panel.add(field1);
JButton bouton = new JButton(new CalculAction(this, "Go"));
panel.add(bouton);
label = new JLabel("Il reste : " + "la variable" + " sec");
panel.setBackground(Color.magenta);
panel.add(label);
return panel;
}
public JTextField getField1(){
return field1;
}
public JLabel getLabel(){
return label;
}
}
public class CalculAction extends AbstractAction {
private Build fenetre;
public CalculAction(Build fenetre, String texte){
super(texte);
this.fenetre = fenetre;
}
public void actionPerformed(ActionEvent e) {
String nombre1String = fenetre.getField1().getText();
double nombre1 = Double.parseDouble(nombre1String);
if (nombre1 >= 6000){
fenetre.getLabel().setText("Error://incorect data");
}
else if (nombre1 <= 0){
fenetre.getLabel().setText("Error://incorect data");
}
else{
Time Time = new Time(this, "Time");
}
}
}
public class Time {
private Build fenetre;
public Time(Build fenetre, String texte){
super(texte);
this.fenetre = fenetre;
}
public Time(){
String nombre1String = fenetre.getField1().getText();
double nombre1 = Double.parseDouble(nombre1String);
double Time = 0;
for(int i = 0; i <= nombre1; i++){
fenetre.getLabel().setText("Le temps passé est " + Time + " sec");
Time++;
if (Time == nombre1){
fenetre.getLabel().setText("ALERTE!");
Toolkit.getDefaultToolkit().beep();
TimeUnit.MILLISECONDS.sleep(100);
Toolkit.getDefaultToolkit().beep();
TimeUnit.MILLISECONDS.sleep(100);
}
TimeUnit.SECONDS.sleep(1);
}
}
}
# 1 楼答案
我不确定您想要实现什么,但关于编译问题,我注意到您的代码中有两个错误:
从
Time
构造函数中删除调用super(texte)
,因为该类不扩展任何其他类,所以不能调用超类的任何构造函数构造函数
Time(Build, String)
接受一个Build
实例作为第一个参数,因此在CalculAction
内实例化时间变量时,不能传递this
,因为它将引用Calculation类的实例,但必须通过以下方式传递实例成员fenetre
:时间=新时间(fenetre,“时间”)