//实验5
/*
*实验内容:综合例子,把所有基本组件放在同一个窗口里显示出来
*基本组件包括:标签组件、按钮组件、文本框组件、文本区组件、单选按钮组件
*复选框组件、下拉列表组件
*实现组件的基本功能,程序运行结果如图
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font.*;
import java.awt.Color;
public class JF extends JFrame
{ //定义7个面板分别存放7个不同组件
private JPanel p1=new JPanel(); //标签组件面板
private JPanel p2=new JPanel(); //按钮组件面板
private JPanel p3=new JPanel(); //文本框组件面板
private JPanel p4=new JPanel(); //文本区组件面板
private JPanel p5=new JPanel(); //单选按钮组件面板
private JPanel PColor =new JPanel(); //创建一个用来显示颜色的面板
private JPanel PButton = new JPanel(); //创建存放单选按钮的面板
private JRadioButton red = new JRadioButton("red",true);//创建单选按钮选项
private JRadioButton green=new JRadioButton("green");
private JRadioButton blue = new JRadioButton("blue");
private JPanel p6= new JPanel(); //复选框组件面板
private JLabel label2 = new JLabel("请注意观察宋体文字的变化");
private JCheckBox b = new JCheckBox("粗体"); //创建复选框
private JCheckBox i = new JCheckBox("斜体");
private JPanel p7 =new JPanel(); //下拉列表组件面板
private JComboBox lbk;
private JLabel label3;
private String names[]={"计算机学院","珠宝学院","外语学院","经贸管理学院"};
public JF()
{
super("综合例子"); //调用父类将窗口命名为"综合例子"
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定窗口对象关闭动作
Container c=getContentPane(); //创建内容面板对象
c.setLayout(new GridLayout(4,2,0,0));//设置面板布局
//添加标签组件
JLabel label1 = new JLabel("This is a Label"); //使用文本创建一个标签对象
label1.setFont(new Font("Serif",Font.BOLD,20));
label1.setToolTipText("这是标签对象"); //设置标签的工具提示
p1.add(label1); //添加标签到面板
p1.setBackground(Color.YELLOW); //设置面板背景颜色
c.add(p1);
//添加组件
JButton button1=new JButton("button1"); //创建按钮对象
button1.setFont(new Font("Serif",Font.PLAIN,20));
ImageIcon img1 = new ImageIcon("bird.gif");
JButton button2 = new JButton("button2",img1);
button2.setRolloverIcon(img1);
button2.setFont(new Font("Serif",Font.PLAIN,14));
//为按钮组件注册监听器
BHandler bh = new BHandler();
button1.addActionListener(bh);
button2.addActionListener(bh);
p2.add(button1);
p2.add(button2);
p2.setBackground(Color.green);
c.add(p2); //添加到内容面板
//添加文本框组件
JTextField t1=new JTextField();
JTextField t2=new JTextField();
JTextField t3=new JTextField();
JPasswordField k1=new JPasswordField();
p3.setLayout(new FlowLayout());
p3.setBackground(Color.pink);
t1=new JTextField(10);
t2=new JTextField("Input the test",20);
t2.setFont(new Font("Serif",Font.PLAIN,12));
t3=new JTextField("Can not edit",20);
t3.setFont(new Font("Serif",Font.PLAIN,12));
t3.setEditable(false); //设置为不可编辑
k1=new JPasswordField("123456",15);
p3.add(t1); p3.add(t2); p3.add(t3);
p3.add(k1); c.add(p3);
//添加文本区组件
p4.setBackground(Color.white);
p4.setLayout(new FlowLayout());
JTextArea ta1=new JTextArea(2,8);
JTextArea ta2=new JTextArea(5,10);
ta1.setFont(new Font("Serif",Font.PLAIN,12));
ta2.setFont(new Font("Serif",Font.PLAIN,12));
ta1.setText("JTextArea1");
ta2.setText("JTextArea2");
ta2.setTabSize(10); //设置【Tab】键跳离方法
ta2.setLineWrap(true); //自动换行功能方法
ta2.setWrapStyleWord(true);//断行不断字的功能方法
p4.add(new JScrollPane(ta1));
p4.add(new JScrollPane(ta2));
c.add(p4);
//添加单选按钮组件
ButtonGroup buttonGroup = new ButtonGroup();
red.setFont(new Font("Serif",Font.PLAIN,14));
green.setFont(new Font("Serif",Font.PLAIN,14));
blue.setFont(new Font("Serif",Font.PLAIN,14));
PColor.setBackground(Color.red); //设置默认背景
buttonGroup.add(red); buttonGroup.add(green); //添加选项按钮到组中
buttonGroup.add(blue);
PButton.add(red); PButton.add(green);
PButton.add(blue);
RHandler rh=new RHandler(); //创建监听器
red.addItemListener(rh); blue.addItemListener(rh);//注册监听器
green.addItemListener(rh);
p5.setLayout(new BorderLayout());
p5.add(PColor,BorderLayout.CENTER); //设置布局
p5.add(PButton,BorderLayout.SOUTH);
c.add(p5);
//添加复选框组件
p6.setLayout(new FlowLayout());
label2.setFont(new Font("Serif",Font.PLAIN,35));
p6.add(label2);
b.setFont(new Font("Serif",Font.PLAIN,20));
i.setFont(new Font("Serif",Font.PLAIN,20));
CBHandler ch=new CBHandler();
b.addItemListener(ch); i.addItemListener(ch);
p6.add(b); p6.add(i);
c.add(p6);
//添加下拉列表组件
p7.setBackground(Color.cyan);
lbk = new JComboBox(names); //创建下拉列表对象
lbk.setMaximumRowCount(4); //设置下拉列表所能显示的列表项最大数目
lbk.setSelectedIndex(0); //设置默认的选项
lbk.setFont(new Font("Serif",Font.PLAIN,15)); //设置字体
lbk.addItemListener(new lbHandler()); //注册监听器
label3 = new JLabel("你选择了:计算机学院");
label3.setFont(new Font("Serif",Font.PLAIN,20));
p7.add(lbk,BorderLayout.NORTH);
p7.add(label3,BorderLayout.CENTER);
c.add(p7);
setSize(600,600); //设置窗口大小
setVisible(true); //指定窗口可见
}
public static void main(String[] args)
{
JF d=new JF(); //创建JF类对象d创建窗口
}
//按钮组件的事件处理
private class BHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(JF.this,"你按了:"+event.
getActionCommand());
}
}
//单选按钮组件的事件处理
private class RHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(red.isSelected())
PColor.setBackground(Color.red);
else if(green.isSelected())
PColor.setBackground(Color.GREEN);
else PColor.setBackground(Color.BLUE);
}
}
//复选框组件的事件处理
private class CBHandler implements ItemListener
{
private int vb=Font.PLAIN;
private int vi=Font.PLAIN;
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==b)
vb=b.isSelected()?Font.BOLD:Font.PLAIN;
if(e.getSource()==i)
vi=i.isSelected()?Font.ITALIC:Font.PLAIN;
label2.setFont(new Font("Serif",vb+vi,20));
}
}
//下拉列表组件的事件处理
private class lbHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==e.SELECTED)
{
label3.setText("你选择了:"+names[lbk.getSelectedIndex()]);
}
}
}
}