【问题标题】:How to resolve an Error on a constructor? [closed]如何解决构造函数上的错误? [关闭]
【发布时间】: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");,其中thisCalculAction 正在尝试调用不存在的Time 构造函数。
  • “我正在做一个小时。”edit问题添加该信息。它会比在 cmets 中更明显。

标签: java swing constructor awt


【解决方案1】:

我不确定您要达到什么目的,但关于您的编译问题,我注意到您的代码中有两个错误:

  1. Time构造函数中删除调用super(texte),因为该类不扩展任何其他类,因此您不能调用超类的任何构造函数。

  2. 构造函数Time(Build, String)接受Build实例作为第一个参数,所以在CalculAction内部实例化Time变量时不能传递this,因为它会引用CalculAction类的实例,但你必须传递实例成员fenetre,这样:

    时间 Time = new Time(fenetre, "Time");

【讨论】:

  • 好的我试试看
  • 我自己试了一下,代码是这样编译的。
  • 感谢它的工作
  • 不客气!
猜你喜欢
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
相关资源
最近更新 更多