button1.setButton(false)不会与我的其他代码链接

黑客人

说明:嗨,我正在创建我的答题器游戏,但是我找不到为什么我的button1.setEnabled(false);。代码不会链接到我的JButton button1 = new JButton(“ H​​acker Level:”); 我正在使用两个按钮。第一个按钮启动计时器并计算点击次数/空格键的按下次数。一旦10秒钟用完,该按钮将自行禁用。第二个按钮重置点击次数并启用第一个按钮。

问题:因为我使用了两个不同的按钮,所以我必须使用JButton,它不会使其链接到button1.setEnabled(false);。码。如何链接?

当计时器用尽时,将button1设置为禁用屏幕截图button1屏幕截图这是问题所在的代码部分:

        public TestPane() {

        new GridLayout(0, 1); // 0 rows, 1 column

        timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (startTime < 0) {
                    startTime = System.currentTimeMillis();
                }
                long now = System.currentTimeMillis();
                long clockTime = now - startTime;
                if (clockTime >= duration) {
                    clockTime = duration;
                    timer.stop();

                    **button1.setEnabled(false);**<<<<<<<<<<<<<<<<<<<<

                }
                SimpleDateFormat df = new SimpleDateFormat("mm:ss.SSS");
                label.setText(df.format(duration - clockTime));
            }
        });
        timer.setInitialDelay(0);

        // \/ label wrapper \/ TITLE
        label = new JLabel("Hacker GUI v1", JLabel.CENTER); // label text
        label.setFont(new Font("Roboto", Font.PLAIN, 20)); // font size

        add(label); // add the label
        // /\ label wrapper /\

        // \/ label wrapper \/ DESCRIPTION
        label = new JLabel("To hack, click on the button or press space while on the window.", JLabel.CENTER); // text label
        label.setFont(new Font("Roboto", Font.PLAIN, 15)); // font size
        label.setHorizontalTextPosition(JLabel.CENTER);

        add(label); // add the label
        // /\ label wrapper /\

        // \/ button wrapper \/ BUTTON PLAY
        Dimension d = new Dimension(215, 30); // first number is length (left to right), second number is heigh (top to bottom).

        **JButton button1 = new JButton("Hacker Level: "); // button text**<<<<<<<<<<<<
        button1.setPreferredSize(d); // size of button relitive to dimension d
        button1.setFont(new Font("Roboto", Font.PLAIN, 15)); //font size of button
        button1.addActionListener( new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                count++; //count in positive integers
                button1.setText("Hacker Level: " + count); //text change when action preformed

                if (!timer.isRunning()) {
                    startTime = -1;
                    timer.start();
                }
            }
            
        });

        add(button); //add the button
        //     /\ button wrapper /\


        // \/ button wrapper \/ BUTTON RESET
        Dimension a = new Dimension(215, 30); // first number is length (left to right), second number is heigh (top to bottom).

        JButton button2 = new JButton("Reset Score"); // button text
        button2.setPreferredSize(a); // size of button relitive to dimension d
        button2.setFont(new Font("Roboto", Font.PLAIN, 15)); //font size of button
        button2.addActionListener( new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                count++; //count in positive integers
                button1.setText("Hacker Level: " + count); //text change when action preformed

                button1.setEnabled(true);

            }
            
        });

        add(button); //add the button
        //     /\ button wrapper /\
卡米克尔
JButton button1 = new JButton("Hacker Level: "); 

您正在定义一个局部变量。局部变量不能在其他方法中使用。

您需要将“ button1”定义为实例变量(定义标签的方式相同):

button1 = new JButton("Hacker Level: ");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

c# - 如何触发button1单击并在c#中执行更多代码?

如果单击button1,如何禁用button2;如果单击button1,如何禁用button2。

调用Button1在Unit2中单击Form1 / Unit1

我的样式表不会与我的html链接

如何检测是否在 If 语句中按下了 button1。网络

按下Button1的边缘时,有时无法打开MessageBox

单击Button1和Button2将根据Java中的顺序更改为多种颜色

Android应用程序在移动设备上单击button1时崩溃,但在模拟器中成功运行

如何使用html input button1 button2 button3 button4 button5显示div1 div2 div3 div4 div5

我的jQuery代码与我的链接冲突?

为什么我的元素不会与我的导航栏内联?

Docker应用程序容器不会与我的MySQL容器通信

1错误不会让我的代码编译

执行代码N次,其他代码N + 1次

如果其他的工作与我想要的完全相反

我在 Word Press 中的 css 代码适用于网站中的所有其他页面,而我只希望它适用于 1 个页面

如何自动调整散点图标签而不会与 python 中的其他标签重叠?

FFMPEG:生成的视频将不会与其他视频保持一致

Rex-Ray卷不会与其他EC2服务器共享

asp:Button-onclick事件会在其他隐藏代码后触发代码?

gcc 不会与 -m32 链接

为什么我代码的其他部分不会触发游戏的重置功能?

当其他提交表单的JQuery代码被调用时,jQuery $(window).load不会淡出(div)我的div

尝试将我的VS代码与其他GitHub存储库链接

withSyles HOC在通过React.forwardRef时不会与其他道具一起传递innerRef

反应:如果1状态为true,则将其他状态更改为false

努力与我的伪代码

它没有在其他if(i == 1)中执行代码,为什么会这样呢?

html源代码中的第一列在其他列下方显示1行