javaSE基礎27之圖形用戶界面

第一節 Swing簡介

 

1,Swingjava的一個圖形框架,組件,還有佈局管理器

2,Swing主要涉及到容器,組件,還有佈局管理器

3,Swing與用戶交互的時候還涉及到事件概念

 

第二節 JFrame 容器

 

1,public void setVisible(boolean b)根據參數b的值顯示或隱藏此窗體

2,public void setSize(int width,int height) 調整組件的大小,使其寬度爲width高度爲height

3,public void setLocation(int x,int y)將組件移到新位置。通過此組件父級座標空間中的xy參數來指定新位置的左上角

4,public Container getContentPane() 返回此窗體的contentPane 對象

public void setBackground(Color c)設置組件的背景顏色

public class JFrameTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        /*Container cp=jf.getContentPane();
        cp.setBackground(Color.BLUE);//設置背景顏色*/
        jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
    }
    

}

第三節 JButton組件

 public class JButtonTest {
    public static void main(String args[]){
         JFrame jf=new JFrame("JButton測試");
         JButton jb=new JButton("這是一個按鈕");
         jf.add(jb);
        //jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

第四節 Swing佈局管理器

1,FlowLayout 流式佈局

使用此種佈局方式會使所有組件像流水一樣依次進行排列

 public class FlowLayoutTest {
    public static void main(String args[]){
        JFrame jf=new JFrame("FlowLayout測試");
        //jf.setLayout(new FlowLayout());
        //jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setLayout(new FlowLayout(FlowLayout.LEFT,15,15));
        JButton jb=null;
        for(int i=1;i<9;i++){
            jb=new JButton("按鈕"+i);
            jf.add(jb);
        }
        //jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

2,BorderLayout

使用此種佈局方式將一個窗體的版面劃分成東,西,南,北,中5個區域,可以直接將需要的組件放到這5個區域中

 public class BorderLayoutTest {
    public static void main(String args[]){
        JFrame jf=new JFrame("BorderLayout測試");
       // jf.setLayout(new BorderLayout());
        jf.setLayout(new BorderLayout(5,5));
        jf.add(new JButton("東"),BorderLayout.EAST);
        jf.add(new JButton("西"),BorderLayout.WEST);
        jf.add(new JButton("南"),BorderLayout.SOUTH);
        jf.add(new JButton("北"),BorderLayout.NORTH);
        jf.add(new JButton("中"),BorderLayout.CENTER);
        
        //jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

3,GridLayout表格佈局

使用此種佈局方式是以表格形式進行佈局管理的,在使用此佈局管理器時必須設置顯示的行數和列數

 public class GridLayoutTest {
    public static void main(String args[]){
        JFrame jf=new JFrame("GridLayout測試");
       
        jf.setLayout(new GridLayout(4,5,5,5));
        JButton jb=null;
        for(int i=1;i<20;i++){
            jb=new JButton("按鈕"+i);
            jf.add(jb);
        }
        
        //jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

4,絕對佈局

public class AbsoluteLayout {
     public static void main(String args[]){
        JFrame jf=new JFrame("AbsoluteLayout測試");
       
        jf.setLayout(null);
        JButton jb1=new JButton("按鈕1");
        JButton jb2=new JButton("按鈕1");
        jb1.setBounds(20, 10, 200, 40);
        jb2.setBounds(100, 50, 100, 100);
        jf.add(jb1);
        jf.add(jb2);
        
        //jf.getContentPane().setBackground(Color.red);
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 500);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

}

第五節 JLabel組件

 public class JLabelTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame jf=new JFrame("JLabel測試");
        JLabel jl=new JLabel("JLabel組件",JLabel.CENTER);
        jf.add(jl);
        
        jf.setBackground(Color.yellow);
        jf.setBounds(500, 200, 400,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}

第六節 文本框組件

1,JTextField文本框

2,JPassWordField 密碼框

3,JTextArea文本域

 public class JTextFieldTest {
    public static void main(String args[]){
        JFrame jf=new JFrame("JTextField測試");
        jf.setLayout(new GridLayout(1,2,20,20));
        JLabel jb=new JLabel("用戶名");
        JTextField jtf=new JTextField();
        jf.add(jb);
        jf.add(jtf);
        
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 60);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

}

》》》》》》》》》》》》

public class JPassWordFieldTest {
    public static void main(String args[]){
        JFrame jf=new JFrame("JPassWordField測試");
        jf.setLayout(new GridLayout(2,2,20,20));
        JLabel jb=new JLabel("用戶名");
        JTextField jtf=new JTextField();
        JLabel jb2=new JLabel("密碼");
        JPasswordField jpw=new JPasswordField();
        jf.add(jb);
        jf.add(jtf);
        jf.add(jb2);
        jf.add(jpw);
        
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 120);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

》》》》》》》》》》》》

public class JTextAreaTest1 {
    public static void main(String args[]){
        JFrame jf=new JFrame("JTextArea測試");
        jf.setLayout(new GridLayout(1,2,20,20));
        JLabel jb=new JLabel("描述:");
        JTextArea jta=new JTextArea();
        
        jf.add(jb);
        jf.add(jta);
        
        jf.setLocation(300,200);//設置容器位置
        jf.setSize(300, 100);//設置容器大小
        jf.setVisible(true);//讓容器顯示
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}


第七節 JPanel輕量級容器

public class JPanelTest {
      public static void main(String[] args) {
        JFrame jf=new JFrame("JPanel測試");
        JPanel jp=new JPanel();
        jp.setLayout(new GridLayout(3,2,10,10));
        jp.setBorder(new EmptyBorder(10,10,10,10));//設置邊距
        jf.add(jp);
        JLabel jb=new JLabel("用戶名");
        JTextField jtf=new JTextField();
        JLabel jl=new JLabel("密碼");
        JPasswordField jpw=new JPasswordField();
        JButton jb1=new JButton("sign");
        JButton jb2=new JButton("exit");
        jp.add(jb);
        jp.add(jtf);   
        jp.add(jl);
        jp.add(jpw);
        jp.add(jb1);
        jp.add(jb2);
        
        jf.setBackground(Color.yellow);
        jf.setBounds(500, 200, 400,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

第八節 Swing事件處理


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
class JButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
        System.out.println(e.getActionCommand());
            JOptionPane.showMessageDialog(null, "我被點擊了");
        }
}
public class EventTest {

    /**
     * @param args the command line arguments
     */
     
    public static void main(String[] args) {
        JFrame jf=new JFrame("Swing 事件測試");
        JButton jb=new JButton("按鈕");
        JButtonListener jbl=new JButtonListener();
        jb.addActionListener(jbl);//註冊事件監聽
        jf.add(jb);
        
        jf.setBackground(Color.yellow);
        jf.setBounds(500, 200, 400,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   

}

》》》》》》》》》》》》》》》

import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
class MyWindowListener implements WindowListener {

    @Override
    public void windowOpened(WindowEvent e) {
        
        System.err.println("窗口被打開");

    }

    @Override
    public void windowClosing(WindowEvent e) {
       System.err.println("窗口關閉");
    }
    @Override
    public void windowClosed(WindowEvent e) {
        System.err.println("窗口被關閉");
    }

    @Override
    public void windowIconified(WindowEvent e) {
       System.err.println("窗口最小化");
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
       System.err.println("窗口從最小化回覆");
    }

    @Override
    public void windowActivated(WindowEvent e) {
        System.err.println("窗口被選中");
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
       System.err.println("窗口選中被取消");
    }
    
}
public class EventTest2 {

    /**
     * @param args the command line arguments
     */
     
    public static void main(String[] args) {
        JFrame jf=new JFrame("Swing 事件測試");
        MyWindowListener myWindowListener=new MyWindowListener();
        jf.addWindowListener(myWindowListener);
        
        jf.setBackground(Color.yellow);
        jf.setBounds(500, 200, 400,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   

}

》》》》》》

class MyWindowAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent e){
        super.windowClosing(e);
        System.out.println("窗口關閉......");
    }
}
public class EventTest3 {
    public static void main(String[] args) {
        JFrame jf=new JFrame("Swing 事件測試");
        MyWindowAdapter mw=new MyWindowAdapter();
        jf.addWindowListener(mw);
        jf.setBackground(Color.yellow);
        jf.setBounds(500, 200, 400,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章