如何将整个变量替换为public void actionPerformedActionEvent e{}?

问题描述

我遇到一个问题,一旦我关闭了私有静态void,它就会关闭我的所有变量。这是一个问题,因为在我的公共空白actionPerformed(ActionEvent e)中,除了Label label1之外我没有任何变量。有谁知道该怎么办?

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame implements ActionListener
{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) 
    {
        new Main().setVisible(true);
    }
    private  Main() 
    {
        super("The Mathematic Game Of IQ");
        setSize(1366,768);
        setResizable(false);
        setDefaultCloSEOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        JButton StartButton = new JButton("Start Game");
        JButton HelpButton = new JButton("Help");
        StartButton.setBackground(Color.orange);
        HelpButton.setBackground(Color.yellow);
        add(StartButton);
        add(HelpButton);
        StartButton.addActionListener(this);
        HelpButton.addActionListener(this);

    }
    public void actionPerformed(ActionEvent e) 
    {

        JLabel label1 = new JLabel("Test");

        String ButtonStarter = e.getActionCommand();


        if(ButtonStarter.equals("Start Game")) 
        {
            
            label1.setText("Start in 3..");
            add(label1);
            try 
            {
                java.util.concurrent.TimeUnit.SECONDS.sleep((long) 1);
            } 
            catch (InterruptedException e1) {
                e1.printstacktrace();
            }
            label1.setText("Start in 2..");
            add(label1);
            try 
            {
                java.util.concurrent.TimeUnit.SECONDS.sleep((long) 1);
            } 
            catch (InterruptedException e1) {
                e1.printstacktrace();
            }
            label1.setText("Start in 1..");
            add(label1);
            try 
            {
                java.util.concurrent.TimeUnit.SECONDS.sleep((long) 1);
            } 
            catch (InterruptedException e1) {
                e1.printstacktrace();
            }
            label1.setText("Go!");
            add(label1);
        }
        else if(ButtonStarter.equals("Help"))
        {
            label1.setText("It's Just a mathematical game,if you\\nkNow the answer type it,if you don't then guess.");
            add(label1);

        }
    
    }

    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)