Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java setPopupVisible在setvisible为true时被取消

当我按下下面程序中的按钮时,我希望JComboBox中的弹出菜单出现并保持打开状态

然而,只有在组合框已经可见的情况下,它才会在第二次按下时这样做。有没有办法让它在第一次推的时候出现

public class Problem extends JPanel 
implements ActionListener{

private static String SEARCH = "start search";
private static String SELECTED = "Selected";

private JTextField field2;
private JTextField field1;

private JComboBox list = new JComboBox();

public Problem() {
    super(new GridBagLayout());

    //Construct the panel
    field2=new JTextField("");
    field1=new JTextField("7564");
    JButton search=new JButton("Search");
    search.addActionListener(this);
    search.setActionCommand(SEARCH);

    list.addActionListener(this);
    list.setActionCommand(SELECTED);


    //Add everything to this panel.
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 8;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(field2,c);

    list.setVisible(false);
    add(list,c);

    c.gridx = 8;
    c.gridy = 0;
    c.gridwidth = 8;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(field1,c);

    c.gridx = 16;
    c.gridy = 0;
    c.gridwidth = 4;
    c.gridheight = 3;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.anchor = GridBagConstraints.NORTH;

    add(search,c);


}

public void actionPerformed(ActionEvent ae) {



    if (ae.getActionCommand()==SEARCH){
        String f2=field2.getText();
        String f1=field1.getText();
        if(f2.equals("")){

            f2=selection(f1);
            f2=null;
        }
        if(f2!=null){
            field2.setText(f2);
        }
        else{
            System.out.println("setPopupVisible runs");
            field2.setVisible(false);
            list.setVisible(true);
            field2.setText("");
            list.setPopupVisible(true);
        }
    }
    else if(ae.getActionCommand().equals(SELECTED)){
        System.out.println("select");
        String listtext=(String) list.getSelectedItem();
        list.removeAllItems();
        if(listtext==null||!listtext.contains(": ")){
            return;
        }
        String f2=listtext.split(": ")[0];
        list.setVisible(false);
        field2.setVisible(true);
        field2.setText(f2);
    }
}

private String selection(String sn) {
    System.out.println("selection runs");

    String name="7";
    list.removeActionListener(this);
    list.removeAllItems();
        list.addItem(name);
    list.addActionListener(this);
    return null;
}

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Stuff");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    Problem newContentPane = new Problem();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(800, 600);
}

public static void main(String[] args) throws FileNotFoundException {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}
}

共 (0) 个答案