如何在java中按钮单击时显示一个组合框?

时间:2014-03-23 21:32:58

标签: java swing combobox jbutton

我试图得到它,所以当按下按钮时,java中会显示一个组合框。这是我尝试过的。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.border.TitledBorder;
public class main {
    public static void main(String[] args) { 
        final JFrame testFrame = new JFrame();
        testFrame.setSize(300,450);     
        testFrame.setLocation(150,250);     
        testFrame.setTitle("My frame");
        testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        testFrame.setResizable(true);       
        testFrame.setVisible(true); 
        testFrame.setLayout(new FlowLayout());
        final JButton testButton = new JButton("show");
        testFrame.add(testButton);


        class MyListener implements ActionListener 
         {
            public void actionPerformed(ActionEvent event) {
                JButton clickedButton = (JButton) event.getSource();
                if (clickedButton == testButton) {
                    String[] myArray = {"test","test2"};
                    JComboBox testingCom = new JComboBox(myArray);
                    testFrame.add(testingCom);
                }
            }
         }



    }

}

非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:3)

根据您的代码,您永远不会addActionListener到您的按钮。 在ActionListener,您每次调用actionPerformed时都会创建一个组合框的新实例,如果您想要的是什么,我可能不感兴趣如果它可见或不可见。

所以你可以改变你的代码:

      final JButton testButton = new JButton("show");
      final JComboBox combo = new JComboBox(new String[]{"test1","test2"});
      testButton.addActionListener(new ActionListener(){
              // this is anonymous class
             @Override
             public void actionPerformed(ActionEvent evt){
                  //then you know that is attached to this button
                  combo.setVisible(!combo.isVisible());
             }
      });              
      combo.setVisible(Boolean.FALSE);
      testFrame.add(testButton);
      testFrame.add(combo);

答案 1 :(得分:2)

只需在ActionListener中将组合设置为可见或不可见,然后重新验证并重新绘制其容器。如,

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
   public static void main(String[] args) {
      final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
      final JPanel mainPanel = new JPanel();
      mainPanel.setPreferredSize(new Dimension(250, 100));
      mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            myCombo.setVisible(!myCombo.isVisible());
            mainPanel.revalidate();
            mainPanel.repaint();
         }
      }));
      mainPanel.add(myCombo);

      JOptionPane.showMessageDialog(null, mainPanel);
   }

}

也许更简洁的方法是使用CardLayout。如果您使用此功能,您的组件将无法转移删除和查看组合。例如:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class Main2 {
   public static void main(String[] args) {
      final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});

      final CardLayout cardLayout = new CardLayout();
      final JPanel cardPanel = new JPanel(cardLayout);
      cardPanel.add(myCombo, "combo");
      cardPanel.add(new JLabel(), "empty");

      final JPanel mainPanel = new JPanel();
      mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {

         @Override
         public void actionPerformed(ActionEvent evt) {
            cardLayout.next(cardPanel);
         }
      }));
      mainPanel.add(cardPanel);

      JOptionPane.showMessageDialog(null, mainPanel);
   }

}