
本文共 975 字,大约阅读时间需要 3 分钟。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main extends JFrame {
JComboBox box = new JComboBox(
new String[] { "1", "22", "33" });
public Main() {
box.setBackground(Color.RED);
box.addFocusListener(getFocusListener());
JTextField f = new JTextField();
add(box, BorderLayout.SOUTH);
add(f, BorderLayout.NORTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private FocusListener getFocusListener() {
return new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
super.focusGained(arg0);
box.setBackground(Color.BLUE);
validate();
}
@Override
public void focusLost(FocusEvent arg0) {
super.focusLost(arg0);
box.setBackground(Color.red);
validate();
}
};
}
public static void main(String... s) {
new Main();
}
}
转载地址:https://blog.csdn.net/weixin_33501587/article/details/114557120 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
