import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Anli7 implements ActionListener,ItemListener,ListSelectionListener{
JLabel jl;
JButton jbtninput;
JButton jbtnexit;
JRadioButton jrbtn_red;
JRadioButton jrbtn_green;
JRadioButton jrbtn_blue;
JCheckBox jchk_bold;
JCheckBox jchk_italic;
JComboBox jcbo;
JList jlst;
Anli7(){
JFrame jf=new JFrame();
jf.setSize(700,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl=new JLabel("我们喜欢JAVA",JLabel.CENTER);
jbtninput=new JButton("修改文字");
jbtnexit=new JButton("退出");
jbtninput.addActionListener(this);
jbtnexit.addActionListener(this);
//设置单选按钮
jrbtn_red=new JRadioButton("红");
jrbtn_green=new JRadioButton("绿");
jrbtn_blue=new JRadioButton("蓝");
ButtonGroup bg=new ButtonGroup();
bg.add(jrbtn_red);
bg.add(jrbtn_green);
bg.add(jrbtn_blue);
jrbtn_red.addActionListener(this);
jrbtn_green.addActionListener(this);
jrbtn_blue.addActionListener(this);
//设置复选框
jchk_bold=new JCheckBox("粗体");
jchk_italic=new JCheckBox("斜体");
jchk_bold.addActionListener(this);
jchk_italic.addActionListener(this);
//设置下拉列表框
jcbo=new JComboBox();
jcbo.addItem("8");
jcbo.addItem("12");
jcbo.addItem("24");
jcbo.addItem("36");
jcbo.addItemListener(this);
//设置列表框
jlst=new JList();
Vector vv=new Vector();
vv.add("宋体");
vv.add("隶书");
vv.add("黑体");
jlst.setListData(vv);
jlst.addListSelectionListener(this);
//设置布局方式
JPanel jp=new JPanel(new BorderLayout());
JPanel jpc=new JPanel();
JPanel jp1=new JPanel(new GridLayout(4,1));
JPanel jp11=new JPanel();
JPanel jp12=new JPanel();
JPanel jp13=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jps=new JPanel(new FlowLayout(FlowLayout.LEFT,40,10));
FlowLayout fl=new FlowLayout(FlowLayout.LEFT);
jp11.setLayout(fl);
jp12.setLayout(fl);
jp13.setLayout(fl);
jp2.setLayout(fl);
jp11.add(new JLabel("请选择前景色:"));
jp11.add(jrbtn_red);
jp11.add(jrbtn_green);
jp11.add(jrbtn_blue);
jp12.add(new JLabel("请选择字型:"));
jp12.add(jchk_bold);
jp12.add(jchk_italic);
jp13.add(new JLabel("请选择字号:"));
jp13.add(jcbo);
jpc.add(jl);
jp1.add(jp11);
jp1.add(jp12);
jp1.add(jp13);
jp2.add(new JLabel("请选择字体:"));
jp2.add(jlst);
jp3.add(jbtninput);
jp3.add(jbtnexit);
jps.add(jp1);
jps.add(jp2);
jps.add(jp3);
jp.add(jpc,BorderLayout.CENTER);
jp.add(jps,BorderLayout.SOUTH);
jf.add(jp);
jf.setVisible(true);
}
public void itemStateChanged(ItemEvent e){ //单击列表框
fontset();
}
public void valueChanged(ListSelectionEvent e){ //单击下拉列表框
fontset();
}
public void actionPerformed(ActionEvent e){ //单击单选按钮,复选按钮,命令按钮等
if(e.getSource()==jrbtn_red){
jl.setForeground(new Color(255,0,0));
}
if(e.getSource()==jrbtn_green){
jl.setForeground(new Color(0,255,0));
}
if(e.getSource()==jrbtn_blue){
jl.setForeground(new Color(0,0,255));
}
if(e.getSource()==jchk_bold){
fontset();
}
if(e.getSource()==jchk_italic){
fontset();
}
if(e.getSource()==jbtninput){
String str = JOptionPane.showInputDialog("请输入内容:");
jl.setText(str);
}
if(e.getSource()==jbtnexit){
int jopvalue=JOptionPane.showConfirmDialog(null, "您确定要退出吗?", "操作提示", JOptionPane.YES_NO_OPTION);
if(jopvalue==JOptionPane.YES_OPTION)
System.exit(0);
}
}
void fontset(){ //设置字体
int fv=Font.PLAIN;
if(jchk_bold.isSelected())
fv=Font.PLAIN|Font.BOLD;
if(jchk_italic.isSelected())
fv=fv|Font.ITALIC;
Object folst=jlst.getSelectedValue();
String fs="宋体";
if(!(folst==null)){
fs=folst.toString();
}
Object focbo=jcbo.getSelectedItem();
int fn=12;
if(!(focbo==null)){
fn=Integer.parseInt(focbo.toString());
}
jl.setFont(new Font(fs,fv,fn));
}
public static void main(String args[]){
new Anli7();
}
}