1

我试图让我的程序启动一个在实际程序启动之前收集信息的 gui。在 main 中,我尝试调用 JFrame,它应该运行,直到按下开始按钮,然后主程序应该启动。除了 initializeLauncher 的基类之外,一切似乎都是正确的。谢谢!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class InitializeLauncher implements ActionListener {

    InitializeLauncher() {
        JFrame frame = new JFrame("launcherClient");

        Container c = frame.getContentPane();
        Dimension d = new Dimension(700,400);
        c.setPreferredSize(d);

        JButton startButton = new JButton("Start");
        JPanel pane = new JPanel();

        startButton.addActionListener(this);

        pane.add(startButton);
        frame.add(pane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public void buttonClicked(ActionEvent e)
    {
        ApplicationDeploy displayExample = new ApplicationDeploy();
        displayExample.initializeGameClient();
    }
}

...然后主要我称之为:

InitializeLauncher launcher = new InitializeLauncher();
launcher.InitializeLauncher();
4

1 回答 1

3

通过使你的类抽象,你正在修复错误的东西。相反,您应该为您的班级提供缺少的方法,public void actionPerformed(ActionEvent e) {...}

这里的基本规则是,如果你声明你的类要实现一个接口,这里是 ActionListener 接口,那么这个类必须实现接口的所有方法。

@Override
public void actionPerformed(ActionEvent e) {
   // ... your code that should occur when the button is pressed goes here
}

请注意,您的buttonClicked(...)方法对您没有任何用处。您可能希望摆脱该方法并将其代码放入 actionPerformed 方法中。

顺便说一句,我经常将 JOptionPane 用于您使用 JFrame 的功能。

于 2013-10-27T19:05:39.157 回答