gpt4 book ai didi

java - 使用 Jframe/文本字段打印数字

转载 作者:行者123 更新时间:2023-12-02 03:58:26 25 4
gpt4 key购买 nike

如何使用 Eclipse 中的 WindowBuilder 将 for 循环中的数字打印到文本字段中,我已经制作了 for 循环,以便在单击开始时开始计数器(最多计数 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);
}
}

最佳答案

Swing 是单线程的,这意味着如果您执行任何阻塞事件调度线程的操作,例如运行循环、使用 Thread.sleep 或其他一些长时间运行的进程,则 UI 将不会已更新(并且不会处理任何新事件)。

Swing 也不是线程安全的,这意味着您永远不应该从 EDT 之外的任何线程更新 UI

首先查看 Concurrency in Swing了解更多详情。

那么问题来了,如何解决这个问题呢?您可以使用 ThreadSwingUtilities.invokeLater,但这有点困惑。您可以使用 SwingWorker,但这对于解决问题来说有点过于严厉。最好的解决方案是使用 Swing Timer,它会在 EDT 内定期调用已注册的回调 (ActionListener),从而可以安全地从内部更新 UI .

看看How to use Swing Timers了解更多详细信息。

举个例子......

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));
}
});

}

}

}

Cavet:这是一个简单的示例。 Swing Timer 仅保证它会在规定的延迟后调用 ActionListener,这使得它稍微不准确(以毫秒为单位)。更“合适”的解决方案是比较 Timer 启动时间和 ActionListener 收到通知的时间

您可能还想查看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners用于使用按钮的更合适的机制

关于java - 使用 Jframe/文本字段打印数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35192782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com