微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将整个变量替换为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 举报,一经查实,本站将立刻删除。