如何在GridLayout中更新JTextFields? [英] How to update JTextFields in a GridLayout?
问题描述
我有一个使用Gridlayout
的MainPanel
.因此,我分别为NORTH,EAST,CENTER和EAST布局创建了四个JPanel
类.然后,将所有四个添加到我的MainPanel
.
I have a MainPanel
which uses the Gridlayout
. Consequently I have created four JPanel
classes for the: NORTH, EAST, CENTER and EAST layouts respectively. I then add all four to my MainPanel
.
但是,在WEST面板上,我使用了另一个网格布局来存储JButtons
和JTextFields
.我要不断更新我的JTextFields
,因为它们显示一个值(单击另一个面板上的按钮时会更改). JFrame
运行时如何允许更改该值?
However, on my WEST panel I use another grid layout to store JButtons
and JTextFields
. I want to constantly update my JTextFields
as they display a value (that changes when a button on another panel is clicked). How do I allow that value to be changed when the JFrame
is running?
我尝试使用paintComponent
,但是随着我在paintComponent
方法中添加相同副本,它会继续添加同一副本JTextField
的多个副本.如果我删除add方法,则值将不会更新.
I tried using paintComponent
, but it keeps on adding multiple copies of the same JTextField
after each other, as I add it in my paintComponent
method. If I remove the add method the values won't update.
推荐答案
Action
很好地封装了此类功能.在下面的示例中,许多文本字段监听从单个 Update 按钮接收到的ActionEvent
.常见的UpdateHandler
源自AbstractAction
.
Action
works well to encapsulate such functionality. In the example below, a number of text fields listen for an ActionEvent
received from a single Update button. The common UpdateHandler
is derived from AbstractAction
.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** @see http://stackoverflow.com/a/14947144/230513 */
public class Test {
private JButton button = new JButton("Update");
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createPanel(button), BorderLayout.NORTH);
f.add(createPanel(button), BorderLayout.WEST);
f.add(createPanel(button), BorderLayout.EAST);
f.add(createPanel(button), BorderLayout.SOUTH);
JPanel p = new JPanel();
p.add(button);
f.add(p, BorderLayout.CENTER);
f.getRootPane().setDefaultButton(button);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static JPanel createPanel(JButton b) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
final JTextField text = new JTextField();
b.addActionListener(new UpdateHandler(text));
panel.add(text);
return panel;
}
private static class UpdateHandler extends AbstractAction {
private JTextField text;
private DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
public UpdateHandler(JTextField t) {
super("update");
t.setText(df.format(new Date()));
this.text = t;
}
@Override
public void actionPerformed(ActionEvent e) {
text.setText(df.format(new Date()));
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}
这篇关于如何在GridLayout中更新JTextFields?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!