1、AWT組件開發java
一、AWTdocker
AWT是抽象窗口工具箱的縮寫,它爲編寫圖形用戶界面提供了用戶接口,經過這個接口就能夠繼承不少方法,省去了不少工做。AWT還能使應用程序更好地同用戶進行交互。編程
AWT中的容器是一種特殊的組件,他能夠包含其餘組件,便可以把組件方法容器中。Container類是用來存放其餘組件的Component類的子類,Frame類又是Component的子類。Frame類用於建立具備標題欄和邊界的窗口。這裏經過繼承Frame類來創建本身的界面。瀏覽器
- public class test extendsFrame{
-
- public test() throws HeadlessException {
- this.setTitle("第一個窗口程序");
-
- this.setBounds(100, 100, 250, 250);
-
- this.setVisible(true);
- }
- public static void main(String[] str) {
- new test();
- }
- }
上面是窗口中一些必不可少的東西,下面是一些窗口的基礎應用:網絡
>setTitle(「窗口」):定義窗口名稱框架
>add(button):把按鈕添加到窗口中less
>setBackground(Color):設置窗口的背景顏色jsp
>setResizable(boolean):設置窗口大小是否能夠改變ide
>setAlwaysOnTop(boolean):設置窗口是否總在最上面函數
>setBounds(x, y, w, h):設置窗口起始位置和大小
>setVisible(boolean):設置窗口可見
若是想建立多個窗口,只須要在主方法main()中建立多個對象就好了。
二、佈局管理器
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 cl = new CardLayout();
- public test() throws HeadlessException {
- this.setTitle("CardLayout佈局管理器");
- this.setLayout(null);
- this.add(p);
-
- p.setLayout(cl);
-
- 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();
- }
三、組件和監聽接口
組件和監聽接口是分不開的。組件和監聽接口在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);
- }
-
- 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監聽文本的輸入、刪除等操做,就像記事本同樣能夠對文本進行修改、錄入。
2、Swing界面編程
隨着Java的發展,AWT已經漸漸被淘汰,它已經不能適應發展的須要,不能知足開發功能強大的用戶界面的須要。這時Swing出現了,它是創建在AWT之上的組件集,在不一樣的平臺上都能保持組件的界面樣式,所以獲得了很是普遍的應用。
一、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組件的行爲和外觀,訪問技術等。
二、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();
- }
- }
三、經過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();
- }
- }
四、按鈕
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");
- }
- }
五、複選框
在Swing中,用JcheckBox類來定義複選框。複選框和單選按鈕有點相似,可是一組複選框中能夠有任何數量的複選框被選中。複選框能夠爲每一次的單擊操做添加一個事件。但日常只會監聽事件,由於它們讓客戶來肯定該單擊操做時選中仍是取消選中複選框。
複選框又被稱爲檢測盒。JcheckBox提供選中/未選中兩種狀態,當用戶單擊複選框時改變複選框原來設置的狀態。
六、彈出式菜單
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);
-
- this.jiaPopMenu();
- }
- public void jiaPopMenu(){
- JMenuItem item = newJMenuItem("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的對象來將菜單激活,當右擊時便可彈出菜單。
七、單選按鈕
單選按鈕的實質就是在一組按鈕中一次只能有一個按鈕被選中。單選按鈕的外觀相似複選框,可是複選框沒有對能夠選擇的選項數目進行限制。對於每一組的單選按鈕,必須建立一個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);
-
- this.Add();
- }
- public void Add(){
-
- bg.add(r1);
- bg.add(r2);
-
- this.add(r1);
- this.add(r2);
-
- r2.setSelected(true);
- }
- public static void main(String[] args){
- new test();
- }
- }
八、下拉列表框
下拉列表框可讓人感受到整個界面很簡潔,在大的網頁中都能感受到這一點。列表能夠有許多選項,因此它們一般被放置在一個滾動窗格中。組合框與下拉列表框類似,區別在於使用組合框時用戶能夠不從列表中選擇項目,還能夠選擇一個項目。在某些版本的組合框中,還能夠輸入本身的選擇,如瀏覽器的地址欄。
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);
-
- 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++) {
-
- str[i] = "選項" + i;
- }
-
-
-
-
-
-
- jcb = new JComboBox(str);
- this.add(jcb);
- }
- public static void main(String[] args){
- new test();
- }
- }
九、選項卡
當今博客盛行的時代,選項卡常常被用到。在博客中,用戶常常會改變北京樣式,用不一樣的風格體現個性。而這些徹底能夠用選項卡來製做。
使用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);
-
- 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();
- }
- }
十、滑桿
在應用程序中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);
-
- 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() +"");
- }
- }
十一、滾動條
在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);
-
- 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();
- }
- }
十二、列表框
列表框是一個很是有用的組件,也愈來愈受到重視。尤爲是它具備多選能力,在選擇選項時能夠按住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);
-
- 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();
- }
- }
1三、菜單
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();
-
- JMenu jm1 = new JMenu("文件");
- JMenu jm2 = new JMenu("編輯");
- JMenu jm3 = new JMenu("新建");
-
- 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);
-
- 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