[英]How to update JFrame in another class?
I want to update an JLabel of created Control outside constructor or from another class. 我想在构造函数外部或从另一个类更新创建的Control的JLabel。 Here I created a Control and add some components in Constructor. 在这里,我创建了一个控件,并在构造函数中添加了一些组件。 The UI is created in main, but I want to update some label outside this class. UI是在main中创建的,但是我想在此类之外更新一些标签。 So I create setResult(final String text, final String path) to do such a thing, but it is not working.... 所以我创建setResult(final String text,final String path)来做这样的事情,但是它不起作用。
public class Control extends JFrame
{
private static Control control;
static HashMap<String, JLabel> results = new HashMap<String, JLabel>();
public Control(ArrayList<TestCase> tests)
{
super("WFC Tests");
setLayout(new GridLayout(tests.size() + 1, 6));
start.addActionListener(bl);
JComboBox comBox = new JComboBox(testCategoties);
loopsField.setText("1");
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout());
jp.add(comBox);
jp.add(loops);
jp.add(loopsField);
jp.add(mainDevice);
jp.add(mainDeviceText);
jp.add(refDevice1);
jp.add(refDeviceText1);
jp.add(refDevice2);
jp.add(refDeviceText2);
jp.add(start);
add(jp);
for (TestCase test : tests)
{
add(createPane(test));
}
}
private JPanel createPane(TestCase test)
{
JPanel jp = new JPanel();
test.getIterationTextField().setText("0");
test.getCallTextField().setText("0");
test.getHoldTextField().setText("0");
jp.setLayout(new FlowLayout(FlowLayout.LEFT));
test.getTestCheckBox().setPreferredSize(new Dimension(200, 20));
jp.add(test.getTestCheckBox(), (int) JCheckBoxMenuItem.CENTER);
jp.add(test.getLogCheckBox(), JCheckBoxMenuItem.CENTER_ALIGNMENT);
jp.add(test.getIterationLabel());
jp.add(test.getIterationTextField());
if (test.getName().contains("Call"))
{
jp.add(test.getCallLabel());
jp.add(test.getCallTextField());
}
if (test.getName().contains("Hold"))
{
jp.add(test.getHoldLabel());
jp.add(test.getHoldTextField());
}
test.getResultLabel().setBackground(Color.LIGHT_GRAY);
test.getResultLabel().setOpaque(true);
jp.add(test.getResultLabel(), JPanel.RIGHT_ALIGNMENT);
results.put(test.getPath(), test.getResultLabel());
return jp;
}
public static void run(final JFrame f, final int width, final int height)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
public static void main(String[] args)
{
testCases = SanityTest.getSanityTestCases();
control = new Control(testCases);
run(control, 1000, 800);
}
My issue is setResult is not working. 我的问题是setResult无法正常工作。 I am not sure why. 我不知道为什么。 Can anybody help? 有人可以帮忙吗? Thanks! 谢谢!
public void setResult(final String text, final String path)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
results.get(path).setText(text);
}
});
control.getContentPane().validate();
}
}
I think the problem is at the control.getContentPane().validate();
我认为问题出在control.getContentPane().validate();
Try adding control.repaint();
尝试添加control.repaint();
instead. 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.