构建下拉菜单按钮,构建下拉菜单,public class
分享于 点击 23416 次 点评:83
构建下拉菜单按钮,构建下拉菜单,public class
public class DropDownComponent extends JComponent implements ActionListener, AncestorListener { protected JComponent drop_down_comp; protected JComponent visible_comp; protected JButton arrow; protected JWindow popup; public DropDownComponent(JComponent vcomp, JComponent ddcomp) { drop_down_comp = ddcomp; visible_comp = vcomp; arrow = new JButton(new MetalComboBoxIcon()); Insets insets = arrow.getMargin(); arrow.setMargin( new Insets( insets.top, 1, insets.bottom, 1 ) ); arrow.addActionListener(this); addAncestorListener(this); setupLayout(); } } protected void setupLayout() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gbl); c.weightx = 1.0; c.weighty = 1.0; c.gridx = 0; c.gridy = 0; c.fill = c.BOTH; gbl.setConstraints(visible_comp,c); add(visible_comp); c.weightx = 0; c.gridx++; gbl.setConstraints(arrow,c); add(arrow); } public void actionPerformed(ActionEvent evt) { // build pop-up window popup = new JWindow(getFrame(null)); popup.getContentPane().add(drop_down_comp); popup.addWindowFocusListener(new WindowAdapter() { public void windowLostFocus(WindowEvent evt) { popup.setVisible(false); } }); popup.pack(); // show the pop-up window Point pt = visible_comp.getLocationOnScreen(); pt.translate(0,visible_comp.getHeight()); popup.setLocation(pt); popup.toFront(); popup.setVisible(true); popup.requestFocusInWindow(); } protected Frame getFrame(Component comp) { if(comp == null) { comp = this; } if(comp.getParent() instanceof Frame) { return (Frame)comp.getParent(); } return getFrame(comp.getParent()); } public void ancestorAdded(AncestorEvent event){ hidePopup(); } public void ancestorRemoved(AncestorEvent event){ hidePopup(); } public void ancestorMoved(AncestorEvent event){ if (event.getSource() != popup) { hidePopup(); } } public void hidePopup() { if(popup != null && popup.isVisible()) { popup.setVisible(false); } }} public class ColorSelectionPanel extends JPanel { public ColorSelectionPanel() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gbl); // reusable listener for each button ActionListener color_listener = new ActionListener() { public void actionPerformed(ActionEvent evt) { selectColor(((JButton)evt.getSource()).getBackground()); } }; // set up the standard 12 colors Color[] colors = new Color[12]; colors[0] = Color.white; colors[1] = Color.black; colors[2] = Color.blue; colors[3] = Color.cyan; colors[4] = Color.gray; colors[5] = Color.green; colors[6] = Color.lightGray; colors[7] = Color.magenta; colors[8] = Color.orange; colors[9] = Color.pink; colors[10] = Color.red; colors[11] = Color.yellow; // lay out the grid c.gridheight = 1; c.gridwidth = 1; c.fill = c.NONE; c.weightx = 1.0; c.weighty = 1.0; for(int i=0; i<3; i++) { for(int j=0; j<4; j++) { c.gridx=j; c.gridy=i; JButton button = new ColorButton(colors[j+i*4]); gbl.setConstraints(button,c); add(button); button.addActionListener(color_listener); } } } // fire off a selectedColor property event protected Color selectedColor = Color.black; public void selectColor(Color newColor) { Color oldColor = selectedColor; selectedColor = newColor; firePropertyChange("selectedColor",oldColor, newColor); } }} public class ColorButton extends JButton { public ColorButton(Color col) { super(); this.setText(""); Dimension dim = new Dimension(15,15); this.setSize(dim); this.setPreferredSize(dim); this.setMinimumSize(dim); this.setBorderPainted(true); this.setBackground(col); } } public class DropDownTest extends JPanel { public static void main(String[] args) { final JButton status = new JButton("Color"); final JPanel panel = new ColorSelectionPanel(); final DropDownComponent dropdown = new DropDownComponent(status,panel); panel.addPropertyChangeListener("selectedColor", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { dropdown.hidePopup(); status.setBackground((Color)evt.getNewValue()); } }); JFrame frame = new JFrame("Drop Down Test"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout( )); frame.getContentPane().add("North",dropdown); frame.getContentPane().add("Center",new JLabel("Drop Down Test")); frame.pack(); frame.setSize(300,300); frame.show(); } }//该片段来自于http://byrx.net
用户点评