0

if ステートメントでは、launchBtn が見つかりません。私はおそらくばかげて明白なことをしています。誰が何が悪いのか見ることができますか?エラーは太字で表示されています (または 2 つの ** で強調表示されています。これが私のコードです:

package launcher;

import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
class Window extends JFrame implements ActionListener
{
JPanel panel = new JPanel();

    public Window()
    {
    //Creates the blank panel
    super("Launcher");
    setSize(500, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(panel);
    setVisible(true);

    //Create the button variables
    JButton launchBtn = new JButton("Launch Game");
    JButton optionsBtn = new JButton("Launcher Options");

    //Add the buttons to the launcher
    panel.add(launchBtn);
    panel.add(optionsBtn);

    //Add the buttons to the action listener
    launchBtn.addActionListener(this);
    optionsBtn.addActionListener(this);
}

public void actionPerformed(ActionEvent event) 
{
    if(event.getSource() == **launchBtn**)
    {
        **launchBtn**.setEnabled(true);
    }
}
}
4

2 に答える 2

1

おそらく、コンストラクターで宣言されたローカル変数ではなく、このクラスのインスタンス変数になりたいlaunchBtnと思っていたでしょう。optionsBtnそれらの宣言をコンストラクタの外に移動します。

于 2013-09-29T09:22:54.730 に答える
1

launchBtnWindowコンストラクターのコンテキストでローカル変数として宣言されています。コンストラクターのスコープ外では意味がありません。

public Window()
{
    //...
    //Create the button variables
    JButton launchBtn = new JButton("Launch Game");

コンストラクタの外で変数にアクセスしたい場合は、クラスのインスタンス変数を作成する必要があります...

private JButton launchBtn;
public Window()
{
    //...
    //Create the button variables
    launchBtn = new JButton("Launch Game");

これにより、Windowクラスの他のメソッドが変数を参照できるようになります

于 2013-09-29T09:23:45.347 に答える