//實驗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()]);
}
}
}
}
Java 圖形使用者界面 綜合例子