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);
}
}
}
解决方案
我不确定您要达到什么目标,但是关于您的编译问题,我注意到您的代码中有两个错误:
super(texte)
从构造函数中删除调用Time
,因为该类不扩展任何其他类,因此您不能调用超类的任何构造函数。构造函数
Time(Build, String)
接受一个实例作为第一个参数,因此在实例化 Time 变量时Build
不能传递,因为它会引用 CalculAction 类的实例,但您必须传递实例成员,通过这种方式:this
CalculAction
fenetre
Time Time = new Time(fenetre, "Time");
推荐阅读
- xamarin.forms - 当我使用适用于 Android 的 FormsAppCompatActivity 时,使用主详细信息页面进行探测
- spring - 没有为请求类型 [request.model.RTPRequest] 和内容类型 [application/x-java-serialized-object] 找到合适的 HttpMessageConverter
- javascript - 大型 SQL OR 语句或代码中的过滤器?
- javascript - 通过 JavaScript 设置文件输入的值会触发一个看似无关的事件
- javascript - 什么语言/技术使用语法 <%VARIABLE%> 以及它是如何使用的?
- java - 如何在知道其包名称的情况下获取已安装应用程序的请求权限?
- python-3.x - 微调的 VGG-16 对所有测试图像给出完全相同的预测
- node.js - Google Cloud Function/NodeJS - 从 JSON 表单事件解析 int
- java - 带有 MySQL 的 JPA 返回错误的最后插入 id
- python - 熊猫将一帧中的值分配给选定行中另一帧中的对应列