實驗十四 Swing圖形界面組件java
一理論部分編程
1.Layout Manager(佈局管理器):佈局管理器是一組類,實現 java.awt.LayoutManager 接口,決定容器中組件的位置和大小。app
每一個容器都有與之相關的默認佈局管理器。框架
五種佈局管理器:函數
(1)FlowLayout: 流佈局(Applet和Panel的默認佈局管理器):從左到右,從上到下逐行擺放。工具
(2)BorderLayout:邊框佈局( Window、Frame和Dialog的默認佈局管理器):分上下左右中五個方位佈局
(3)GridLayout: 網格佈局學習
(4)GridBagLayout: 網格組佈局:允許組件擴展到多行、多列。測試
(5)CardLayout :卡片佈局:把組件象一系列卡片同樣疊放,一個時刻只能看到最上面的。字體
經過setLayout( )方法爲容器設置新的佈局。格式 :容器組件名.setLayout( 佈局類對象名)
a.FlowLayout (流佈局管理器)
– FlowLayout( ):生成一個默認的流式佈局對象
– FlowLayout(int align): 設定每一行組件的對齊方式(FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT)
– FlowLayout(int align,int hgap,int vgap):能夠設定組件間的水平和垂直距離(缺省時組件之間沒有空隙)
b.邊框佈局管理器是每一個JFrame的內容窗格的默認佈局管理器
向容器中加入組件時,若使用兩個參數的add()方法,第二個參數必須說明加入組件在容器中的放置位置;
位置參數是BorderLayout 類的常量:CENTER、NORTH、SOUTH、EAST、 WEST.
c.網格佈局按行列排列全部的組件;在網格佈局對象的構造器中,須要指定行數和列數:panel.setLayout(new GridLayout(6,10));
放置組件的每一個單元具備相同的尺寸。
添加組件,從第一行和第一列開始,而後是第一行的第二列。以此類推
d.GridLayout的構造函數以下:
(1)GridLayout():生成一個單行單列的網格佈局
(2)GridLayout(int rows,int cols):生成一個設定行數和列數的網格佈局
(3)GridLayout(int rows,int columns,int hgap,int vgap):能夠設置組件之間的水平和垂直間隔
2.文本域(JTextField) : 用於獲取單行文本輸入。
用於文本輸入的組件繼承於JTextComponent抽象類
3.文本區(JTextArea)組件可以讓用戶輸入多行文本。生成JTextArea組件對象時,能夠指定文本區的行數和列數:textArea = new JTextArea(8, 40);
4.文本區與文本域的異同:
相同之處:
文本域和文本區組件均可用於獲取文本輸入。
不一樣之處:
文本域只能接受單行文本的輸入;
文本區可以接受多行文本的輸入
5.標籤是容納文本的組件。它們沒有任何修飾(如沒有邊界 ),也不響應用戶輸入。
標籤的經常使用用途之一就是標識組件.
6.密碼域是一種特殊類型的文本域。每一個輸入的字符都用回顯字符實現,典型的回顯字符是 *。
7.Swing中文本區沒有滾動條,若須要滾動條。將文本區放入一個滾動窗格中便可.
8.複選框構造器
1.bold = new JCheckBox("Bold");複選框自動地帶有表示標籤。
2. JCheckBox(String label,Icon icon);構造帶有標籤與圖標的複選框,默認初始未被選擇。
3.JCheckBox(String label,boolean state);用指定的標籤和初始化選
9.單選按鈕的構造器:
1.JRadioButton(String label,Icon icon);建立一個帶標籤和圖標的單選按鈕
2.JRadioButton(String label,boolean state);用指定的標籤和初始化狀態構造單選按鈕
10.菜單是GUI編程中常常用到的一種組件。位於窗口頂部的菜單欄(menu bar)中包括下拉菜單的名字。點擊一個名字就能夠打開包含菜單項(menuitems)和子菜單(submenus)的菜單.
11.單選按鈕菜單項與普通單選按鈕的工做方式同樣,必須將它們加入的按鈕組中。當按鈕組中的一個按鈕被選中時,其它按鈕就自動變爲選擇項。
12.彈出菜單:建立一個彈出菜單與建立一個常規菜單的方法相似 ,可是彈出菜單沒有標題。
13.佈局管理器應用總結:
FlowLayout是 Applet 和麪板的缺省佈局管理器。組件從左上角到右下角進行排列。
BorderLayout 按北、南、東、西、中的不一樣區域劃分將組件排列於容器中。
GridLayout 將組件按行和列排列。全部組件大小相同。
GridBagLayout 能將組件放置在最精確的位置。各組件的大小能夠不一樣。
對話框是一種大小不能變化、不能有菜單的容器窗口;
對話框不能做爲一個應用程序的主框架,而必須包含在其它容器。
二:實驗部分。
1、實驗目的與要求
(1) 掌握GUI佈局管理器用法;
(2) 掌握各種Java Swing組件用途及經常使用API;
2、實驗內容和步驟
實驗1: 導入第12章示例程序,測試程序並進行組內討論。
測試程序1
l 在elipse IDE中運行教材479頁程序12-1,結合運行結果理解程序;
掌握各類佈局管理器的用法;
理解GUI界面中事件處理技術的用途。
在佈局管理應用代碼處添加註釋;
import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-06-12 * @author Cay Horstmann */ public class Calculator { public static void main(String[] args) { EventQueue.invokeLater(() -> { CalculatorFrame frame = new CalculatorFrame(); frame.setTitle("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
import javax.swing.*; /** * A frame with a calculator panel. */ public class CalculatorFrame extends JFrame { public CalculatorFrame() { add(new CalculatorPanel()); pack(); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A panel with calculator buttons and a result display. */ public class CalculatorPanel extends JPanel { private JButton display;//定義顯示Button組件對象 private JPanel panel; private double result;//定義基本數據對象 private String lastCommand; private boolean start;//布爾型:開始啓動爲ture public CalculatorPanel()//構造器 { setLayout(new BorderLayout());//邊框佈局管理器 result = 0; lastCommand = "="; start = true; // 添加顯示 display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH);//顯示在窗口上方 ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); //在一個4×4的網格中添加按鈕 panel = new JPanel(); panel.setLayout(new GridLayout(4, 4));//網格佈局管理器:4行4列 addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); add(panel, BorderLayout.CENTER);//顯示在窗口中心位置 } /** * Adds a button to the center panel. * @param label the button label * @param listener the button listener */ private void addButton(String label, ActionListener listener)//普通方法 { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); } /** * 此操做將按鈕操做字符串插入到顯示文本的末尾. */ private class InsertAction implements ActionListener// { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } /** * 該操做執行按鈕操做字符串表示的命令. */ private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } /** * Carries out the pending calculation. * @param x the value to be accumulated with the prior result. */ public void calculate(double x)//普通方法:計算數值 { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; display.setText("" + result); } }
程序運行結果以下:
測試程序2:
在elipse IDE中調試運行教材486頁程序12-2,結合運行結果理解程序;
掌握各類文本組件的用法;
記錄示例代碼閱讀理解中存在的問題與疑惑。
package text; import java.awt.*; import javax.swing.*; /** * @version 1.41 2015-06-12 * @author Cay Horstmann */ public class TextComponentTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new TextComponentFrame(); frame.setTitle("TextComponentTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package text; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * A frame with sample text components. */ public class TextComponentFrame extends JFrame { public static final int TEXTAREA_ROWS = 8;//定義行 public static final int TEXTAREA_COLUMNS = 20;//定義列 public TextComponentFrame()//構造器 { JTextField textField = new JTextField(); JPasswordField passwordField = new JPasswordField(); JPanel northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2, 2));//網格佈局管理器:2行2列 northPanel.add(new JLabel("User name: ", SwingConstants.RIGHT)); northPanel.add(textField);//將文本域添加到窗口 northPanel.add(new JLabel("Password: ", SwingConstants.RIGHT)); northPanel.add(passwordField);//將密碼輸入框添加到窗口 add(northPanel, BorderLayout.NORTH);//顯示在窗口的上方 JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER);//顯示在窗口中心 // 添加按鈕,將文本追加到文本區域 JPanel southPanel = new JPanel(); JButton insertButton = new JButton("Insert");//定義Button按鈕:insert southPanel.add(insertButton);//添加insert按鈕 insertButton.addActionListener(event -> textArea.append("User name: " + textField.getText() + " Password: " + new String(passwordField.getPassword()) + "\n")); add(southPanel, BorderLayout.SOUTH);//顯示在窗口下方 pack(); } }
程序運行結果以下:
咱們組的問題是在文本框輸入姓名和密碼後,若是輸入過長,會顯示不了,只能將文本框放大。能不能設置爲根據窗口大小自動換行來顯示。
測試程序3
l 在elipse IDE中調試運行教材489頁程序12-3,結合運行結果理解程序;
l 掌握複選框組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
package text; import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-06-12 * @author Cay Horstmann */ public class CheckBoxTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new CheckBoxFrame(); frame.setTitle("CheckBoxTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package text; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A frame with a sample text label and check boxes for selecting font * attributes. */ public class CheckBoxFrame extends JFrame { private JLabel label;//文本 private JCheckBox bold;//定義一個標籤 private JCheckBox italic;//斜體字 private static final int FONTSIZE = 24; public CheckBoxFrame()//構造器 { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.BOLD, FONTSIZE)); add(label, BorderLayout.CENTER);//邊框佈局管理器:顯示在窗口中心位置 // this listener sets the font attribute of // the label to the check box state ActionListener listener = event -> {//設置字體爲常規、加粗或斜體等 int mode = 0; if (bold.isSelected()) mode += Font.BOLD; if (italic.isSelected()) mode += Font.ITALIC; label.setFont(new Font("Serif", mode, FONTSIZE)); }; //添加複選框 JPanel buttonPanel = new JPanel(); bold = new JCheckBox("Bold"); bold.addActionListener(listener); bold.setSelected(true); buttonPanel.add(bold); italic = new JCheckBox("Italic"); italic.addActionListener(listener); buttonPanel.add(italic); add(buttonPanel, BorderLayout.SOUTH); pack(); } }
運行結果以下:
問題:對設置字體那部分的代碼不是太理解。
測試程序4
l 在elipse IDE中調試運行教材491頁程序12-4,運行結果理解程序;
l 掌握單選按鈕組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序以下:
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 帶有示例文本標籤和用於選擇字體大小的單選按鈕的框架. */ public class RadioButtonFrame extends JFrame { private JPanel buttonPanel; private ButtonGroup group;//定義一個ButtonGroup對象 private JLabel label; private static final int DEFAULT_SIZE = 36; public RadioButtonFrame()//構造器 { // add the sample text label label = new JLabel("The quick brown fox jumps over the lazy dog."); label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE)); add(label, BorderLayout.CENTER);//邊框佈局管理器:顯示在窗口中心 // 添加單選按鈕 buttonPanel = new JPanel(); group = new ButtonGroup(); addRadioButton("Small", 8); addRadioButton("Medium", 12); addRadioButton("Large", 18); addRadioButton("Extra large", 36); add(buttonPanel, BorderLayout.SOUTH);//四個按鈕顯示在窗口下方 pack(); } /** * 添加一個單選按鈕,用於設置示例文本的字體大小. * @param name the string to appear on the button * @param size the font size that this button sets */ public void addRadioButton(String name, int size) { boolean selected = size == DEFAULT_SIZE; JRadioButton button = new JRadioButton(name, selected); group.add(button); buttonPanel.add(button); // 這個監聽器設置標籤字體大小 ActionListener listener = event -> label.setFont(new Font("Serif", Font.PLAIN, size)); button.addActionListener(listener); } }
import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-06-12 * @author Cay Horstmann */ public class RadioButtonTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new RadioButtonFrame(); frame.setTitle("RadioButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
程序運行結果以下:
測試程序5
l 在elipse IDE中調試運行教材494頁程序12-5,結合運行結果理解程序;
l 掌握邊框的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序運行結果:
測試程序6
l 在elipse IDE中調試運行教材498頁程序12-6,結合運行結果理解程序;
l 掌握組合框組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
程序運行結果:
測試程序7
l 在elipse IDE中調試運行教材501頁程序12-7,結合運行結果理解程序;
l 掌握滑動條組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試程序8
l 在elipse IDE中調試運行教材512頁程序12-8,結合運行結果理解程序;
l 掌握菜單的建立、菜單事件監聽器、複選框和單選按鈕菜單項、彈出菜單以及快捷鍵和加速器的用法。
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
運行結果:
測試程序9
l 在elipse IDE中調試運行教材517頁程序12-9,結合運行結果理解程序;
l 掌握工具欄和工具提示的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
運行結果:
測試程序10
l 在elipse IDE中調試運行教材524頁程序12-10、12-11,結合運行結果理解程序,瞭解GridbagLayout的用法。
l 在elipse IDE中調試運行教材533頁程序12-12,結合程序運行結果理解程序,瞭解GroupLayout的用法。
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
運行結果:
三:實驗總結。
這一週學習了Swing用戶界面組件以及GUI相關組件。在學習過程當中,本身對理論知識的學習學的比較混亂,混淆了這幾部分的學習內容。另外,對於本週的實驗,實驗都有不少相同和相似的地方,在實驗過程當中任然沒有理解的太清楚。在查了課本上的內容以後,稍微有了掌握。此外,經過小組協做進行實驗,結合兩我的對實驗的理解,相比獨自完成試驗,在互相學習中有了更好的理解和掌握。