详细说明等待线程

时间:2015-11-19 15:42:01

标签: java

我找到了一个问题的答案,但是我对如何在我的程序中实现它感到非常困惑。我不会发布我的整个程序,因为它很长,而且我不确定如何隔离这个例子。真的,我只需要澄清一下如何让它发挥作用。

基本上我拥有的是两个不同的GUI。第一个GUI是一个自定义双重列表,允许用户在列表1和列表2之间移动项目。第二个GUI将包含在while循环中。对于移动到列表2的每个项目,将出现单独的JOptionsPane,允许用户为每个项目选择原因代码。

我遇到的问题是,当我尝试运行第一个自定义GUI,然后是第二个GUI时,它们会同时出现。我理解为什么,自定义GUI不像JOptionsPane,并且不会神奇地停止其余的代码。我尝试将条件变量添加到第二个GUI,这将阻止它运行,除非用户单击双列表中的确认,但这不起作用。下面是我在这里找到的代码我很困惑

new Thread(new Runnable(){
                        @Override
                        public void run(){
                        ListGUI gui = new ListGUI();
                        gui.createAndShowGUI(); 
                        unlockWaiter();
                        }
                    }).start();

                    System.out.println("test");

                    waitForThread();

                    System.out.println("test2");



                }

public static void waitForThread(){
        monitorState=true;
        while(monitorState){
            synchronized(monitor){
                try{
                    monitor.wait();
                }catch (Exception e) {}
            }
        }
    }

public static void unlockWaiter(){
        synchronized(monitor){
            monitorState=false;
            monitor.notifyAll();
        }
    }

从顶部示例中可以看出,我尽力通过测试来实现此代码。 " gui.createAndShowGUI"只是我的双列表方法。

我感到困惑的是我应该把它放在"测试"和" test2"打印出来。如果我的第二个GUI是调用JOptionPane的方法(我需要将它包含在循环中),我在哪里将此方法调用放在下面的代码中?

如何在出现双重列表时确保代码停止,并在用户单击“继续”按钮时仅继续使用第二个GUI?

抱歉,我没有更多的代码可以显示,但我很困惑,我可以得到更远的代码。

解决方案:

我的解决方案不是使用自定义GUI,而是使用带有添加面板的JOptionPane。由于JOptionPane已经具有保存其他所有内容的功能,直到该过程完成,我发现这种方法效果最好。我会在下面发布我的代码给任何好奇我是如何做到的:

package jpaneltest;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class JOptionPaneExample

{

    public static JList outputDetails, inputDetails;
    public static JButton buttonin;
    public static JButton buttonout;
    public static JButton buttontest;
    public static ArrayList<String> list = new ArrayList<String>();
    public static DefaultListModel input;
    public static DefaultListModel output;

    public static void displayGUI(){

        JOptionPane.showConfirmDialog(null, getPanel(),"JOptionPane Example : ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

    }


    private static JPanel getPanel(){

        JPanel panel = new JPanel();



        list.add("1");
        list.add("2");
        list.add("3");

        input = new DefaultListModel();
        output = new DefaultListModel();

        String[] shoppingItems = new String[BufferedWriterTester.entryDetails.size()];
        shoppingItems = BufferedWriterTester.entryDetails.toArray(shoppingItems);

        for(int i = 0; i < shoppingItems.length; i++){
            input.addElement(shoppingItems[i]);
        }

        outputDetails = new JList(input);
        outputDetails.setVisibleRowCount(10);
        outputDetails.setFixedCellHeight(20);
        outputDetails.setFixedCellWidth(140);
        outputDetails.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list1 = new JScrollPane(outputDetails);

        inputDetails = new JList(output);
        inputDetails.setVisibleRowCount(10);
        inputDetails.setFixedCellHeight(20);
        inputDetails.setFixedCellWidth(140);
        inputDetails.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list2 = new JScrollPane(inputDetails);

        JPanel buttonPanel = new JPanel();

        buttonin = new JButton(">>");
        buttonin.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                int[] fromindex = outputDetails.getSelectedIndices();
                Object[] from = outputDetails.getSelectedValues();

                for(int i=0; i< from.length; i++){
                    output.addElement(from[i]);
                }

                for(int i = (fromindex.length-1); i>=0; i--){
                    input.remove(fromindex[i]);
                }
            }
        });
        buttonPanel.add(buttonin);

        buttonout = new JButton("<<");
        buttonout.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                Object[] to = inputDetails.getSelectedValues();
                int[] toindex = inputDetails.getSelectedIndices();

                for(int i = 0; i < to.length; i++){
                    input.addElement(to[i]);
                }

                for(int i = (toindex.length-1); i >=0; i--){
                    output.remove(toindex[i]);
                }
            }
        });
        buttonPanel.add(buttonout);

        buttontest=new JButton("Test");
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));

        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(list1);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(buttonPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(list2);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));


        return panel;
    }


}

1 个答案:

答案 0 :(得分:1)

在接受zapls反馈后,我再看看我的选择。我发现,对于我的意图,最好的解决方案是简单地创建一个JOptionsPane,其中添加了额外的面板,可以处理上述要求。这样,该过程简化并自动化。

相关问题