当前位置: 首页 > 知识库问答 >
问题:

单击按钮后Java setText未更新

陈野
2023-03-14

我正在做一个计数计时器使用按钮开始和停止。

我需要帮助解决这个...当我按下“Start”按钮时,它可以工作(使用system.out.print)但是settext不会为我更新。

但是,如果将t.start();放在frame.setvisible(true)之后;而不是在start=1之后;settext会更新...但我没有按“开始”键。如何使setText在“开始”按钮被按下后更新?

public class Cook implements ActionListener{

    private enum Actions {
        Start, Stop, Setting, Exit
      }

    JLabel minsleft;
    int start = 0, counter, stop = 0, close = 0, whileloop = 0;

     JFrame frame = new JFrame("SMART STOVE");
    public void Cook() {

        Cook instance = new Cook();
        // TODO Auto-generated method stub
         JLabel minsleft = new JLabel();
         minsleft.setFont(new Font("Serif", Font.BOLD, 17));
         minsleft.setBounds(100,300,250,40);

         try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src\\cook.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

            JButton Start = new JButton("Start");
            Start.setFont(new Font("monospaced", Font.BOLD, 30));
            Start.setActionCommand(Actions.Start.name());
            Start.addActionListener(instance);          
            Start.setBounds(150, 180, 200, 100);
            frame.add(Start);

            JButton Stop = new JButton("Stop");
            Stop.setFont(new Font("monospaced", Font.BOLD, 30));
            Stop.setActionCommand(Actions.Stop.name());
            Stop.addActionListener(instance);           
            Stop.setBounds(360, 180, 200, 100);

            frame.add(Stop);
            countdownLabel.setBounds(360, 250, 200, 100);
            frame.add(countdownLabel);

            frame.setLayout(null);
            frame.setSize(700, 500);
            frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        if (evt.getActionCommand() == Actions.Start.name()) {
            start = 1;
            t.start();
          }

        if (evt.getActionCommand() == Actions.Stop.name()) {
            close = 1;
          }
        System.out.println("Done");
    }

    final JLabel countdownLabel = new JLabel("Waiting. . . ");    
    public Timer t = new Timer(1000, new ActionListener() {
        int time = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            time++;
            countdownLabel.setText(format(time / 60) + ":" + format(time % 60));
            System.out.println(format(time / 60) + ":" + format(time % 60));
            if (close == 1) {
                final Timer timer = (Timer) e.getSource();
                timer.stop();
                System.out.println("Stopped");
            }
        }
    });

    private static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

}

共有1个答案

殳越
2023-03-14

我想知道如何运行代码,而没有main。所以我加了一个

public static void main(String[] args) {
    new Cook().Cook();
}

然后我想知道为什么要在Cook方法中创建新的Cook实例(对于Java来说,命名非常糟糕)。

Cook instance = new Cook();

所以我删除了它,并将start.addActionListener(instance);更改为start.addActionListener(this);,它现在正在工作...

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class Cook implements ActionListener{

    private enum Actions {
        Start, Stop, Setting, Exit
      }

    JLabel minsleft;
    int start = 0, counter, stop = 0, close = 0, whileloop = 0;

     JFrame frame = new JFrame("SMART STOVE");
    public void Cook() {

//        Cook instance = new Cook();
        // TODO Auto-generated method stub
         JLabel minsleft = new JLabel();
         minsleft.setFont(new Font("Serif", Font.BOLD, 17));
         minsleft.setBounds(100,300,250,40);

         try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src\\cook.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

            JButton Start = new JButton("Start");
            Start.setFont(new Font("monospaced", Font.BOLD, 30));
            Start.setActionCommand(Actions.Start.name());
            Start.addActionListener(this);          
            Start.setBounds(150, 180, 200, 100);
            frame.add(Start);

            JButton Stop = new JButton("Stop");
            Stop.setFont(new Font("monospaced", Font.BOLD, 30));
            Stop.setActionCommand(Actions.Stop.name());
            Stop.addActionListener(this);           
            Stop.setBounds(360, 180, 200, 100);

            frame.add(Stop);
            countdownLabel.setBounds(360, 250, 200, 100);
            frame.add(countdownLabel);

            frame.setLayout(null);
            frame.setSize(700, 500);
            frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        if (evt.getActionCommand() == Actions.Start.name()) {
            start = 1;
            t.start();
          }

        if (evt.getActionCommand() == Actions.Stop.name()) {
            close = 1;
          }
        System.out.println("Done");
    }

    final JLabel countdownLabel = new JLabel("Waiting. . . ");    
    public Timer t = new Timer(1000, new ActionListener() {
        int time = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            time++;
            countdownLabel.setText(format(time / 60) + ":" + format(time % 60));
            System.out.println(format(time / 60) + ":" + format(time % 60));
            if (close == 1) {
                final Timer timer = (Timer) e.getSource();
                timer.stop();
                System.out.println("Stopped");
            }
        }
    });

    private static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

    public static void main(String[] args) {
        new Cook().Cook();
    }

}
 类似资料:
  • 问题内容: 我正在使用此处找到的自定义表格模型。我已经使用该帖子中提供的建议更新了我的代码,并遇到了一个新问题。我对我的代码所做的改变是一个注入到我,以避免与线程的问题。完成此操作后,通过按按钮更新我的表的代码已停止工作。 用于初始化的代码如下: 我用来更新的代码如下: 我也尝试过使用,但这也不起作用。截至目前,更新的唯一方法是关闭并重新打开程序。具有我正在使用的,随着用户添加更多玩家,尺寸会增加

  • 编辑:底部的解决方案 这是一个跳棋游戏。单击一个按钮后,它等待单击第二个按钮与之交换。然而,有时你可能不想移动那个按钮,但一旦你点击了它,就没有回头路了,因为我无法禁用它。 在这里的其他帖子中,我看到人们使用 这只是使它在第一次单击后不可见。 这什么都干不了。 这也没什么用。编辑:所有这些方法都用true和false进行了尝试。 私有无效交换(){ 但你也需要 这样它就重新启用了它,或者其他什么,

  • 我已经检查了这个链接,但它提到了长点击。但是我在点击自定义对话框的按钮后面临这个问题。我已经把我的代码贴在这里了。有人能帮我避免这个错误吗?

  • 我需要一些android编程的帮助。所以onbutton click我在icite方法中调用。基本上,当单击按钮时,所有文本框文本都将转换为一个新变量上的字符串。之后,我将所有变量组合成一个名为total的字符串。然后将标签textView10的文本更改为字符串total。然而,它只是崩溃了我的应用程序。你知道我可能做错了什么吗?我的猫: 代码

  • 我正试图抓取一个页面,获取一盘国际象棋的移动列表,该列表位于右侧菜单的“移动”选项卡下。 在浏览器中手动单击“移动”选项卡时,我可以通过 哪个(正确地)返回 当尝试通过单击按钮时,使用 似乎什么都没有发生,我不知道如何进行故障排除。 如何通过模拟单击(活动事件侦听器)切换到“移动”选项卡? 额外提示:是否可以使用rvest软件包?

  • 问题内容: 我正在尝试在www.meetme.com上发送消息,但不知道该怎么做。我可以在评论区域中键入消息,但是单击“发送”按钮不会执行任何操作。我究竟做错了什么?当我登录并按登录按钮时,页面确实发生了变化,一切都很好。有人有任何想法或线索吗? 问题答案: 如果不了解正在访问的网页,就无法在禁用JavaScript的情况下执行AJAX请求。如果更改未成功,则必须继续进行调试,但请确保已启用Jav