0

我试图使用 SwingUtilities.invokelater 来允许我在我的长进程运行时调整程序窗口的大小和移动我的程序窗口。我得到的是我能够移动窗口并调整它的大小,但里面的组件不会更新,例如,如果我将窗口的大小调整为比它开始时更大,我只会得到黑色填充它。

我不太确定哪里出错了,但我觉得这就是我设置面板/框架的方式?任何帮助,将不胜感激。

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

1 回答 1

2

听起来您正在事件调度线程中运行长时间运行的进程,这会阻止 GUI 更新。当事件调度线程忙于完成任务时,不会发生绘图,因为 Swing 在同一个线程中完成所有工作。因此,只有在任务完成并且 Swing 有机会完成其工作(如果任务是无限循环时,这永远不会)之前,才能更新 GUI。

解决方案是在后台运行耗时的任务。 SwingWorker通常是最方便的后台任务工具。

于 2014-01-20T15:55:15.807 回答