组合框显示按钮列表

时间:2014-07-01 11:08:07

标签: java swing button combobox

组合框有问题,显示按钮列表。我现在拥有的是什么 http://hizliresim.com/djQzr7

但是我想要更像这样的东西 http://hizliresim.com/QXDE3G

第一个按钮是组合框,第二个按钮是单击时的组合框。

这是代码

package asd;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

public class asd extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                asd frame = new asd();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public asd() {
    try {
        // Set System L&F
    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    catch (ClassNotFoundException e) {
       // handle exception
    }
    catch (InstantiationException e) {
       // handle exception
    }
    catch (IllegalAccessException e) {
       // handle exception
    }
    ComboBoxRender renderer;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    JComboBox comboBox = new JComboBox();
    comboBox.setBounds(92, 85, 60, 40);     
    renderer = new ComboBoxRender();
    comboBox.setRenderer(renderer);
    comboBox.addItem("1");
    comboBox.addItem("2");
    comboBox.addItem("3");
    comboBox.setPreferredSize(new Dimension(60, 40));
    comboBox.setMinimumSize(new Dimension(70, 30));
    comboBox.setMaximumSize(new Dimension(500, 500));
    comboBox.setBackground(Color.GRAY);
    comboBox.setUI(new javax.swing.plaf.metal.MetalComboBoxUI(){
        public void layoutComboBox(Container parent, MetalComboBoxLayoutManager manager) {
          super.layoutComboBox(parent, manager);
          arrowButton.setBounds(0,0,0,0);
        }
      });

    contentPane.add(comboBox);
}
}
class ComboBoxRender implements ListCellRenderer<Object> {

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,boolean      isSelected, boolean cellHasFocus) { 

        JButton btn = new JButton(value.toString());
        btn.setSelected(false);
        btn.setBorderPainted(true);
        btn.setBackground(Color.gray);
        btn.setSize(new Dimension(100, 100));
        btn.setMargin(new Insets(1, 1, 1, 1));
        btn.setMinimumSize(new Dimension(200,200));
        return btn;

}

}

1 个答案:

答案 0 :(得分:1)

为此您需要使用ListCellRenderer。阅读custom renderers

例如:

import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class TestFrame extends JFrame {

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        JComboBox<String> box = new JComboBox<>(new String[]{"1","2"});
        box.setRenderer(getRenderer());
        add(box);
    }


    private ListCellRenderer<? super String> getRenderer() {
        return new ListCellRenderer<String>() {
            private JButton btn = new JButton();

            public Component getListCellRendererComponent(JList<? extends String> list,String value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                btn.setText(value);
                return btn;
            };
        };
    }

    public static void main(String args[]) {
        new TestFrame();
    }

}

enter image description here