java jcombobox 事件_java – 在JComboBox上附加动作事件箭头JButton

也许问题是由于您期望JComboBox不是BasicComboBoxUI而是另一种外观和感觉,也许是MetalComboBoxUI.

您可以从现有的JComboBox对象中提取JButton组件,而不是创建新的CustomBasicComboBoxUI对象吗?即,

import java.awt.Component;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class ComboBoxArrowListener {

private static void createAndShowUI() {

String[] data = {"One", "Two", "Three"};

JComboBox combo = new JComboBox(data);

JPanel panel = new JPanel();

panel.add(combo);

JButton arrowBtn = getButtonSubComponent(combo);

if (arrowBtn != null) {

arrowBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("arrow button pressed");

}

});

}

JFrame frame = new JFrame("ComboBoxArrowListener");

frame.getContentPane().add(panel);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

private static JButton getButtonSubComponent(Container container) {

if (container instanceof JButton) {

return (JButton) container;

} else {

Component[] components = container.getComponents();

for (Component component : components) {

if (component instanceof Container) {

return getButtonSubComponent((Container)component);

}

}

}

return null;

}

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

createAndShowUI();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值