>GUI組件介紹
>>分類
控制組件:button,label
容器:能容納和排列組件的組件。
>>界面的構成:(分析一個代碼的結構層次)
一個頂層容器(主窗口)
頂層容器包含若干個中間容器
每個中間容器包含若干個基本組件
按照合理的佈局方式,將他們組織在一起。
基本組件可相應發生在其上的事件
>>swing中的容器
頂層容器:
JFrame:用來創建application的頂層窗體
JApplet:用來創建applet程序
JDialog:用來創建對話框
JWindow:不常用
中間容器:
JPanel:提供一個面板
JScrollPane:具有滾動條的窗格
JSplitPane:具有拆分功能的窗格
JTablePane:具有若干標籤的窗格
JInternalFrame:
Box:盒式佈局的中間容器
基本組件:
JLabelJButton、JCheckBox、JRadioButtonJList、JComboBoxJTextField、JPasswordField、JTextAreaJToolBar、JToolTip、JProgressBarJSlider、JSpinnerJFileChooser、JColorChooserJMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem、JRadioButtonMenuItem、JPopupMenuJTableJTreeJOptionPane、JSeparator
>swing組件的使用:
package useSwing;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class Containing {
public static void main(String[] args){
//設置JFrame
JFrame f = new JFrame();
f.setTitle("My first JFrame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
Container con = f.getContentPane();
JPanel pan = new JPanel();
//設置JLabel
JLabel Label_name = new JLabel("name");
pan.add(Label_name);
//添加textfield
JTextField tf_name = new JTextField(25);
pan.add(tf_name);
//設置密碼域passwordfield
JLabel Label_pw = new JLabel("password");
pan.add(Label_pw);
JPasswordField password = new JPasswordField(20);
password.setEchoChar('*');
pan.add(password);
//設置單選按鈕radiobox
JLabel Label_sex =new JLabel("sex");
pan.add(Label_sex);
JRadioButton male = new JRadioButton("male",true);
JRadioButton female = new JRadioButton("female",true);
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
pan.add(male);
pan.add(female);
//設置checkbox
JLabel Label_love = new JLabel();
JCheckBox[] love={new JCheckBox("music"),new JCheckBox("football"),new JCheckBox("painting")
};
pan.add(Label_love);
pan.add(love[0]);
pan.add(love[1]);
pan.add(love[2]);
//設置下拉列表ComboBox
JLabel Label_school = new JLabel("school");
pan.add(Label_school);
String[] departmentNames = {
"computer school",
"electronic school",
"software school"
};
JComboBox department = new JComboBox(departmentNames);
department.setEditable(false);
pan.add(department);
//設置List列表框
JLabel Label_choose = new JLabel();
String[] coursesNames = {
"date structure",
"operation system",
"network",
"designer of java",
"first of computer",
"graphic"
};
JList course = new JList(coursesNames);
pan.add(Label_choose);
pan.add(course);
//設置滑動條slider
JLabel Label_score = new JLabel("addScore");
pan.add(Label_score);
JSlider addition = new JSlider(JSlider.HORIZONTAL,0,100,50);
addition.setMajorTickSpacing(10);
addition.setMinorTickSpacing(5);
addition.setPaintTicks(true);
addition.setPaintLabels(true);
addition.setSnapToTicks(true);
pan.add(addition);
//設置文件選擇器JFileChoose
JFileChooser fc1 = new JFileChooser();
//設置顏色選擇器JColorChooser
JColorChooser c = new JColorChooser();
//設置對話框JOptionPane
//設置計時器Timer
//Timer time = new Timer(1000,new ActionListener());
//設置組件的邊框
/*
Border border1 = BorderFactory.createLineBorder(color,int);//創建一個具有指定顏色和寬度的線邊框
border2 = BorderFactory.createTitledBorder(String); //創建一個新標題邊框,採用默認設置,並指定了標題文本。
Border border3 = BorderFactory. createLoweredBevelBorder();//創建一個具有凹入斜面邊緣的邊框
Border border4 = BorderFactory.createRaisedBevelBorder();//創建一個具有凸出斜面邊緣的邊框
Border border5 = BorderFactory.createEtchedBevelBorder();//創建一個具有“浮雕化”外觀效果的邊框,將組件的當前背景色用於突出顯示和陰影顯示
*/
//設置分割線Jseparator
JSeparator separator1 = new JSeparator();
//設置組件的顏色
Color colors = new Color(9,255,0);
//設置組件的字體
Font fon = new Font("ja",Font.BOLD,24);
//設置光標java.awt.Cursor
//設置微調器JSpinner
JLabel Label_age = new JLabel("age");
JSpinner jsp = new JSpinner();
jsp.setValue(new Integer(20));
pan.add(Label_age);
pan.add(jsp);
//設置button
JButton butt_sub = new JButton("submit");
pan.add(butt_sub);
JButton butt_save = new JButton("save");
pan.add(butt_save);
//設置JTextArea
JLabel Label_result = new JLabel("result");
JTextArea result = new JTextArea(10,30);
JScrollPane sp = new JScrollPane(result);
pan.add(Label_result);
pan.add(sp);
//設置進度條ProgressBar
JProgressBar progbar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
progbar.setStringPainted(true);
pan.add(progbar);
//添加到容器
con.add(pan);
f.setVisible(true);
}
}