一、AWT組件開發
1、AWT
AWT是抽象窗口工具箱的縮寫,它為編寫圖形用戶界面提供了用戶接口,通過這個接口就可以繼承很多方法,省去了很多工作。AWT還能使應用程序更好地同用戶進行交互。
AWT中的容器是一種特殊的組件,他可以包含其他組件,即可以把組件方法容器中。Container類是用來存放其他組件的Component類的子類,Frame類又是Component的子類。Frame類用於創建具有標題欄和邊界的窗口。這里通過繼承Frame類來建立自己的界面。
- public class test extendsFrame{
- //創建構造器
- public test() throws HeadlessException {
- this.setTitle("第一個窗口程序");
- //x , y是距離顯示器左上角的距離;w , h是窗口的長寬
- this.setBounds(100, 100, 250, 250);
- //或者使用以下方式代替上面的一句代碼
- // this.setLocation(100, 100);
- // this.setSize(250 , 250);
- this.setVisible(true);
- }
- public static void main(String[] str) {
- new test();
- }
- }
上面是窗口中一些必不可少的東西,下面是一些窗口的基礎應用:
>setTitle(“窗口”):定義窗口名稱
>add(button):把按鈕添加到窗口中
>setBackground(Color):設置窗口的背景顏色
>setResizable(boolean):設置窗口大小是否可以改變
>setAlwaysOnTop(boolean):設置窗口是否總在最上面
>setBounds(x, y, w, h):設置窗口起始位置和大小
>setVisible(boolean):設置窗口可見
如果想創建多個窗口,只需要在主方法main()中創建多個對象就行了。
2、布局管理器
Java中的圖形界面在布局管理上采用容器和布局管理相分離的方案,也就是說容器只是把組件放進來,但它不管怎樣放置。至於如何放置需要用到布局管理器。Java中有幾種布局管理器,分別是:FlowLayout , BorderLayout, GridLayout和GardLayout。
1)、FlowLayout
FlowLayout布局管理器是默認的布局管理器,它將組件按照從左到右、從上到下的順序來安排,並在默認情況下使組件盡量居中放置。下面的代碼要添加到test()類中:
this.setLayout(new FlowLayout());
//將按鈕組件放入容器中
this.add(new Button("確定"));
this.add(new Button("取消"));
大家可以通過創建許多按鈕來觀察FlowLayout對組件的擺放規律,下面是使用一個按鈕監聽事件來實現按鈕的添加:
- public class test extendsFrame implements ActionListener{
- int i;
- Button b = new Button("Add");
- public test() throws HeadlessException {
- this.setTitle("第一個窗口程序");
- this.setLayout(new FlowLayout());
- this.add(b);
- b.addActionListener(this);
- this.setBounds(100, 100, 250, 250);
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- i++;
- Button bi = newButton("Button" + i);
- this.add(bi);
- this.setVisible(true);
- }
- public static void main(String[] str) {
- new test();
- }
- }
在本程序中,由於按鈕的大小不同,其整體看起來不太整齊,但這更說明了FlowLayout布局管理器的規則。
2)、BorderLayout
BorderLayout布局管理器只允許在容器內放置5個組件,這5個組件的位置是由BorderLayout類中的North、South、East、West和Center5個常量來確定的,他們對應着容器中的上下左右中,用法如下:
this.add(new Button(“按鈕”) ,BorderLayout.NORTH);
this.add(new Button(“按鈕”) ,BorderLayout.CENTER);
組件在BorderLayout中的大小都是可以改變的。一般情況下可以讓中間區域大一些,而且可以只用其中幾個區域。
3)、GridLayout
GridLayout布局管理器是矩形網格,在網格中放置組件,每個網格的高度和寬度都相等,組件的排列順序與FlowLayout相同。組件隨着網格的大小而在水平和垂直方向上拉伸,網格的大小是由容器的大小和創建網格的多少來確定的。其用法如下:
this.setLayout(newGridLayout(2 , 3)); //創建一個2行3列的網格
this.add(new Button(“按鈕”));
當組件數目大於網格數時,GridLayout保持行數不變而自動增加列數。
4)、CardLayout
CardLayout運行在一個組件中每次只顯示一組組件中的某一個,用戶可以根據需要來選擇使用哪個組件。CardLayout類提供了如下選擇組件的方法。
>First(Container p):選擇容器中的第一個組件
>last(Container p):選擇容器中的最后一個組件
>next(Container p):選擇容器中當前組件的下一個組件
>prebvious(Container p):選擇容器中當前組件的上一個組件
>show(Container p ,String name):選擇容器中指定的組件
前面幾種很容易理解,而show()方法來選擇容器中指定的組件。它的第一個參數是管理組件的容器,第二個參數是標識要顯示組件字符串,該字符串與將組件添加到容器時使用的字符串相同。其用法如下:
- public class test extendsFrame implements ActionListener{
- Panel p = new Panel();
- Button bf = new Button("First");
- Button bl = new Button("Last");
- Button bn = new Button("next");
- Button bp = newButton("previous");
- Button bg = new Button("Go");
- TextField tf = new TextField();
- //設置CardLayout布局管理器
- CardLayout cl = new CardLayout();
- public test() throws HeadlessException {
- this.setTitle("CardLayout布局管理器");
- this.setLayout(null);
- this.add(p);
- //定義p面板為CardLayout布局管理器
- p.setLayout(cl);
- //為CardLayout布局管理器添加按鈕
- for (int i = 1; i <= 10; i++) {
- Button btemp = newButton("Button" + i);
- p.add(btemp, "" + i);
- }
- //為每個組件設置大小位置並添加到容器中
- p.setBounds(10, 40, 100, 100);
- this.add(bf);
- bf.addActionListener(this);
- bf.setBounds(120, 40, 60, 20);
- this.add(bl);
- bl.addActionListener(this);
- bl.setBounds(120, 70, 60, 20);
- this.add(bn);
- bn.addActionListener(this);
- bn.setBounds(120, 100, 60, 20);
- this.add(bp);
- bp.addActionListener(this);
- bp.setBounds(120, 130, 60, 20);
- this.add(bg);
- bg.addActionListener(this);
- bg.setBounds(60, 160, 40, 20);
- this.add(tf);
- tf.setBounds(20, 160, 40, 20);
- //設置窗口的位置和大小
- this.setBounds(200, 200, 210, 220);
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == bn) {
- cl.next(p);
- }
- if (e.getSource() == bp) {
- cl.previous(p);
- }
- if (e.getSource() == bf) {
- cl.first(p);
- }
- if (e.getSource() == bl) {
- cl.last(p);
- }
- if (e.getSource() == bg) {
- cl.show(p, tf.getText().trim());
- tf.setText("");
- }
- }
- public static void main(String[] args){
- new test();
- }
3、組件和監聽接口
組件和監聽接口是分不開的。組件和監聽接口在AWT中有很多,我們只對復雜的、難以理解的進行講解,其他的大家可以通過API自行學習。
1)、按鈕和ActionListener
ActionListener是由處理ActionEvent事件的監聽器對象實現的。當單擊按鈕、在文本區域中按回車鍵、選擇菜單項、雙擊列表項都會觸發監聽器。該接口中的方法為:
public voidactoinPerformed(ActionEvent e)
下面是一個實現ActionListener接口的實例:
- public class mytest01extends Frame implements ActionListener{
- Button b1 = new Button("進入社區");
- Button b2 = new Button("退出");
- public mytest01() throws HeadlessException{
- this.setTitle("論壇");
- this.add(b1);
- this.add(b2 , BorderLayout.SOUTH);
- //為按鈕添加監聽
- b1.addActionListener(this);
- b2.addActionListener(this);
- this.setBounds(100, 100, 200, 300);
- this.setVisible(true);
- }
- //實現ActionListener接口中的方法
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == b1) {
- System.out.println("進入社區");
- }
- else{
- System.out.println("退出");
- }
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
2)、運用WindowListener
WindowListener是處理WindowEvent事件的對象實現的。這個監聽器確定了窗口設么時候被打開、關閉、激活、不激活、最小化和最大化。該接口中的方法有:
>public voidwindowActivated(WindowEvent e)
>public voidwindowClosed(WindowEvent e)
>public voidwindowClosing(WindowEvent e)
>public voidwindowDeactivated(WindowEvent e)
>public voidwindowDeiconfied(WindowEvent e)
>public void windowIconified(WindowEvente)
>public voidwindowOpened(WindowEvent e)
這些接口很容易但是有點繁瑣,下面的實例程序非常清楚:
- public class mytest01extends Frame implements WindowListener{
- public mytest01() throws HeadlessException{
- this.setTitle("WindowListener");
- this.addWindowListener(this);
- this.setBounds(100, 100, 200, 300);
- this.setVisible(true);
- }
- //實現接口中的方法
- public void windowOpened(WindowEvent e) {
- System.out.println("打開");
- }
- public void windowClosing(WindowEvent e) {
- System.out.println("菜單關閉");
- this.dispose();
- }
- public void windowClosed(WindowEvent e) {
- System.out.println("釋放");
- }
- public void windowIconified(WindowEvent e){
- System.out.println("最小化");
- }
- public void windowDeiconified(WindowEvente) {
- System.out.println("最大化");
- }
- public void windowActivated(WindowEvent e){
- System.out.println("激活");
- }
- public void windowDeactivated(WindowEvente) {
- System.out.println("失去焦點");
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
3)、文本組件和TextListener
文本組件就像把窗口空白處看成記事本一樣,它是一個用來寫內容的組件。TextListener用來確定何時文本值改變。該接口還可以用到很多地方,其接口方法如下:
public voidtextValueChanged(TextEvent e)
下面通過實例來了解TextListener監聽事件的使用
- public class mytest01extends Frame implements TextListener{
- TextArea ta = newTextArea("saasdfgadsfg");
- public mytest01() throws HeadlessException{
- this.setTitle("textArea");
- this.add(ta);
- //設置文字樣式
- Font f = new Font("宋體", Font.ITALIC+ Font.BOLD, 50);
- ta.setFont(f);
- ta.addTextListener(this);
- ta.setForeground(Color.red);
- this.setBounds(100, 100, 300, 300);
- this.setVisible(true);
- }
- @Override
- public void textValueChanged(TextEvent e) {
- System.out.println(ta.getText());
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
上面的這段程序使用TextListener監聽文本的輸入、刪除等操作,就像記事本一樣可以對文本進行修改、錄入。
二、Swing界面編程
隨着Java的發展,AWT已經漸漸被淘汰,它已經不能適應發展的需要,不能滿足開發功能強大的用戶界面的需要。這時Swing出現了,它是建立在AWT之上的組件集,在不同的平台上都能保持組件的界面樣式,因此得到了非常廣泛的應用。
1、Swing組件庫
在Swing組件中有許多種組件,它們被封裝在JFC中,下面我們會對每一種組件進行詳細介紹。Swing包很多,但平常用到的只有javax.swing.*和javax.swing.event.*這兩個包,其他的很少用到。
1)、JFC結構
JFC是Java的基礎類,是Java Foundation Classes的縮寫形式,封裝了一組用於構建圖形用戶界面的組件和特性。JFC包含了圖形用戶界面構建中需要用到的頂級容器(Applet、Dialog、Frame)、普通容器(面板、滾動面板、拆分窗格組件、選項卡插U能給個和工具條等)、特殊容器(InternalFrame、Layeredpane、root pane)、基本組件(button , combo box , list , menu , slider , spinner和textfild)等。
2)、與AWT的區別
最大的區別在於Swing組件的實現與本地實現無關。Swing組件比AWT組件具有更多的功能。例如在Swing中添加了按鈕組件和標簽組件,通過繼承來更改Swing組件的行為和外觀,訪問技術等。
2、Jfram窗口容器
它定義了一個UI程序的框架,是圖形程序不可缺少的一部分。它與java.awt.Frame類很類似,它是RootPaneContainer的一種,是頂層的Swing容器。通過繼承jframe,所構建的容器就有一些最基本的組件。例如和關閉按鈕相對應的容器有一個setDefaultCloseOperation(int operation)方法,這是每個Swing程序都有的,它有四個參數:
>DO_NOTHING_ON_CLOSE(單擊后不管用)
>HIDE_ON_CLOSE(單擊后窗口隱藏)
>DISPOSE_ON_CLOSE(單擊后窗口釋放)
>EXIT_ON_CLOSE(單擊后退出)
Jframe是最重要的頂層容器,其顯示效果是一個窗口,帶有標題和尺寸重置角標。可以在其中添加按鈕、標簽等組件。下面是一個應用JFrame類的基本程序:
- public class test extendsJFrame{
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args){
- new test();
- }
- }
3、通過Icon接口進行圖像操作
Icon接口用於顯示圖像。在Icon接口中定義了許多方法,這些方法都是圖像應用方面必不可少的。
在Swing組件中支持圖像顯示。ImageIcon類用來描述圖像。創建Icon對象的方法有3種:
>new ImageIcon(Image i)
>new ImageIcon(Stringfilename)(參數為圖像文件的名稱)
>new ImageIcon(URL u)(參數為網絡地址)
實現Icon接口的方法也有3種:
>PaintIcon(Graphics)
>getIconWidth()(設置圖像寬度)
>getIconHeight()(設置圖像長度)
JLable是一種既可以包含文本又可以包含圖像的控件,該控件不能響應用戶的動作。創建一個JLable的方法如下:
Jlabel j1 = new JLable(“a”);
this.add(j1);
Icon組件可以實現帶有圖標的按鈕或標簽。Icon是一個固定大小的圖片,通常用於裝飾組件。下面是一個Icon的實例應用:
- public class test extendsJFrame{
- JLabel j1 = new JLabel("a");
- JLabel j2 = new JLabel("b");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.add(j1);
- this.add(j2);
- //引入原有圖片
- ImageIcon i1 = new ImageIcon("D:\\桌面\\桌面\\安卓開發工具\\素材\\圖標\\a1.png");
- j1.setIcon(i1);
- //引入自做圖片
- Icon i2 = new MyIconlmp();
- j2.setIcon(i2);
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- //定義一個揮之圖片的類
- class MyIconlmp implements Icon{
- public void paintIcon(Component c,Graphics g, int x, int y) {
- for (int i = 0; i < 3; i++) {
- g.setColor(new Color(30*i,50*i, 60*i));
- g.fillOval(10, 10 + 100*i, 120,80);
- }
- }
- //設置圖片寬度
- public int getIconWidth() {
- return 200;
- }
- //設置圖片高度
- public int getIconHeight() {
- return 200;
- }
- }
- public static void main(String[] args){
- new test();
- }
- }
4、按鈕
JButton類用來定義按鈕。JButton類的常用方法如下:
>addActionListener():注冊點擊事件監聽器;
>setText():設置按鈕文字;
>setIcon():設置按鈕圖標。
通過組建的setMnemonic()方法可以設置組件Mnemonic助記符。通過組件的setTipText可以設置組建的ToolTip提示信息。
在Swing中,JButton是有AbstractButton類派生的。AbstractButton類派生了兩個組件:JButton和JtoggleButton組件。JButton是Swing的按鈕,而ToggleButton組件是單選按鈕和復選框的基類。下面是一個按鈕的應用程序。
- public class test extendsJFrame implements ActionListener{
- JButton jb = new JButton("Clickme!");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.add(jb);
- jb.addActionListener(this);
- jb.setMnemonic('b');//給按鈕設置組件助記符
- jb.setToolTipText("I am abutton");
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args){
- new test();
- }
- @Override
- public void actionPerformed(ActionEvent e){
- this.jb.setText("Clicked");
- }
- }
5、復選框
在Swing中,用JcheckBox類來定義復選框。復選框和單選按鈕有點類似,但是一組復選框中可以有任何數量的復選框被選中。復選框可以為每一次的單擊操作添加一個事件。但平常只會監聽事件,因為它們讓客戶來確定該單擊操作時選中還是取消選中復選框。
復選框又被稱為檢測盒。JcheckBox提供選中/未選中兩種狀態,當用戶單擊復選框時改變復選框原來設置的狀態。
6、彈出式菜單
JPopupMenu是一種Menu的組件,因此在使用JPopupMenu時都需要一個Container來放置JPopupMenu。
通過JpopupMenu類可以定義彈出式菜單,其重要方法有:
>add(JmenuItem e):(往菜單中增加菜單項)
>show():(顯示菜單)
通過JMenuItem類來定義菜單項,通過addActionListener()為菜單項增加事件處理。
JpopupMenu是一個可彈出並顯示一系列選項的小窗口,可用於用戶在菜單欄上選擇選項時顯示菜單,還可以用於當用戶選擇菜單項並激活時顯示“右拉式(pull-right)“菜單。通過下面的程序進一步了解相關的知識:
- public class test extendsJFrame {
- //定義一個彈出式菜單
- JPopupMenu jpm = new JPopupMenu();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //引入jiaPopMenu()方法
- this.jiaPopMenu();
- }
- public void jiaPopMenu(){
- JMenuItem item = newJMenuItem("A");
- //為A添加監聽事件
- item.addActionListener(newActionListener() {
- public voidactionPerformed(ActionEvent e) {
- System.out.println("AClicked!!!");
- }
- });
- jpm.add(item);//將彈出項添加到菜單
- //定義菜單項
- item = new JMenuItem("B");
- item.addActionListener(newActionListener() {
- public voidactionPerformed(ActionEvent e) {
- System.out.println("BClicked");
- }
- });
- jpm.add(item);
- //編寫右鍵彈出單擊事件
- this.addMouseListener(newMouseAdapter() {
- public voidmouseReleased(MouseEvent e) {
- if (e.isPopupTrigger()) {
- jpm.show(e.getComponent(),e.getX(), e.getY());
- }
- }
- });
- }
- public static void main(String[] args){
- new test();
- }
- }
使用JPopupMenu組件實現右鍵快捷菜單功能,並使用ActionListener的對象來將菜單激活,當右擊時即可彈出菜單。
7、單選按鈕
單選按鈕的實質就是在一組按鈕中一次只能有一個按鈕被選中。單選按鈕的外觀類似復選框,但是復選框沒有對可以選擇的選項數目進行限制。對於每一組的單選按鈕,必須創建一個ButtonGroup對象實例並且將每一個單選按鈕添加到該ButtonGroup對象中。下面的實例講解的單選按鈕的基本用法:
- public class test extendsJFrame {
- //定義兩個單選選項
- JRadioButton r1 = newJRadioButton("No.1");
- JRadioButton r2 = newJRadioButton("No.2");
- //定義一個按鈕組
- ButtonGroup bg = new ButtonGroup();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(3, 1));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //引入jiaPopMenu()方法
- this.Add();
- }
- public void Add(){
- //將單選按鈕加入按鈕組中
- bg.add(r1);
- bg.add(r2);
- //將單選按鈕加入布局管理器
- this.add(r1);
- this.add(r2);
- //默認選中r2
- r2.setSelected(true);
- }
- public static void main(String[] args){
- new test();
- }
- }
8、下拉列表框
下拉列表框可以讓人感覺到整個界面很簡潔,在大的網頁中都能感覺到這一點。列表可以有許多選項,所以它們通常被放置在一個滾動窗格中。組合框與下拉列表框相似,區別在於使用組合框時用戶可以不從列表中選擇項目,還可以選擇一個項目。在某些版本的組合框中,還可以輸入自己的選擇,如瀏覽器的地址欄。
JComboBox方法很多,它們主要用來管理列表中的數據:
>addItem():添加一個項目到JComboBox
>get/setSelectedIndex():獲取/設置JComboBox中選中項目的索引
>get/setSelectedItem():獲取/設置選中的對象
>removeAllItems():從JComboBox刪除所有對象
>remoteItem():從JComboBox刪除特定對象
下面是一個下拉別表框的實例,從中可以更詳細的了解到下拉列表框的使用:
- public class test extendsJFrame {
- //定義下啦菜單
- JComboBox jcb = new JComboBox();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(3, 1));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- String[] str = new String[10];
- for (int i = 0; i < 10; i++) {
- //jcb.addItem("此方法直接將內容添加進列表中" +i);
- str[i] = "選項" + i;
- }
- //或者使用Vector來作為item
- //Vector v = new Vector();
- //v.add("選項1");
- //v.add("選項2");
- //v.add("選項3");
- //jcb = new JComboBox(v);
- jcb = new JComboBox(str);
- this.add(jcb);
- }
- public static void main(String[] args){
- new test();
- }
- }
9、選項卡
當今博客盛行的時代,選項卡經常被用到。在博客中,用戶經常會改變北京樣式,用不同的風格體現個性。而這些完全可以用選項卡來制作。
使用JTabbedPane類可以把幾個組件放在一個組件中,如面板。用戶可以通過選擇對應於目標組件的選項卡來選擇要查看的組件。要創建一個選項卡窗格,只要實例化一個JTabbedPane對象,創建要顯示的租金啊,然后再將這些組件添加到選項卡窗格中即可。
當創建一個要添加到選項卡窗格的組件時,無論當前可見的是哪個子選項卡,每一個子選項都將獲得相同的顯示空間。只不過當前顯示窗格的高度會比其他窗格稍高一點,以進行區分。下面是一個應用實例:
- public class test extendsJFrame {
- //定義選項卡
- JTabbedPane jtb = newJTabbedPane(JTabbedPane.BOTTOM);
- //定義選項
- JPanel jp1 = new JPanel();
- JPanel jp2 = new JPanel();
- JPanel jp3 = new JPanel();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 1));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- //設置選項事件
- jp1.setBackground(Color.green);
- jp2.setBackground(Color.red);
- jp3.setBackground(Color.BLUE);
- this.add(jtb);
- //將選項加入選項卡中
- jtb.add("綠色背景", jp1);
- jtb.add("紅色背景", jp2);
- jtb.add("藍色背景", jp3);
- }
- public static void main(String[] args){
- new test();
- }
- }
10、滑桿
在應用程序中JSlider支持數值變化。它是一種迅速而簡單的方式,不僅能讓用戶以可視形式獲得他們當前選擇的反饋,還能得到可以接受的值的范圍。
JSlider類定義了滑桿組件,它的重要方法有:
>setMaxorTickSpacing():設置主刻度
>setMinorTickSpacing():設置次刻度
>setPaintTicks():設置是否繪制刻度
>setPaintLabels():設置是否繪制標簽
>addChangeListener():刻度變化事件處理
>getValue():獲取當前滑塊位置值
>setValue():設置滑塊初始位置
下面是具體應用實例:
- public class test extendsJFrame implements ChangeListener{
- JSlider js = new JSlider();
- JLabel jl = new JLabel("20");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(4, 1));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- js.setMaximum(100);//最大刻度
- js.setMinimum(0);//最小刻度
- js.setOrientation(JSlider.HORIZONTAL);
- js.setValue(20);//設置滑桿初始位置
- js.setMajorTickSpacing(20);//設置主刻度
- js.setMinorTickSpacing(5);//次刻度
- js.setPaintTicks(true);//繪制刻度
- js.setPaintLabels(true);//繪制標簽
- this.add(js);
- this.add(jl);
- js.addChangeListener(this);
- }
- public static void main(String[] args){
- new test();
- }
- public void stateChanged(ChangeEvent e) {
- jl.setText(js.getValue() +"");
- }
- }
11、滾動條
在Swing中,組件中的內容超出其區域時,就需要使用滾動條。它和網頁的滾動條相似。JscrollPane的方法如下:
>getHorizontalScrollBar():返回水平的JscrollBar組件
>getVerticalScrollBar():返回垂直的JscrollBar組件
>get/setHorizontalScrollBarPolicy():Always、Never或As Needed
>get/setVerticalScrollBarPolicy():與水平函數相同
下面是對方法的演示:
- public class test extendsJFrame {
- JLabel jl = new JLabel();
- JScrollPane jsp = new JScrollPane(jl);
- JScrollBar jsb =jsp.getVerticalScrollBar();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 2));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- this.add(jsp);
- jl.setIcon(new ImageIcon("D:\\桌面\\桌面\\image062_s.jpg"));
- //監聽滾動條的滾動
- jsb.addAdjustmentListener(newAdjustmentListener() {
- @Override
- public voidadjustmentValueChanged(AdjustmentEvent e) {
- System.out.println(jsb.getModel() + "");
- }
- });
- System.out.println("處理滾動條的四個基本屬性的數據模型:minimum、maximum、value 和extent。" + jsb.getModel());
- }
- public static void main(String[] args){
- new test();
- }
- }
12、列表框
列表框是一個非常有用的組件,也越來越受到重視。尤其是它具有多選能力,在選擇選項時可以按住Shift鍵進行多選。
JList是一個有用的組件,其類似於一組復選框或一組單選按鈕。通過設置,允許對列表框中的項目進行多項選擇。JList的方法如下:
>getSelectedIndex():獲取選中的第一個索引
>getSelectionMode():設置單項選擇還是多項選擇
>getListData():設置在列表框中使用數據的模型
>getSelectedValue():獲取選中的第一個值
列表框經常與滾動條搭配,因為如果沒有滾動條,列表框中的內容可能無法完全顯示,下面是一個實例:
- public class test extendsJFrame {
- //定義列表框
- JList jl = new JList(new String[]{"北京" , "天津" , "上海" , "大連" , "青島" , "武漢" , "西安"});
- JScrollPane jsp = new JScrollPane(jl);
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(4, 2));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- this.add(jsp);
- }
- public static void main(String[] args){
- new test();
- }
- }
13、菜單
JMenu、JMenuItem和JMenuBar組件是在JFrame中開發菜單系統的主要構造塊。任何菜單系統的基礎都是JMenuBar。它們就像Word中的文件、編輯一樣,下面還有新建、復制、粘貼等一系列內容。其方法如下:
JMenuItem和JMenu:
>get/setAccelerator():獲取/設置快捷鍵
>get/setText():獲取/設置菜單的文本
>get/setIcon():獲取/設置菜單使用的圖片
JMenu專用:
>add():將JMenu或JMenuItem添加到JMenuBar或JMenu中。
JMenu組件使用來存放和整合JMenuItem的組件,也是構成一個菜單不可缺少的組件之一。實現包含JMenuItem的彈出窗口,用戶選擇JMenuBar上的選項時會顯示該JMenuItem。下面是一個關於菜單的應用實例:
- public class test extendsJFrame {
- //定義菜單條組件
- JMenuBar jmb = new JMenuBar();
- //定義3個菜單組件
- JMenu jm1 = new JMenu("文件");
- JMenu jm2 = new JMenu("編輯");
- JMenu jm3 = new JMenu("新建");
- //定義3個菜單項組件
- JMenuItem jmi1 = newJMenuItem("word");
- JMenuItem jmi2 = new JMenuItem("復制");
- JMenuItem jmi3 = new JMenuItem("粘貼");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 1));
- this.setBounds(10, 10, 600, 400);
- //引入jiaPopMenu()方法
- this.Add();
- //設置當單機窗口的關閉按鈕時退出
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- //調整組件間的包含關系
- jmb.add(jm1);
- jmb.add(jm2);
- jm1.add(jm3);
- jm3.add(jmi1);
- jm2.add(jmi2);
- jm2.add(jmi3);
- this.setJMenuBar(jmb);
- }
- public static void main(String[] args){
- new test();
- }
- }
包含關系為:JmenuBar包含JMenu,JMenu可以包含JMenu和JMenuItem