JAVA:实现combobox中点击背景颜色变化
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassDoNotBotherMee...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DoNotBotherMe extends JFrame {
private JTextArea ta;
private JPanel p;
private JComboBox cb;
public DoNotBotherMe()
{
super("Testing ComboBox...");
Container c=this.getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
p=new JPanel();
ta=new JTextArea(10,20);
ta.setText("Please choose your age!!!");
cb=new JComboBox();
cb.addItem("18 age");
cb.addItem("19 age");
cb.addItem("20 age");
cb.addItem("21 age");
cb.addItem("22 age");
cb.addItemListener(new ComboBoxListener());
p.add(cb);
c.add(ta);
c.add(p);
this.setLocationRelativeTo(null);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class ComboBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String age=cb.getSelectedItem().toString().trim();
ta.setText("Your age is"+age);
}
}
public static void main(String args[])
{
new DoNotBotherMe();
}
}
这段代码不能改变背景颜色,我要的是改变背景,如点19岁,20岁,改变其颜色,编写事件代码 展开
import javax.swing.*;
import java.awt.event.*;
public class DoNotBotherMe extends JFrame {
private JTextArea ta;
private JPanel p;
private JComboBox cb;
public DoNotBotherMe()
{
super("Testing ComboBox...");
Container c=this.getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
p=new JPanel();
ta=new JTextArea(10,20);
ta.setText("Please choose your age!!!");
cb=new JComboBox();
cb.addItem("18 age");
cb.addItem("19 age");
cb.addItem("20 age");
cb.addItem("21 age");
cb.addItem("22 age");
cb.addItemListener(new ComboBoxListener());
p.add(cb);
c.add(ta);
c.add(p);
this.setLocationRelativeTo(null);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class ComboBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String age=cb.getSelectedItem().toString().trim();
ta.setText("Your age is"+age);
}
}
public static void main(String args[])
{
new DoNotBotherMe();
}
}
这段代码不能改变背景颜色,我要的是改变背景,如点19岁,20岁,改变其颜色,编写事件代码 展开
1个回答
展开全部
背景颜色实际上是JTextArea的颜色,也就是说你只要改变ta的背景色就是改变窗口的背景颜色了。我改了一下代码,你可以看看:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class DoNotBotherMe extends JFrame {
private JTextArea ta;
private JPanel p;
private JComboBox cb;
public DoNotBotherMe() {
super("Testing ComboBox...");
Container c = this.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
p = new JPanel();
ta = new JTextArea(10, 20);
ta.setText("Please choose your age!!!");
cb = new JComboBox();
cb.addItem("18 age");
cb.addItem("19 age");
cb.addItem("20 age");
cb.addItem("21 age");
cb.addItem("22 age");
cb.addItemListener(new ComboBoxListener());
p.add(cb);
c.add(ta);
c.add(p);
this.setLocationRelativeTo(null);
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class ComboBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
String age = cb.getSelectedItem().toString().trim();
ta.setText("Your age is" + age);
int a = Integer.parseInt(String.valueOf(age.charAt(0))
+ String.valueOf(age.charAt(1)));
switch (a) {
case 18:
ta.setBackground(Color.BLACK);
break;
case 19:
ta.setBackground(Color.RED);
break;
case 20:
ta.setBackground(Color.BLUE);
break;
case 21:
ta.setBackground(Color.GRAY);
break;
default:
ta.setBackground(Color.GREEN);
break;
}
}
}
public static void main(String args[]) {
new DoNotBotherMe();
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class DoNotBotherMe extends JFrame {
private JTextArea ta;
private JPanel p;
private JComboBox cb;
public DoNotBotherMe() {
super("Testing ComboBox...");
Container c = this.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
p = new JPanel();
ta = new JTextArea(10, 20);
ta.setText("Please choose your age!!!");
cb = new JComboBox();
cb.addItem("18 age");
cb.addItem("19 age");
cb.addItem("20 age");
cb.addItem("21 age");
cb.addItem("22 age");
cb.addItemListener(new ComboBoxListener());
p.add(cb);
c.add(ta);
c.add(p);
this.setLocationRelativeTo(null);
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class ComboBoxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
String age = cb.getSelectedItem().toString().trim();
ta.setText("Your age is" + age);
int a = Integer.parseInt(String.valueOf(age.charAt(0))
+ String.valueOf(age.charAt(1)));
switch (a) {
case 18:
ta.setBackground(Color.BLACK);
break;
case 19:
ta.setBackground(Color.RED);
break;
case 20:
ta.setBackground(Color.BLUE);
break;
case 21:
ta.setBackground(Color.GRAY);
break;
default:
ta.setBackground(Color.GREEN);
break;
}
}
}
public static void main(String args[]) {
new DoNotBotherMe();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询