请参看下面代码:package untitled20;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.plaf.basic.BasicComboBoxEditor;
public class Frame1 extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JComboBox jComboBox1 = new JComboBox();
EditorEX comboboxEditor = new EditorEX(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.getContentPane().add(jComboBox1, new XYConstraints(18, 70, 199, 23));
//------------------------------------------------------------------
jComboBox1.addItem("测试数据1");
jComboBox1.addItem("测试数据2");
jComboBox1.setEditable(true);//必须这这句,否则你看不到所要的效果.
jComboBox1.setEditor(comboboxEditor);
comboboxEditor.setBackgroundColor(Color.black);
comboboxEditor.setForegroundColor(Color.red); comboboxEditor.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.err.println(" get focus");
} public void focusLost(FocusEvent e) {
System.err.println(" lost focus");
}
});
} //Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} class EditorEX extends BasicComboBoxEditor { public void setBackgroundColor(Color value) {
editor.setBackground(value);
} public void setForegroundColor(Color value) {
editor.setForeground(value);
} public void addFocusListener(FocusListener l) {
editor.addFocusListener(l); } }
}
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.plaf.basic.BasicComboBoxEditor;
public class Frame1 extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JComboBox jComboBox1 = new JComboBox();
EditorEX comboboxEditor = new EditorEX(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.getContentPane().add(jComboBox1, new XYConstraints(18, 70, 199, 23));
//------------------------------------------------------------------
jComboBox1.addItem("测试数据1");
jComboBox1.addItem("测试数据2");
jComboBox1.setEditable(true);//必须这这句,否则你看不到所要的效果.
jComboBox1.setEditor(comboboxEditor);
comboboxEditor.setBackgroundColor(Color.black);
comboboxEditor.setForegroundColor(Color.red); comboboxEditor.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.err.println(" get focus");
} public void focusLost(FocusEvent e) {
System.err.println(" lost focus");
}
});
} //Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} class EditorEX extends BasicComboBoxEditor { public void setBackgroundColor(Color value) {
editor.setBackground(value);
} public void setForegroundColor(Color value) {
editor.setForeground(value);
} public void addFocusListener(FocusListener l) {
editor.addFocusListener(l); } }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货