Java Swing UI元素不总是更新
我试图为一些只有CLI界面的工具制作一个简单的GUI。我想,拥有一个带有多个标签的窗口将是实现这一点的最佳方式。我遇到的问题是,在填写信息/做出选择时,一些UI元素没有被更新。特别是组合框一直是最大的痛苦 它显示所有选项,我可以选择不同的选项,但在选择某个选项后,GUI并不总是更新组合框以显示新的选择。例如,它以选择1开始,但我将其更改为3,事件触发器会看到选择了3,但组合框仍然显示1 我曾尝试在框架选项卡甚至组合框本身中添加重新绘制和验证,但没有成功地强制更新。任何帮助都将不胜感激 值得注意的是,我的一些文本字段也会出现这种情况,这些文本字段在从文件选择器中选择文件/文件夹后显示文件路径,但并不经常出现Java Swing UI元素不总是更新,java,swing,combobox,Java,Swing,Combobox,我试图为一些只有CLI界面的工具制作一个简单的GUI。我想,拥有一个带有多个标签的窗口将是实现这一点的最佳方式。我遇到的问题是,在填写信息/做出选择时,一些UI元素没有被更新。特别是组合框一直是最大的痛苦 它显示所有选项,我可以选择不同的选项,但在选择某个选项后,GUI并不总是更新组合框以显示新的选择。例如,它以选择1开始,但我将其更改为3,事件触发器会看到选择了3,但组合框仍然显示1 我曾尝试在框架选项卡甚至组合框本身中添加重新绘制和验证,但没有成功地强制更新。任何帮助都将不胜感激 值得注意的
package com.test.JavaSwing;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
public class TestSwing_Main {
/**
* start here
* @param args
*/
public static void main(String[] args) {
try {
// Run the GUI construction in the Event-Dispatching thread for thread-safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Control_GUI.createAndShowGUI(); // Let the constructor do the job
}
});
} catch(Exception e) {
//bad times
}
}
}
class Control_GUI extends JPanel {
private static final long serialVersionUID = 1L;
public static TabA tab1 = new TabA();
public Control_GUI() {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel4 = tab1.makeTabPanel();
tabbedPane.addTab("Tab Name", null, panel4, "Tool Tip");
//Add the tabbed pane to this panel.
add(tabbedPane);
//The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from
* the event dispatch thread.
*/
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Tool Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Control_GUI(), BorderLayout.CENTER);
//Display the window.
frame.setSize(700, 400);
frame.setVisible(true);
}
}
class TabA {
private JComboBox<String> process = new JComboBox<>(
new String[] {"1","2","3","4"});
/**
* empty constructor
*/
public TabA() {}
/**
* Get the values from the text fields and check boxes
* and convert them into arguments
* @return List<String>
*/
public List<String> toolArgs() {
List<String> temp = new ArrayList<String>();
return temp;
}
/**
* makes the contents of the Tool tab
*
* @return JComponent
*/
public JComponent makeTabPanel() {
/*
* set up panel and define layout
*/
final JPanel panel = new JPanel(false);
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
/*
* define and set up components
*/
//choose process
JLabel processLabel = new JLabel("Process:");
process.setEditable(false);
/*
* add components to panel
*/
panel.add(processLabel);
panel.add(process);
/*
* define positioning of components
*/
//process
layout.putConstraint(SpringLayout.WEST, processLabel, 5, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, processLabel, 5, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, process, 5, SpringLayout.EAST, processLabel);
layout.putConstraint(SpringLayout.NORTH, process, 15, SpringLayout.NORTH, panel);
//process dropdown action on select
process.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String temp = (String) ((JComboBox<String>)e.getSource()).getSelectedItem();
System.out.println(temp);
}
});
return panel;
}
}
编辑:
所以我想我已经解决了问题,或者说代码本身没有问题。我曾尝试在不同的计算机上运行此程序,但从未能够重现此问题。这似乎只是我正在运行的MacBookPro版本所特有的东西。我不能让它发生在比我更新的型号上,但在类似于我的或旧的版本上,我可以让它发生。我不知道到底是什么原因造成的
作为参考,我使用的是~2011 13英寸MacBook Pro。为了更快地获得更好的帮助,请发布一个最小的完整可验证示例或简短、自包含、正确的示例。任何错误或异常?例如,它从选择1开始,但我将其更改为3,事件触发器会看到选择了3,但组合框仍显示1。-组合框中的数据不包含值1或3,因此我不知道您在说什么。@camickr已更新,以便您能够更好地理解…@cmeinert,发布的代码无法编译。在我修复了这些问题之后,代码运行了,但在选项卡上看不到任何组件。我去掉了SpringLayout,只使用了默认的FlowLayout,并显示了标签和组合框。我单击了一个项目,输出就是我单击的项目,组合框显示了该项目。所以我不知道你的问题是关于什么的。下次发布正确的/MCVE/SSCCE。所有代码都应该在一个文件中,包括导入,这样我们就可以复制/粘贴/编译和测试。