【发布时间】:2021-06-14 05:51:25
【问题描述】:
我不知道为什么会这样写:
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);
}
}
}
【问题讨论】:
-
我正在做一个时钟。
-
您没有将
Time声明为扩展另一个类,那么super(texte);在Time构造函数中应该做什么? -
我认为您使用了错误的 Time 课程,而不是您期望的课程。例如,您认为您正在实例化您实现的 Time 类,而(可能)您正在使用 java.sql.Time。您能否将所有“import”语句也添加到上面的代码中?
-
还有
new Time(this, "Time");,其中this是CalculAction正在尝试调用不存在的Time构造函数。 -
“我正在做一个小时。”请edit问题添加该信息。它会比在 cmets 中更明显。
标签: java swing constructor awt