Java Swing GUI;能够在长时间运行过程中调整窗口大小,但主框架内的面板获胜';在流程完成之前,请不要更新
我正在尝试使用SwingUtilities.invokelater,以便在长进程运行时调整程序窗口的大小和移动。我得到的是,我能够移动窗口并调整其大小,但内部组件不会更新,例如,如果我将窗口调整为比开始时更大,我只会将其填充为黑色 我不太确定哪里出了问题,但我觉得我的面板/框架就是这样设置的?任何帮助都将不胜感激Java Swing GUI;能够在长时间运行过程中调整窗口大小,但主框架内的面板获胜';在流程完成之前,请不要更新,java,swing,resize,event-dispatch-thread,invokelater,Java,Swing,Resize,Event Dispatch Thread,Invokelater,我正在尝试使用SwingUtilities.invokelater,以便在长进程运行时调整程序窗口的大小和移动。我得到的是,我能够移动窗口并调整其大小,但内部组件不会更新,例如,如果我将窗口调整为比开始时更大,我只会将其填充为黑色 我不太确定哪里出了问题,但我觉得我的面板/框架就是这样设置的?任何帮助都将不胜感激 public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManage
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error setting system look and feel: "+e);
}
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
BrowseModel model = new BrowseModel();
BrowseView view = new BrowseView(model);
BrowseController controller = new BrowseController(model, view);
view.setVisible(true);
}
});
}
我的看法是:
BrowseView(BrowseModel model){
m_model = model;
JScrollPane targetUrlScroller = new JScrollPane(m_targetURLs);
targetUrlScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
targetUrlScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
targetUrlScroller.setAlignmentX(Component.LEFT_ALIGNMENT);
m_resultsTable = new JTable(table_model);
table_model.addColumn("Locale");
table_model.addColumn("URL");
table_model.addColumn("Product");
table_model.addColumn("Category");
table_model.addColumn("Filter");
table_model.addColumn("Article Title");
table_model.addColumn("Article Image");
table_model.addColumn("Article Description");
table_model.addColumn("Article URL");
m_resultsTable.setFillsViewportHeight(true);
JScrollPane resultsScroller = new JScrollPane(m_resultsTable);
resultsScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
resultsScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
resultsScroller.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel localeSelection = new JPanel();
localeSelection.add(new JLabel("Locales:"));
localeSelection.add(m_selectLocale = new JComboBox(getLocales().toArray()));
localeSelection.setLayout(new FlowLayout(FlowLayout.RIGHT));
localeSelection.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(m_currentStatus);
buttonsPanel.add(m_clearButton);
buttonsPanel.add(m_runButton);
buttonsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JPanel content = new JPanel();
content.setBorder(new EmptyBorder(10, 10, 10, 10));
content.setAlignmentX(Component.LEFT_ALIGNMENT);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(localeSelection);
content.add(new JLabel("Target URLs:"));
content.add(targetUrlScroller);
content.add(new JLabel("Results:"));
content.add(resultsScroller);
content.add(buttonsPanel);
this.setContentPane(content);
this.pack();
this.setMinimumSize(new Dimension(900,600));
this.setTitle("Browse Page Inspector");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
听起来您正在事件分派线程中运行长时间运行的进程,这会阻止GUI的更新。当事件分派线程忙于完成任务时,不会发生绘图,因为swing在同一线程中完成所有工作。因此,在任务完成并且swing有机会完成其工作之前,无法更新GUI(如果任务是一个无限循环,那么这永远不会发生) 解决方案是在后台运行耗时的任务。
通常是后台任务最方便的工具。长时间运行的流程在哪里?发布的代码似乎没有任何问题。听起来像是在事件分派线程中运行长时间运行的进程,这会阻止GUI更新。在后台执行耗时的任务通常是一种很好的方法。我的长时间运行的进程将在模型中,此时我正在使用循环和线程对其进行测试。sleep(1000)@kiheru,很可能是这样的,我本来打算请一个swingworker,但我想先让它工作起来。我要试一试。谢谢不客气:-)这是UI冻结的最典型原因。我会尝试写一些更完整的东西,即使评论已经包含了所有的内容。有一个公认的答案会让其他人更容易接受。