这是一个关于复选框与单选按钮的一个界面,要求再选择复选框时,改变文本框的字的颜色。
为什么不能通过设置前景色来设置字的颜色?请回答,谢谢!!
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class sz1 extends JApplet implements ItemListener,ActionListener{
JCheckBox c1=new JCheckBox("红色",false);
JCheckBox c2=new JCheckBox("绿色",false);
JCheckBox c3=new JCheckBox("蓝色",false);
ButtonGroup buttongroup1=new ButtonGroup();
JRadioButton rb1=new JRadioButton("10",false);
JRadioButton rb2=new JRadioButton("16",false);
JRadioButton rb3=new JRadioButton("24",false);
JLabel a=new JLabel("请选择");
JTextField t=new JTextField("你好",20);
public void init(){
getContentPane().setSize(300,300);
getContentPane().setVisible(true);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(c1);
getContentPane().add(c2);
getContentPane().add(c3);
buttongroup1.add(rb1);
buttongroup1.add(rb2);
buttongroup1.add(rb3);
getContentPane().add(rb1);
getContentPane().add(rb2);
getContentPane().add(rb3);
getContentPane().add(a);
getContentPane().add(t);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
rb1.addActionListener(this);
rb2.addActionListener(this);
rb3.addActionListener(this);}
public void itemStateChanged(ItemEvent e){
if(e.getItem()=="红色")
t.setForeground(Color.red);
if (e.getItem()=="绿色")
t.setForeground(Color.green);
if (e.getItem()=="蓝色")
t.setForeground(Color.blue);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==rb1)
t.setFont(new Font("宋体",Font.PLAIN,10));
if (e.getSource()==rb2)
t.setFont(new Font("宋体",Font.PLAIN,16));
if (e.getSource()==rb3)
t.setFont(new Font("宋体",Font.PLAIN,24));
}
}
为什么不能通过设置前景色来设置字的颜色?请回答,谢谢!!
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class sz1 extends JApplet implements ItemListener,ActionListener{
JCheckBox c1=new JCheckBox("红色",false);
JCheckBox c2=new JCheckBox("绿色",false);
JCheckBox c3=new JCheckBox("蓝色",false);
ButtonGroup buttongroup1=new ButtonGroup();
JRadioButton rb1=new JRadioButton("10",false);
JRadioButton rb2=new JRadioButton("16",false);
JRadioButton rb3=new JRadioButton("24",false);
JLabel a=new JLabel("请选择");
JTextField t=new JTextField("你好",20);
public void init(){
getContentPane().setSize(300,300);
getContentPane().setVisible(true);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(c1);
getContentPane().add(c2);
getContentPane().add(c3);
buttongroup1.add(rb1);
buttongroup1.add(rb2);
buttongroup1.add(rb3);
getContentPane().add(rb1);
getContentPane().add(rb2);
getContentPane().add(rb3);
getContentPane().add(a);
getContentPane().add(t);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
rb1.addActionListener(this);
rb2.addActionListener(this);
rb3.addActionListener(this);}
public void itemStateChanged(ItemEvent e){
if(e.getItem()=="红色")
t.setForeground(Color.red);
if (e.getItem()=="绿色")
t.setForeground(Color.green);
if (e.getItem()=="蓝色")
t.setForeground(Color.blue);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==rb1)
t.setFont(new Font("宋体",Font.PLAIN,10));
if (e.getSource()==rb2)
t.setFont(new Font("宋体",Font.PLAIN,16));
if (e.getSource()==rb3)
t.setFont(new Font("宋体",Font.PLAIN,24));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货