Java 使用Jframe/textfields打印数字

Java 使用Jframe/textfields打印数字,java,eclipse,swing,windowbuilder,Java,Eclipse,Swing,Windowbuilder,如何在eclipse中使用WindowBuilder将for循环中的数字打印到文本字段中,我已经为计数器创建了for循环,以便在单击start(计数高达60)时开始。我只是想知道,当我点击“开始”时,你如何让它显示在不同的文本字段中。 如果这个解释不是很清楚,换句话说,我正在做一个秒表,当你点击开始时,它会在eclipse控制台中打印到60。我希望在单击开始时,这些数字显示在窗口的JTextfield中。感谢您的帮助:) 这是代码页,我希望你们能帮助我:)(这就是我试图做的 package co

如何在eclipse中使用WindowBuilder将for循环中的数字打印到文本字段中,我已经为计数器创建了for循环,以便在单击start(计数高达60)时开始。我只是想知道,当我点击“开始”时,你如何让它显示在不同的文本字段中。 如果这个解释不是很清楚,换句话说,我正在做一个秒表,当你点击开始时,它会在eclipse控制台中打印到60。我希望在单击开始时,这些数字显示在窗口的JTextfield中。感谢您的帮助:)

这是代码页,我希望你们能帮助我:)(这就是我试图做的

package com.racecar484.user;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JTable;

public class StopWatch extends ForLoopTesting {

    private JFrame frmStopWatchPro;
    private JTextField txtClickMeTo;
    private JButton Terminate;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StopWatch window = new StopWatch();
                    window.frmStopWatchPro.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public StopWatch() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * @param i 
     */



    private void initialize(String i) {
        frmStopWatchPro = new JFrame();
        frmStopWatchPro.getContentPane().setBackground(new Color(255, 127, 80));
        frmStopWatchPro.setTitle("Stop Watch Pro");
        frmStopWatchPro.setBounds(100, 100, 450, 300);
        frmStopWatchPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmStopWatchPro.getContentPane().setLayout(null);

        txtClickMeTo = new JTextField();
        for(int i1 = 0; i1 < 60; i1++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

        txtClickMeTo.setText(i);
        txtClickMeTo.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            System.out.println("Oh my god this worked!");

            }
        });
        txtClickMeTo.setEditable(false);
        txtClickMeTo.setBounds(19, 24, 300, 58);
        frmStopWatchPro.getContentPane().add(txtClickMeTo);
        txtClickMeTo.setColumns(10);

        JButton btnNewButton = new JButton("Start");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Stop-Watch activated.");
                for(int i = 0; i < 60; i++){
                    System.out.println(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        });
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnNewButton.setBounds(53, 121, 108, 40);
        frmStopWatchPro.getContentPane().add(btnNewButton);

        JButton btnStop = new JButton("Stop");
        btnStop.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            String Meow = "hey";
                System.out.println("Stop-Watch stopped.");
            }
        });
        btnStop.setBounds(211, 121, 108, 40);
        frmStopWatchPro.getContentPane().add(btnStop);

        Terminate = new JButton("Terminate");
        Terminate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            frmStopWatchPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                System.out.println("Closing Application.");
            System.exit(0);
            }
        });
        Terminate.setBounds(329, 0, 105, 261);
        frmStopWatchPro.getContentPane().add(Terminate);

        JLabel lblonlyOneThat = new JLabel("(Only one that actually works without console ->)");
        lblonlyOneThat.setBounds(53, 211, 266, 39);
        frmStopWatchPro.getContentPane().add(lblonlyOneThat);

        JLabel lblStopWatchPro = new JLabel("Stop Watch Pro V.1- made by Andrew Lopez ");
        lblStopWatchPro.setBounds(53, 187, 257, 29);
        frmStopWatchPro.getContentPane().add(lblStopWatchPro);
    }
}
package com.racecar484.user;
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JTextField;
导入javax.swing.JButton;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入javax.swing.SwingConstants;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.Color;
导入javax.swing.JLabel;
导入javax.swing.JTable;
公共类秒表扩展为循环测试{
私人JFrame frmStopWatchPro;
私有JTextField txtClickMeTo;
私有按钮终止;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
秒表窗口=新秒表();
window.frmStopWatchPro.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公众秒表{
初始化();
}
/**
*初始化框架的内容。
*@param i
*/
私有void初始化(字符串i){
frmStopWatchPro=新JFrame();
frmStopWatchPro.getContentPane().setBackground(新颜色(255、127、80));
frmStopWatchPro.setTitle(“秒表专业版”);
frmStopWatchPro.setBounds(100100450300);
frmStopWatchPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmStopWatchPro.getContentPane().setLayout(null);
txtClickMeTo=newjtextfield();
对于(inti1=0;i1<60;i1++){
试一试{
睡眠(1000);
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
txtClickMeTo.setText(i);
txtClickMeTo.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效鼠标单击(鼠标事件arg0){
System.out.println(“哦,我的上帝,这成功了!”);
}
});
txtClickMeTo.setEditable(false);
txtClickMeTo.setBounds(19,24,300,58);
frmStopWatchPro.getContentPane().add(txtClickMeTo);
txtClickMeTo.setColumns(10);
JButton btnNewButton=新JButton(“开始”);
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
System.out.println(“秒表激活”);
对于(int i=0;i<60;i++){
系统输出打印LN(i);
试一试{
睡眠(1000);
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
}
});
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
}
});
btnNewButton.setBounds(53,121,108,40);
frmStopWatchPro.getContentPane().add(btnNewButton);
JButton btnStop=新JButton(“停止”);
btnStop.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
String Meow=“嘿”;
System.out.println(“秒表停止”);
}
});
btnStop.setBounds(211、121、108、40);
frmStopWatchPro.getContentPane().add(btnStop);
终止=新按钮(“终止”);
Terminate.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
frmStopWatchPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(“关闭应用程序”);
系统出口(0);
}
});
终止。立根(329,0,105,261);
frmStopWatchPro.getContentPane().add(终止);
JLabel lblonlyOneThat=新的JLabel(“(只有一个在没有控制台的情况下实际工作->)”;
立根(5321126639);
frmStopWatchPro.getContentPane().add(lblonlyOneThat);
JLabel lblStopWatchPro=新JLabel(“秒表专业版1-安德鲁·洛佩兹制作”);
lblStopWatchPro.setBounds(5318725729);
frmStopWatchPro.getContentPane().add(lblStopWatchPro);
}
}

在我看来,您对Java swing框架的理解有点有限。您有两个主要问题。其中一个问题您已经在问题中描述过了。另一个问题是您不知道如何制作计时器

目前,您正在使用
Thread.sleep(1000)
来“等待”1秒。但是您将其放在
initialize
方法中。为什么要在窗口初始化时等待60秒?这对我来说毫无意义

我看到您有另一个
线程。单击“开始”按钮的侦听器中有sleep(1000)
。这是有道理的,但您没有将
txtClickMeTo设置为

应该是这样的:

for(int i = 0; i < 60; i++){
    System.out.println(i);
    try {
        Thread.sleep(1000);
        txtClickMeTo.setText(Integer.toString(i));
     } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
这样,“开始”按钮就可以正常工作

但是停止按钮呢?为什么它不工作
final JTextField txtClickMeTo = new JTextField();
Timer t = new Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // blah blah blah
    }
});
int secondsElapsed = 0;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class StopWatch {

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

    public StopWatch() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField field;
        private Timer timer;
        private int counter;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            field = new JTextField(4);
            add(field, gbc);

            JButton btn = new JButton("Start");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (timer.isRunning()) {
                        timer.stop();
                        btn.setText("Start");
                    } else {
                        counter = 0;
                        timer.start();
                        field.setText(Integer.toString(counter));
                        btn.setText("Stop");
                    }
                }
            });
            add(btn, gbc);

            timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    counter++;
                    if (counter >= 60) {
                        timer.stop();
                        btn.setText("Start");
                    }
                    field.setText(Integer.toString(counter));
                }
            });

        }

    }

}