JavaSwing圖形介面程式設計(一)
阿新 • • 發佈:2019-02-01
本例程式碼執行效果:
本例窗口布局說明:
package three.day.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class JFrameDemo01 extends JFrame implements ItemListener,ActionListener{
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JCheckBox cbRed;
private JCheckBox cbGreen;
private JCheckBox cbBlue;
private JRadioButton rb1;
private JRadioButton rb2;
private JRadioButton rb3;
private JLabel lbChange;
private Font font;
private Color fgColor;
private ButtonGroup bg;
private int fontSize;
private int r,g,b;
public JFrameDemo01(String title) throws HeadlessException {
super(title);
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());//流式佈局
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
Container panel = getContentPane();//主佈局容器
panel.setLayout(new GridLayout(3,1));//網格佈局,3行1列
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
cbRed = new JCheckBox("紅色");//定義複選框並註冊事件監聽器
cbRed.addItemListener(this);
cbGreen = new JCheckBox("綠色");
cbGreen.addItemListener(this);
cbBlue = new JCheckBox("藍色");
cbBlue.addItemListener(this);
rb1 = new JRadioButton("16");//定義單選框並註冊事件監聽器
rb1.addActionListener(this);
rb2 = new JRadioButton("24");
rb2.addActionListener(this);
rb3 = new JRadioButton("34");
rb3.addActionListener(this);
bg = new ButtonGroup();//將多個單選框放置於同一個按鈕組,可保證多選一
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
lbChange = new JLabel("請選擇");//定義一個標籤
fontSize = 12;
font = new Font("宋體",Font.PLAIN,fontSize);//定義一個字型物件
fgColor = new Color(0,0,0);//定義一個顏色物件
lbChange.setFont(font);
lbChange.setForeground(fgColor);
panel1.add(cbRed);//將複選框佈局在panel1
panel1.add(cbGreen);
panel1.add(cbBlue);
panel2.add(rb1);//將單選按鈕佈局在panel2
panel2.add(rb2);
panel2.add(rb3);
panel3.add(lbChange);//將標籤佈局在panel3
pack();//根據控制元件佔據總大小設定JFrame視窗大小
setLocation(400,200);//設定視窗初始化位置
setVisible(true);//設定視窗可見
addWindowListener(new WindowAdapter()//設定點選視窗右上角的關閉按鈕,關閉視窗同時終止當前程序,如不設定,視窗雖然關閉了,可程式仍在後臺執行
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args) {
new JFrameDemo01("JFrameDemo01");//執行JFrame
}
@Override
public void actionPerformed(ActionEvent e) {//設定選擇複選框的對應事件處理程式
if(e.getSource()==rb1)
{
fontSize = 16;
System.out.println("16");
}
else if(e.getSource()==rb2)
{
fontSize = 24;
System.out.println("24");
}
else if(e.getSource()==rb3)
{
fontSize = 34;
System.out.println("34");
}
font = new Font("宋體",Font.PLAIN,fontSize);
lbChange.setFont(font);
}
@Override
public void itemStateChanged(ItemEvent e) {//設定選擇單選框的對應事件處理程式
if((JCheckBox)e.getItem()==cbRed)
{
if(e.getStateChange()==e.SELECTED)
{
r = 255;
}
else
{
r = 0;
}
}
if((JCheckBox)e.getItem()==cbGreen)
{
if(e.getStateChange()==e.SELECTED)
{
g = 255;
}
else
{
g = 0;
}
}
if((JCheckBox)e.getItem()==cbBlue)
{
if(e.getStateChange()==e.SELECTED)
{
b = 255;
}
else
{
b = 0;
}
}
fgColor = new Color(r,g,b);
lbChange.setForeground(fgColor);
}
}