如何解决构造函数上的错误?
How to resolve an Error on a constructor?
不知道为什么写:
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)
,因为 class 不扩展任何其他 class,因此您不能调用任何superclass.
的构造函数
构造函数Time(Build, String)
接受一个Build
实例作为第一个参数,所以在CalculAction
中实例化Time变量时不能传递this
,因为它会引用 CalculAction class 的实例,但您必须通过这种方式传递实例成员 fenetre
:
Time Time = new Time(fenetre, "Time");
不知道为什么写:
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)
,因为 class 不扩展任何其他 class,因此您不能调用任何superclass.构造函数
Time(Build, String)
接受一个Build
实例作为第一个参数,所以在CalculAction
中实例化Time变量时不能传递this
,因为它会引用 CalculAction class 的实例,但您必须通过这种方式传递实例成员fenetre
:Time Time = new Time(fenetre, "Time");