201771010111 李瑞紅《第十四周學習總結》

實驗十四  Swing圖形界面組件java

實驗時間 20178-11-29程序員

理論知識數據庫

1.佈局管理器設計模式

a 佈局管理器是一組類。app

b 實現java.awt.LayoutManager接口框架

c 決定容器中組件的位置和大小ide

d Java.awt包中定義了5種佈局管理類,每一種布 局管理類對應一種佈局策略。 工具

e 每一個容器都有與之相關的默認佈局管理器。佈局

f 當一個容器選定一種佈局策略時,它應該建立該 策略對應的佈局管理器對象,並將此對象設置爲 本身的佈局管理器。學習

g 5種佈局管理器

(1)FlowLayout:流佈局(Applet和Panel的默認 佈局管理器)

(2)BorderLayout:邊框佈局(Window、Frame和 Dialog的默認佈局管理器)

(3)GridLayout:網格佈局

(4)GridBagLayout:網格組佈局

(5)CardLayout:卡片佈局

2.Swing和MVC設計模式

(1)設計模式(Design pattern)是設計者一種流行的 思考設計問題的方法,是一套被反覆使用,多數人 知曉的,通過分類編目的,代碼設計經驗的總結。

(2)模型-視圖-控制器設計模式(Model –ViewController )是Java EE平臺下建立 Web 應用程序 的重要設計模式。

(3)MVC設計模式 – Model(模型):是程序中用於處理程序數據邏 輯的部分,一般模型負責在數據庫中存取數據。

– View(視圖):是程序中處理數據顯示的部分, 一般視圖依據模型存取的數據建立。

– Controller(控制器):是程序中處理用戶交互 的部分。一般控制器負責從視圖讀取數據,控制 用戶輸入,並向模型發送數據。

(4)Java組件有內容、外觀、行爲三個主要元素;

3.文本輸入

(1)文本域(JTextField) : 用於獲取單行文本輸入。

(2)文本區(JTextArea)組件可以讓用戶輸入多行文 本。生成JTextArea組件對象時,能夠指定文本 區的行數和列數: textArea = new JTextArea(8, 40);

  (3)文本區與文本域的異同相同之處: 文本域和文本區組件均可用於獲取文本輸入。

不一樣之處:  文本域只能接受單行文本的輸入;  文本區可以接受多行文本的輸入。

(4)文本區JTextArea的經常使用API:Java.swing. JTextArea 1.2 – JTextArea(int rows, int cols)

構造一個rows行cols列的文本區對象 – JTextArea(String text,int rows, int cols)

用初始文本構造一個文本區對象 – void setRows(int rows)

設置文本域使用的行數 – void append(String newText)

將給定文本附加到文本區中已有文本的後面 – void setLineWrap(boolean wrap)

打開或關閉換行

(5)標籤組件:標籤是容納文本的組件。它們沒有任何修飾(如沒有邊界 ),也不響應用戶輸入。

 標籤的經常使用用途之一就是標識組件,例如標識文本域。其使用步驟以下:

4.選擇組件

複選框  單選按鈕  邊框  組合框  滑動條

(1)複選框構造器 1.bold = new JCheckBox("Bold"); 複選框自動地帶有表示標籤。 JCheckBox(String label,Icon icon); 構造帶有標籤與圖標的複選框,默認初始未被選擇。JCheckBox(String label,boolean state); 用指定的標籤和初始化選擇狀態構造一個複選框單選按鈕的構造器(教材492頁) 1.JRadioButton(String label,Icon icon); 建立一個帶標籤和圖標的單選按鈕RadioButton(String label,boolean state); 用指定的標籤和初始化狀態構造單選按鈕按鈕組:爲單選按鈕組構造一個ButtonGroup的對象。 而後,再將JRadioButton類型的對象添加到按鈕 組中。按鈕組負責在新按鈕被按下的時,取消前一 個按鈕的選擇狀態。若是在一個窗口中 有多組複選框或單選按 鈕,就須要可視化的形 式指明哪些按鈕屬於同 一組。Swing提供了一 組頗有用的邊框)若是有多個選擇項,使用單選按鈕佔據的屏幕空 間太大時,就能夠選擇組合框。組合框的事件監聽:爲了判斷組合框的哪一個選項被選擇,可經過 事件參數調用getSource方法來獲得發送事件的組 合框引用,接着調用getSelectdeItem方法獲取當 前選擇的選項。滑動條:滑動條可讓用戶從一組離散值中進行選擇 ,而且它還容許進行連續值得選擇。

5.菜單

菜單建立  菜單項中的圖標  複選框和單選按鈕菜單項  彈出菜單  快捷鍵和加速器  啓用和禁用菜單項  工具欄  工具提示

網格組佈局 (GridBagLayout):GridBagLayout與GridLayout有點類似,它也是 將組件排在格子裏,可是GridBagLayout在網格 的基礎上提供更復雜的佈局。

GridBagLayout容許單個組件在一個單元中不填 滿整個單元,而只是佔用最佳大小,也容許單個 組件擴展成不止一個單元,而且能夠用任意順序 加入組件。

定製佈局管理器: 程序員可經過本身設計LayoutManager類來實現 特殊的佈局方式。定製佈局管理器須要實現LayoutManager接口, 並覆蓋如下方法。

6. 對話框

選項對話框   建立對話框   數據選擇   文件對話框  顏色選擇器

(1)對話框是一種大小不能變化、不能有菜單的容器窗口; 對話框不能做爲一個應用程序的主框架,而必須包含在其 他的容器中。

(2)選項對話框:JOptionPane提供的對話框是模式對話框。當模 式對話框顯示時,它不容許用戶輸入到程序的 其餘的窗口。使用JOptionPane,能夠建立和自 定義問題、信息、警告和錯誤等幾種類型的對 話框。

(3)數據交換:輸入對話框含有供用戶輸入文本的文本框、一個確認和取 消按鈕,是有模式對話框。當輸入對話框可見時,要求用戶 輸入一個字符串。

(4)文件對話框:專門用於對文件(或目錄)進行瀏覽和選擇的對 話框,經常使用的構造方法: – JFileChooser():根據用戶的缺省目錄建立文件對話框 – JFileChooser(File currentDirectory):根據File型參數 currentDirectory指定的目錄建立文件對話框

(5)顏色對話框: javax.swing包中的JColorChooser類的靜態方 法: public static Color showDialog(Component component, String title, Color initialColor)建立一個顏色對話框

(6)參數component指定對話框所依賴的組件,title 指定對話框的標題;initialColor 指定對話框返回 的初始顏色,即對話框消失後,返回的默認值。 顏色對話框可根據用戶在顏色對話框中選擇的顏 色返回一個顏色對象.

1、實驗目的與要求

(1) 掌握GUI佈局管理器用法;

(2) 掌握各種Java Swing組件用途及經常使用API;

2、實驗內容和步驟

實驗1: 導入第12章示例程序,測試程序並進行組內討論。

測試程序1

在elipse IDE中運行教材479頁程序12-1,結合運行結果理解程序;

掌握各類佈局管理器的用法;

理解GUI界面中事件處理技術的用途。

在佈局管理應用代碼處添加註釋;

package calculator;

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;
   private JPanel panel;
   private double result;
   private String lastCommand;
   private boolean start;
   //構造器
   public CalculatorPanel()
   {
      setLayout(new BorderLayout());//設定新的邊框佈局管理器

      result = 0;
      lastCommand = "=";
      start = true;

      // 添加display

      display = new JButton("0");//用來顯示完成算式計算結果
      display.setEnabled(false);//使得使能屬性不能起做用
      add(display, BorderLayout.NORTH);//將display添加到北部區域
      //生成監聽器對象
      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();

      // 在4 x 4網格中添加按鈕

      panel = new JPanel();//新建窗格組件
      panel.setLayout(new GridLayout(4, 4));//設定4行4列的網格佈局管理器
      //生成16個事件源,數字註冊的事件源是insert對象,運算符註冊的事件源是command對象
      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);//將panel添加到中部區域
     
   }

   /**
    * 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);//將其添加到窗格中
   }

   /**
    * This action inserts the button action string to the end of the display text.
    */
   //內部類
   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);//完成多個數字的輸入操做
      }
   }

   /**
    * This action executes the command that the button action string denotes.
    */
  //內部類
   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);//將數字轉化爲數字字符串輸出
   }
}
package calculator;

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);//設置可見
      });
   }
}
package calculator;

import javax.swing.*;

/**
 * A frame with a calculator panel.
 */
public class CalculatorFrame extends JFrame
{
   public CalculatorFrame()
   {
      add(new CalculatorPanel());
      pack();
   }
}

測試程序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;

/**
 * 帶有輸入文本框組件的框架。
 */
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));
      //SwingConstants一般用於在屏幕上定位或定向組件的常量的集合
      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);

      //構造具備指定行數和列數的新的空 TextArea。
      JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS);
      //建立一個顯示指定組件內容的 JScrollPane對象,只要組件的內容超過視圖大小就會顯示水平和垂直滾動條。 
      JScrollPane scrollPane = new JScrollPane(textArea);

      add(scrollPane, BorderLayout.CENTER);

      // add button to append text into the text area

      JPanel southPanel = new JPanel();

      JButton insertButton = new JButton("Insert");
      southPanel.add(insertButton);
      //將給定文本追加到文檔結尾。
      insertButton.addActionListener(event ->
         textArea.append("User name: " + textField.getText() + " Password: "
            + new String(passwordField.getPassword()) + "\n"));

      add(southPanel, BorderLayout.SOUTH);
      pack();
   }
}

測試程序3

在elipse IDE中調試運行教材489頁程序12-3,結合運行結果理解程序;

掌握複選框組件的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package checkBox;

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 checkBox;

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()
   {
      // 添加示例文本標籤

      label = new JLabel("The quick brown fox jumps over the lazy dog.");
      label.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
      add(label, BorderLayout.CENTER);

      // 字體屬性
      // 複選框狀態的標籤

      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

在elipse IDE中調試運行教材491頁程序12-4,運行結果理解程序;

掌握單選按鈕組件的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package radioButton;

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);
      });
   }
}
package radioButton;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 帶有示例文本標籤和用於選擇字體大小的單選按鈕的框架。
 */
public class RadioButtonFrame extends JFrame
{
   private JPanel buttonPanel;
   private ButtonGroup group;
   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);

      // add the radio buttons

      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 大小的規格要出如今按鈕上的字符串
    * @param 按鈕設置的字體大小
    */
   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);
   }
}

 

測試程序5

在elipse IDE中調試運行教材494頁程序12-5,結合運行結果理解程序;

掌握邊框的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package border;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.34 2015-06-13
 * @author Cay Horstmann
 */
public class BorderTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new BorderFrame();
         frame.setTitle("BorderTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}
package border;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * A frame with radio buttons to pick a border style.
 */
public class BorderFrame extends JFrame
{
   private JPanel demoPanel;
   private JPanel buttonPanel;
   private ButtonGroup group;

   public BorderFrame()
   {
      demoPanel = new JPanel();
      buttonPanel = new JPanel();
      group = new ButtonGroup();
      
      //設置不一樣的邊框類型按鈕,共六種(提供標準 Border 對象的工廠類)
      addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder());
      addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder());
      addRadioButton("Etched", BorderFactory.createEtchedBorder());
      addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE));
      addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE));
      addRadioButton("Empty", BorderFactory.createEmptyBorder());
      
      Border etched = BorderFactory.createEtchedBorder();
      Border titled = BorderFactory.createTitledBorder(etched, "Border types");
      buttonPanel.setBorder(titled);

      setLayout(new GridLayout(2, 1));
      add(buttonPanel);
      add(demoPanel);
      pack();
   }

   public void addRadioButton(String buttonName, Border b)
   {
      JRadioButton button = new JRadioButton(buttonName);
      button.addActionListener(event -> demoPanel.setBorder(b));
      group.add(button);
      buttonPanel.add(button);
   }
}

 點其中一個按鈕,實驗結果

測試程序6

在elipse IDE中調試運行教材498頁程序12-6,結合運行結果理解程序;

掌握組合框組件的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package comboBox;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.35 2015-06-12
 * @author Cay Horstmann
 */
public class ComboBoxTest
{
   public static void main(String[] args)
   {
      //lambda表達式
      EventQueue.invokeLater(() -> {
     
         JFrame frame = new ComboBoxFrame();
      
         frame.setTitle("ComboBoxTest");
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
         frame.setVisible(true);
      });
   }
}
package comboBox;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 具備示例文本標籤和用於選擇字體外觀的組合框的框架。
 * 組合框:將按鈕或可編輯字段與下拉列表組合的組件。
 * 用戶能夠從下拉列表中選擇值,下拉列表在用戶請求時顯示
 */

public class ComboBoxFrame extends JFrame
{
   private JComboBox<String> faceCombo;
   private JLabel label;
   private static final int DEFAULT_SIZE = 24;

   public ComboBoxFrame()
   {
      // 添加示例文本標籤

      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);

      // 建立一個組合框對象並添加項目名稱

      faceCombo = new JComboBox<>();
      faceCombo.addItem("Serif");
      faceCombo.addItem("SansSerif");
      faceCombo.addItem("Monospaced");
      faceCombo.addItem("Dialog");
      faceCombo.addItem("DialogInput");

      // 組合框監聽器將標籤字體更改成所選的名稱(添加監聽器,使用lambda表達式)

      faceCombo.addActionListener(event ->
     
         label.setFont(
            //getItemAt用於返回指定索引處的列表項;getSelectedIndex用於返回當前選擇的選項
            new Font(faceCombo.getItemAt(faceCombo.getSelectedIndex()), 
               Font.PLAIN, DEFAULT_SIZE)));



      JPanel comboPanel = new JPanel();
      comboPanel.add(faceCombo);
      add(comboPanel, BorderLayout.SOUTH);
      pack();
   }
}

 

測試程序7

在elipse IDE中調試運行教材501頁程序12-7,結合運行結果理解程序;

掌握滑動條組件的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package slider;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.15 2015-06-12
 * @author Cay Horstmann
 */
public class SliderTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         SliderFrame frame = new SliderFrame();//生成類對象
         frame.setTitle("SliderTest");//將此窗體的標題設置爲指定的字符串
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置用戶在此窗體上發起 "close" 時默認執行的操做
         frame.setVisible(true);//根據參數 b 的值顯示或隱藏此 Window
      });
   }
}
package slider;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A frame with many sliders and a text field to show slider values.
 */
public class SliderFrame extends JFrame//類的繼承
{
   private JPanel sliderPanel;
   private JTextField textField;
   private ChangeListener listener;

   public SliderFrame()
   {
      sliderPanel = new JPanel();//生成類對象
      sliderPanel.setLayout(new GridBagLayout());//設置此容器的佈局管理器爲網格包佈局管理器。 

      // 爲全部 sliders註冊監聽器
      listener = event -> {
         //當滑動slider的時候 更新文本域的值
         JSlider source = (JSlider) event.getSource();//生成一個讓用戶以圖形方式在有界區間內經過移動滑塊來選擇值的組件類對象。 
         textField.setText("" + source.getValue());//將此 TextComponent 文本設置爲指定文本
      };

 

      JSlider slider = new JSlider();//建立一個水平滑塊。
      addSlider(slider, "Plain");


      slider = new JSlider();
      slider.setPaintTicks(true);//在滑塊上繪製刻度標記
      slider.setMajorTickSpacing(20);//設置主刻度標記的間隔
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Ticks");

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setSnapToTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Snap to ticks");

      // 添加沒有軌跡的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      slider.setPaintTrack(false);
      addSlider(slider, "No track");

      //添加倒置的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      slider.setInverted(true);
      addSlider(slider, "Inverted");

      // 添加一個數字標籤的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);//在滑塊上繪製標籤。
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Labels");

      // 添加一個字母標籤的平滑快

      slider = new JSlider();
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);//在滑塊上繪製刻度標記
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);

      Dictionary<Integer, Component> labelTable = new Hashtable<>();
      labelTable.put(0, new JLabel("A"));//建立 JLabel 實例。
      labelTable.put(20, new JLabel("B"));
      labelTable.put(40, new JLabel("C"));
      labelTable.put(60, new JLabel("D"));
      labelTable.put(80, new JLabel("E"));
      labelTable.put(100, new JLabel("F"));

      slider.setLabelTable(labelTable);//在給定值處繪製標籤
      addSlider(slider, "Custom labels");


      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      slider.setSnapToTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(20);

      labelTable = new Hashtable<Integer, Component>();//構造一個新的空哈希表。 

      // 添加圖像

      labelTable.put(0, new JLabel(new ImageIcon("nine.gif")));
      labelTable.put(20, new JLabel(new ImageIcon("ten.gif")));
      labelTable.put(40, new JLabel(new ImageIcon("jack.gif")));
      labelTable.put(60, new JLabel(new ImageIcon("queen.gif")));
      labelTable.put(80, new JLabel(new ImageIcon("king.gif")));
      labelTable.put(100, new JLabel(new ImageIcon("ace.gif")));

      slider.setLabelTable(labelTable);//用於指定將在給定值處繪製標籤
      addSlider(slider, "Icon labels");

      // 添加文本域

      textField = new JTextField();
      add(sliderPanel, BorderLayout.CENTER);
      add(textField, BorderLayout.SOUTH);
      pack();//調整此窗口的大小,以適合其子組件的首選大小和佈局。
   }

   /**
    * Adds a slider to the slider panel and hooks up the listener
    * @param s the slider
    * @param description the slider description
    */
   public void addSlider(JSlider s, String description)//一個讓用戶以圖形方式在有界區間內經過移動滑塊來選擇值的組件。 
   {
      s.addChangeListener(listener);
      JPanel panel = new JPanel();
      panel.add(s);
      panel.add(new JLabel(description));
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);//設置垂直對齊方式。 
      GridBagConstraints gbc = new GridBagConstraints();//設置組件間的約束關係
      gbc.gridy = sliderPanel.getComponentCount();
      gbc.anchor = GridBagConstraints.WEST;
      sliderPanel.add(panel, gbc);
   }
}

實驗結果

測試程序8

在elipse IDE中調試運行教材512頁程序12-8,結合運行結果理解程序;

掌握菜單的建立、菜單事件監聽器、複選框和單選按鈕菜單項、彈出菜單以及快捷鍵和加速器的用法。

記錄示例代碼閱讀理解中存在的問題與疑惑。

package menu;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.24 2012-06-12
 * @author Cay Horstmann
 */
public class MenuTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new MenuFrame();
         frame.setTitle("MenuTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}
package menu;

import java.awt.event.*;
import javax.swing.*;

/**
 * A frame with a sample menu bar.
 */
public class MenuFrame extends JFrame
{
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 200;
   private Action saveAction;
   private Action saveAsAction;
   private JCheckBoxMenuItem readonlyItem;
   private JPopupMenu popup;

   /**
    * A sample action that prints the action name to System.out
    */
   class TestAction extends AbstractAction
   {
      public TestAction(String name)
      {
         super(name);
      }

      public void actionPerformed(ActionEvent event)
      {
         System.out.println(getValue(Action.NAME) + " selected.");
      }
   }

   public MenuFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      JMenu fileMenu = new JMenu("File");
      fileMenu.add(new TestAction("New"));

      //演示加速器

      JMenuItem openItem = fileMenu.add(new TestAction("Open"));
      openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

      fileMenu.addSeparator();

      saveAction = new TestAction("Save");
      JMenuItem saveItem = fileMenu.add(saveAction);
      saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

      saveAsAction = new TestAction("Save As");
      fileMenu.add(saveAsAction);
      fileMenu.addSeparator();

      fileMenu.add(new AbstractAction("Exit")
         {
            public void actionPerformed(ActionEvent event)
            {
               System.exit(0);
            }
         });

      //演示覆選框和單選按鈕菜單

      readonlyItem = new JCheckBoxMenuItem("Read-only");
      readonlyItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               boolean saveOk = !readonlyItem.isSelected();
               saveAction.setEnabled(saveOk);
               saveAsAction.setEnabled(saveOk);
            }
         });

      ButtonGroup group = new ButtonGroup();

      JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
      insertItem.setSelected(true);
      JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

      group.add(insertItem);
      group.add(overtypeItem);

      //演示圖標

      Action cutAction = new TestAction("Cut");
      cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
      Action copyAction = new TestAction("Copy");
      copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
      Action pasteAction = new TestAction("Paste");
      pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

      JMenu editMenu = new JMenu("Edit");
      editMenu.add(cutAction);
      editMenu.add(copyAction);
      editMenu.add(pasteAction);

      // 演示嵌套菜單

      JMenu optionMenu = new JMenu("Options");

      optionMenu.add(readonlyItem);
      optionMenu.addSeparator();
      optionMenu.add(insertItem);
      optionMenu.add(overtypeItem);

      editMenu.addSeparator();
      editMenu.add(optionMenu);

      // 說明助記符

      JMenu helpMenu = new JMenu("Help");
      helpMenu.setMnemonic('H');

      JMenuItem indexItem = new JMenuItem("Index");
      indexItem.setMnemonic('I');
      helpMenu.add(indexItem);

      //您還能夠向操做添加助記符鍵
      Action aboutAction = new TestAction("About");
      aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
      helpMenu.add(aboutAction);
      
      // 將全部頂級菜單添加到菜單欄

      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

      menuBar.add(fileMenu);
      menuBar.add(editMenu);
      menuBar.add(helpMenu);

      // 顯示彈出窗口

      popup = new JPopupMenu();
      popup.add(cutAction);
      popup.add(copyAction);
      popup.add(pasteAction);

      JPanel panel = new JPanel();
      panel.setComponentPopupMenu(popup);
      add(panel);
   }
}

測試程序9

在elipse IDE中調試運行教材517頁程序12-9,結合運行結果理解程序;

掌握工具欄和工具提示的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package toolBar;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.14 2015-06-12
 * @author Cay Horstmann
 */
public class ToolBarTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         ToolBarFrame frame = new ToolBarFrame();
         frame.setTitle("ToolBarTest");//設置標題
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);//設置可見
      });
   }
}
package toolBar;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A frame with a toolbar and menu for color changes.
 */
public class ToolBarFrame extends JFrame
{
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 200;
   private JPanel panel;

   public ToolBarFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // 添加顏色更改面板

      panel = new JPanel();
      add(panel, BorderLayout.CENTER);

      // 設定動做
      Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
      Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
            Color.YELLOW);
      Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);

      Action exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif"))
         {
            public void actionPerformed(ActionEvent event)
            {
               System.exit(0);
            }
         };
      exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");

      //填充工具欄

      JToolBar bar = new JToolBar();
      bar.add(blueAction);
      bar.add(yellowAction);
      bar.add(redAction);
      bar.addSeparator();
      bar.add(exitAction);
      add(bar, BorderLayout.NORTH);

      // populate menu

      JMenu menu = new JMenu("Color");
      menu.add(yellowAction);
      menu.add(blueAction);
      menu.add(redAction);
      menu.add(exitAction);
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);
      setJMenuBar(menuBar);
   }

   /**
    * The color action sets the background of the frame to a given color.
    */
   class ColorAction extends AbstractAction
   {
      public ColorAction(String name, Icon icon, Color c)
      {
         putValue(Action.NAME, name);
         putValue(Action.SMALL_ICON, icon);
         putValue(Action.SHORT_DESCRIPTION, name + " background");
         putValue("Color", c);
      }

      public void actionPerformed(ActionEvent event)
      {
         Color c = (Color) getValue("Color");
         panel.setBackground(c);
      }
   }
}

點一個按鈕以後

測試程序10

在elipse IDE中調試運行教材524頁程序12-十、12-11,結合運行結果理解程序,瞭解GridbagLayout的用法。

在elipse IDE中調試運行教材533頁程序12-12,結合程序運行結果理解程序,瞭解GroupLayout的用法。

記錄示例代碼閱讀理解中存在的問題與疑惑。

package gridbag;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

/**
 * A frame that uses a grid bag layout to arrange font selection components.
 */
public class FontFrame extends JFrame
{
   public static final int TEXT_ROWS = 10;
   public static final int TEXT_COLUMNS = 20;

   private JComboBox<String> face;
   private JComboBox<Integer> size;
   private JCheckBox bold;
   private JCheckBox italic;
   private JTextArea sample;

   public FontFrame()
   {
      GridBagLayout layout = new GridBagLayout();
      setLayout(layout);

      ActionListener listener = event -> updateSample();

      // 構建組件

      JLabel faceLabel = new JLabel("Face: ");

      face = new JComboBox<>(new String[] { "Serif", "SansSerif", "Monospaced",
            "Dialog", "DialogInput" });

      face.addActionListener(listener);

      JLabel sizeLabel = new JLabel("Size: ");

      size = new JComboBox<>(new Integer[] { 8, 10, 12, 15, 18, 24, 36, 48 });

      size.addActionListener(listener);

      bold = new JCheckBox("Bold");
      bold.addActionListener(listener);

      italic = new JCheckBox("Italic");
      italic.addActionListener(listener);

      sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
      sample.setText("The quick brown fox jumps over the lazy dog");
      sample.setEditable(false);
      sample.setLineWrap(true);
      sample.setBorder(BorderFactory.createEtchedBorder());

      // 使用GBC便利類向網格添加組件

      add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
      add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)
            .setInsets(1));
      add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
      add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)
            .setInsets(1));
      add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
      add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
      add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
      pack();
      updateSample();
   }

   public void updateSample()
   {
      String fontFace = (String) face.getSelectedItem();
      int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
            + (italic.isSelected() ? Font.ITALIC : 0);
      int fontSize = size.getItemAt(size.getSelectedIndex());
      Font font = new Font(fontFace, fontStyle, fontSize);
      sample.setFont(font);
      sample.repaint();
   }
package groupLayout;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

/**
 * A frame that uses a group layout to arrange font selection components.
 */
public class FontFrame extends JFrame
{
   public static final int TEXT_ROWS = 10;
   public static final int TEXT_COLUMNS = 20;

   private JComboBox<String> face;
   private JComboBox<Integer> size;
   private JCheckBox bold;
   private JCheckBox italic;
   private JScrollPane pane;
   private JTextArea sample;

   public FontFrame()
   {
      ActionListener listener = event -> updateSample(); 

      // 構建組件

      JLabel faceLabel = new JLabel("Face: ");

      face = new JComboBox<>(new String[] { "Serif", "SansSerif", "Monospaced", "Dialog",
            "DialogInput" });

      face.addActionListener(listener);

      JLabel sizeLabel = new JLabel("Size: ");

      size = new JComboBox<>(new Integer[] { 8, 10, 12, 15, 18, 24, 36, 48 });

      size.addActionListener(listener);

      bold = new JCheckBox("Bold");
      bold.addActionListener(listener);

      italic = new JCheckBox("Italic");
      italic.addActionListener(listener);

      sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
      sample.setText("The quick brown fox jumps over the lazy dog");
      sample.setEditable(false);
      sample.setLineWrap(true);
      sample.setBorder(BorderFactory.createEtchedBorder());

      pane = new JScrollPane(sample);

      GroupLayout layout = new GroupLayout(getContentPane());
      setLayout(layout);
      layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(
                  layout.createSequentialGroup().addContainerGap().addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                              GroupLayout.Alignment.TRAILING,
                              layout.createSequentialGroup().addGroup(
                                    layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                          .addComponent(faceLabel).addComponent(sizeLabel))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(
                                          layout.createParallelGroup(
                                                GroupLayout.Alignment.LEADING, false)
                                                .addComponent(size).addComponent(face)))
                              .addComponent(italic).addComponent(bold)).addPreferredGap(
                        LayoutStyle.ComponentPlacement.RELATED).addComponent(pane)
                        .addContainerGap()));

      layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] { face, size });

      layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(
                  layout.createSequentialGroup().addContainerGap().addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(
                              pane, GroupLayout.Alignment.TRAILING).addGroup(
                              layout.createSequentialGroup().addGroup(
                                    layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                          .addComponent(face).addComponent(faceLabel))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(
                                          layout.createParallelGroup(
                                                GroupLayout.Alignment.BASELINE).addComponent(size)
                                                .addComponent(sizeLabel)).addPreferredGap(
                                          LayoutStyle.ComponentPlacement.RELATED).addComponent(
                                          italic, GroupLayout.DEFAULT_SIZE,
                                          GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(bold, GroupLayout.DEFAULT_SIZE,
                                          GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap()));
      pack();
   }
   
   public void updateSample()
   {
      String fontFace = (String) face.getSelectedItem();
      int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
            + (italic.isSelected() ? Font.ITALIC : 0);
      int fontSize = size.getItemAt(size.getSelectedIndex());
      Font font = new Font(fontFace, fontStyle, fontSize);
      sample.setFont(font);
      sample.repaint();
   }
}
 

 

測試程序11

在elipse IDE中調試運行教材539頁程序12-1三、12-14,結合運行結果理解程序;

掌握定製佈局管理器的用法。

記錄示例代碼閱讀理解中存在的問題與疑惑。

package fileChooser;

import java.io.*;

import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;

/**
 * A frame that has a menu for loading an image and a display area for the
 * loaded image.
 */
public class ImageViewerFrame extends JFrame
{
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 400;
   private JLabel label;
   private JFileChooser chooser;

   public ImageViewerFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // set up menu bar
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

      JMenu menu = new JMenu("File");
      menuBar.add(menu);

      JMenuItem openItem = new JMenuItem("Open");
      menu.add(openItem);
      openItem.addActionListener(event -> {
         chooser.setCurrentDirectory(new File("."));

         // show file chooser dialog
            int result = chooser.showOpenDialog(ImageViewerFrame.this);

            // if image file accepted, set it as icon of the label
            if (result == JFileChooser.APPROVE_OPTION)
            {
               String name = chooser.getSelectedFile().getPath();
               label.setIcon(new ImageIcon(name));
               pack();
            }
         });

      JMenuItem exitItem = new JMenuItem("Exit");
      menu.add(exitItem);
      exitItem.addActionListener(event -> System.exit(0));

      // use a label to display the images
      label = new JLabel();
      add(label);

      // set up file chooser
      chooser = new JFileChooser();

      // accept all image files ending with .jpg, .jpeg, .gif
      FileFilter filter = new FileNameExtensionFilter(
            "Image files", "jpg", "jpeg", "gif");
      chooser.setFileFilter(filter);

      chooser.setAccessory(new ImagePreviewer(chooser));

      chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif")));
   }
}
package circleLayout;

import javax.swing.*;

/**
 * A frame that shows buttons arranged along a circle.
 */
public class CircleLayoutFrame extends JFrame
{
   public CircleLayoutFrame()
   {
      setLayout(new CircleLayout());
      add(new JButton("Yellow"));
      add(new JButton("Blue"));
      add(new JButton("Red"));
      add(new JButton("Green"));
      add(new JButton("Orange"));
      add(new JButton("Fuchsia"));
      add(new JButton("Indigo"));
      pack();
   }
}
package circleLayout;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.33 2015-06-12
 * @author Cay Horstmann
 */
public class CircleLayoutTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new CircleLayoutFrame();
         frame.setTitle("CircleLayoutTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

 

測試程序12

在elipse IDE中調試運行教材544頁程序12-1五、12-16,結合運行結果理解程序;

掌握選項對話框的用法。

記錄示例代碼閱讀理解中存在的問題與疑惑。

package optionDialog;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.34 2015-06-12
 * @author Cay Horstmann
 */
public class OptionDialogTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new OptionDialogFrame();
         frame.setTitle("OptionDialogTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

package optionDialog;

import javax.swing.*;

/**
 * A panel with radio buttons inside a titled border.
 */
public class ButtonPanel extends JPanel
{
   private ButtonGroup group;

   /**
    * Constructs a button panel.
    * @param title the title shown in the border
    * @param options an array of radio button labels
    */
   public ButtonPanel(String title, String... options)
   {
      setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
      group = new ButtonGroup();

      // make one radio button for each option
      for (String option : options)
      {
         JRadioButton b = new JRadioButton(option);
         b.setActionCommand(option);
         add(b);
         group.add(b);
         b.setSelected(option == options[0]);
      }
   }

   /**
    * Gets the currently selected option.
    * @return the label of the currently selected radio button.
    */
   public String getSelection()
   {
      return group.getSelection().getActionCommand();
   }
}
package optionDialog;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

/**
 * A frame that contains settings for selecting various option dialogs.
 */
public class OptionDialogFrame extends JFrame
{
   private ButtonPanel typePanel;
   private ButtonPanel messagePanel;
   private ButtonPanel messageTypePanel;
   private ButtonPanel optionTypePanel;
   private ButtonPanel optionsPanel;
   private ButtonPanel inputPanel;
   private String messageString = "Message";
   private Icon messageIcon = new ImageIcon("blue-ball.gif");
   private Object messageObject = new Date();
   private Component messageComponent = new SampleComponent();

   public OptionDialogFrame()
   {
      JPanel gridPanel = new JPanel();
      gridPanel.setLayout(new GridLayout(2, 3));

      typePanel = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input");
      messageTypePanel = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE",
            "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE");
      messagePanel = new ButtonPanel("Message", "String", "Icon", "Component", "Other", 
            "Object[]");
      optionTypePanel = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_NO_OPTION",
            "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION");
      optionsPanel = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]");
      inputPanel = new ButtonPanel("Input", "Text field", "Combo box");

      gridPanel.add(typePanel);
      gridPanel.add(messageTypePanel);
      gridPanel.add(messagePanel);
      gridPanel.add(optionTypePanel);
      gridPanel.add(optionsPanel);
      gridPanel.add(inputPanel);

      // add a panel with a Show button

      JPanel showPanel = new JPanel();
      JButton showButton = new JButton("Show");
      showButton.addActionListener(new ShowAction());
      showPanel.add(showButton);

      add(gridPanel, BorderLayout.CENTER);
      add(showPanel, BorderLayout.SOUTH);
      pack();
   }

   /**
    * Gets the currently selected message.
    * @return a string, icon, component, or object array, depending on the Message panel selection
    */
   public Object getMessage()
   {
      String s = messagePanel.getSelection();
      if (s.equals("String")) return messageString;
      else if (s.equals("Icon")) return messageIcon;
      else if (s.equals("Component")) return messageComponent;
      else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon,
            messageComponent, messageObject };
      else if (s.equals("Other")) return messageObject;
      else return null;
   }

   /**
    * Gets the currently selected options.
    * @return an array of strings, icons, or objects, depending on the Option panel selection
    */
   public Object[] getOptions()
   {
      String s = optionsPanel.getSelection();
      if (s.equals("String[]")) return new String[] { "Yellow", "Blue", "Red" };
      else if (s.equals("Icon[]")) return new Icon[] { new ImageIcon("yellow-ball.gif"),
            new ImageIcon("blue-ball.gif"), new ImageIcon("red-ball.gif") };
      else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon,
            messageComponent, messageObject };
      else return null;
   }

   /**
    * Gets the selected message or option type
    * @param panel the Message Type or Confirm panel
    * @return the selected XXX_MESSAGE or XXX_OPTION constant from the JOptionPane class
    */
   public int getType(ButtonPanel panel)
   {
      String s = panel.getSelection();
      try
      {
         return JOptionPane.class.getField(s).getInt(null);
      }
      catch (Exception e)
      {
         return -1;
      }
   }

   /**
    * The action listener for the Show button shows a Confirm, Input, Message, or Option dialog
    * depending on the Type panel selection.
    */
   private class ShowAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         if (typePanel.getSelection().equals("Confirm")) JOptionPane.showConfirmDialog(
               OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel),
               getType(messageTypePanel));
         else if (typePanel.getSelection().equals("Input"))
         {
            if (inputPanel.getSelection().equals("Text field")) JOptionPane.showInputDialog(
                  OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel));
            else JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title",
                  getType(messageTypePanel), null, new String[] { "Yellow", "Blue", "Red" },
                  "Blue");
         }
         else if (typePanel.getSelection().equals("Message")) JOptionPane.showMessageDialog(
               OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel));
         else if (typePanel.getSelection().equals("Option")) JOptionPane.showOptionDialog(
               OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel),
               getType(messageTypePanel), null, getOptions(), getOptions()[0]);
      }
   }
}

/**
 * A component with a painted surface
 */

class SampleComponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      Rectangle2D rect = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1);
      g2.setPaint(Color.YELLOW);
      g2.fill(rect);
      g2.setPaint(Color.BLUE);
      g2.draw(rect);
   }

   public Dimension getPreferredSize()
   {
      return new Dimension(10, 10);
   }
}

測試程序13

在elipse IDE中調試運行教材552頁程序12-1七、12-18,結合運行結果理解程序;

掌握對話框的建立方法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

 

測試程序14

在elipse IDE中調試運行教材556頁程序12-1九、12-20,結合運行結果理解程序;

掌握對話框的數據交換用法記錄示例代碼閱讀理解中存在的問題package dimport java.awt.*import javax.sw

/**
 * @version 1.34 2015-06-12
 * @author Cay Horstmann
 */
public class DataExchangeTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new DataExchangeFrame();
         frame.setTitle("DataExchangeTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
import java.awt.*import java.awt.event.*/* * A frame with a menu whose File->Connect action shows a password dialog.
*/ public class DataExchangeFrame extends JFrame { public static final int TEXT_ROWS = 20; public static final int TEXT_COLUMNS = 40; private PasswordChooser dialog = null; private JTextArea textArea; public DataExchangeFrame() {  JMenuBar mbar = new JMenuBar(); setJMenuBar(mbar); JMenu fileMenu = new JMenu("File"); mbar.add(fileMenu);  JMenuItem connectItem = new JMenuItem("Connect"); connectItem.addActionListener(new ConnectAction()); fileMenu.add(connectItem); // The Exit item exits the program  JMenuItem exitItem = new JMenuItem("Exit"); exitItem.addActionListener(event -> System.exit(0)); fileMenu.add(exitItem); textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS); add(new JScrollPane(textArea), BorderLayout.CENTER); pack(); } /** * The Connect action pops up the password dialog. */ private class ConnectAction implements ActionListener { public void actionPerformed(ActionEvent event) { // if (dialog == null) dialog = new PasswordChooser();  dialog.etUser(new User("yourname", null));  if (dialog.showDialog(DataExchangeFrame.this, "Connect")) { // User u = dialog.getUser(); textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + "\n"); } } } }
package dataExchange;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 * A password chooser that is shown inside a dialog
 */
public class PasswordChooser extends JPanel
{
   private JTextField username;
   private JPasswordField password;
   private JButton okButton;
   private boolean ok;
   private JDialog dialog;

   public PasswordChooser()
   {
      setLayout(new BorderLayout());

      // construct a panel with user name and password fields

      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(2, 2));
      panel.add(new JLabel("User name:"));
      panel.add(username = new JTextField(""));
      panel.add(new JLabel("Password:"));
      panel.add(password = new JPasswordField(""));
      add(panel, BorderLayout.CENTER);

      // create Ok and Cancel buttons that terminate the dialog

      okButton = new JButton("Ok");
      okButton.addActionListener(event -> {
         ok = true;
         dialog.setVisible(false);
      });

      JButton cancelButton = new JButton("Cancel");
      cancelButton.addActionListener(event -> dialog.setVisible(false));

      // add buttons to southern border

      JPanel buttonPanel = new JPanel();
      buttonPanel.add(okButton);
      buttonPanel.add(cancelButton);
      add(buttonPanel, BorderLayout.SOUTH);
   }

   /**
    * Sets the dialog defaults.
    * @param u the default user information
    */
   public void setUser(User u)
   {
      username.setText(u.getName());
   }

   /**
    * Gets the dialog entries.
    * @return a User object whose state represents the dialog entries
    */
   public User getUser()
   {
      return new User(username.getText(), password.getPassword());
   }

   /**
    * Show the chooser panel in a dialog
    * @param parent a component in the owner frame or null
    * @param title the dialog window title
    */
   public boolean showDialog(Component parent, String title)
   {
      ok = false;

      // locate the owner frame

      Frame owner = null;
      if (parent instanceof Frame)
         owner = (Frame) parent;
      else
         owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);

      // if first time, or if owner has changed, make new dialog

      if (dialog == null || dialog.getOwner() != owner)
      {
         dialog = new JDialog(owner, true);
         dialog.add(this);
         dialog.getRootPane().setDefaultButton(okButton);
         dialog.pack();
      }

      // set title and show dialog

      dialog.setTitle(title);
      dialog.setVisible(true);
      return ok;
   }
}
package dataExchange;

/**
 * A user has a name and password. For security reasons, the password is stored as a char[], not a
 * String.
 */
public class User
{
   private String name;
   private char[] password;

   public User(String aName, char[] aPassword)
   {
      name = aName;
      password = aPassword;
   }

   public String getName()
   {
      return name;
   }

   public char[] getPassword()
   {
      return password;
   }

   public void setName(String aName)
   {
      name = aName;
   }

   public void setPassword(char[] aPassword)
   {
      password = aPassword;
   }
}

測試程序15

在elipse IDE中調試運行教材556頁程序12-2一、12-2212-23,結合程序運行結果理解程序;

掌握文件對話框的用法;

記錄示例代碼閱讀理解中存在的問題與疑惑。

package fileChooser;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.25 2015-06-12
 * @author Cay Horstmann
 */
public class FileChooserTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new ImageViewerFrame();
         frame.setTitle("FileChooserTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}
package fileChooser;

import java.awt.*;
import java.io.*;

import javax.swing.*;

/**
 * A file chooser accessory that previews images.
 */
public class ImagePreviewer extends JLabel
{
   /**
    * Constructs an ImagePreviewer.
    * @param chooser the file chooser whose property changes trigger an image
    *        change in this previewer
    */
   public ImagePreviewer(JFileChooser chooser)
   {
      setPreferredSize(new Dimension(100, 100));
      setBorder(BorderFactory.createEtchedBorder());

      chooser.addPropertyChangeListener(event -> {
         if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
         {
            // the user has selected a new file
            File f = (File) event.getNewValue();
            if (f == null)
            {
               setIcon(null);
               return;
            }

            // read the image into an icon
            ImageIcon icon = new ImageIcon(f.getPath());

            // if the icon is too large to fit, scale it
            if (icon.getIconWidth() > getWidth())
               icon = new ImageIcon(icon.getImage().getScaledInstance(
                     getWidth(), -1, Image.SCALE_DEFAULT));

            setIcon(icon);
         }
      });
   }
}
package fileChooser;

import java.io.*;

import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;

/**
 * A frame that has a menu for loading an image and a display area for the
 * loaded image.
 */
public class ImageViewerFrame extends JFrame
{
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 400;
   private JLabel label;
   private JFileChooser chooser;

   public ImageViewerFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // set up menu bar
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

      JMenu menu = new JMenu("File");
      menuBar.add(menu);

      JMenuItem openItem = new JMenuItem("Open");
      menu.add(openItem);
      openItem.addActionListener(event -> {
         chooser.setCurrentDirectory(new File("."));

         // show file chooser dialog
            int result = chooser.showOpenDialog(ImageViewerFrame.this);

            // if image file accepted, set it as icon of the label
            if (result == JFileChooser.APPROVE_OPTION)
            {
               String name = chooser.getSelectedFile().getPath();
               label.setIcon(new ImageIcon(name));
               pack();
            }
         });

      JMenuItem exitItem = new JMenuItem("Exit");
      menu.add(exitItem);
      exitItem.addActionListener(event -> System.exit(0));

      // use a label to display the images
      label = new JLabel();
      add(label);

      // set up file chooser
      chooser = new JFileChooser();

      // accept all image files ending with .jpg, .jpeg, .gif
      FileFilter filter = new FileNameExtensionFilter(
            "Image files", "jpg", "jpeg", "gif");
      chooser.setFileFilter(filter);

      chooser.setAccessory(new ImagePreviewer(chooser));

      chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif")));
   }
}

 

測試程序16

在elipse IDE中調試運行教材570頁程序12-24,結合運行結果理解程序;

瞭解顏色選擇器的用法。

記錄示例代碼閱讀理解中存在的問題與疑惑。

package colorChooser;

import javax.swing.*;

/**
 * A frame with a color chooser panel
 */
public class ColorChooserFrame extends JFrame
{
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 200;

   public ColorChooserFrame()
   {      
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add color chooser panel to frame

      ColorChooserPanel panel = new ColorChooserPanel();
      add(panel);
   }
}
package colorChooser;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.04 2015-06-12
 * @author Cay Horstmann
 */
public class ColorChooserTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new ColorChooserFrame();
         frame.setTitle("ColorChooserTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

實驗2:組內討論反思本組負責程序,理解程序整體結構,梳理程序GUI設計中應用的相關組件,整理相關組件的API,對程序中組件應用的相關代碼添加註釋。

本組負責第七個實驗

package slider;

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.15 2015-06-12
 * @author Cay Horstmann
 */
public class SliderTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         SliderFrame frame = new SliderFrame();//生成類對象
         frame.setTitle("SliderTest");//將此窗體的標題設置爲指定的字符串
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置用戶在此窗體上發起 "close" 時默認執行的操做
         frame.setVisible(true);//根據參數 b 的值顯示或隱藏此 Window
      });
   }
}

package slider;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A frame with many sliders and a text field to show slider values.
 */
public class SliderFrame extends JFrame//類的繼承
{
   private JPanel sliderPanel;
   private JTextField textField;
   private ChangeListener listener;

   public SliderFrame()
   {
      sliderPanel = new JPanel();//生成類對象
      sliderPanel.setLayout(new GridBagLayout());//設置此容器的佈局管理器爲網格包佈局管理器。 

      // 爲全部 sliders註冊監聽器
      listener = event -> {
         //當滑動slider的時候 更新文本域的值
         JSlider source = (JSlider) event.getSource();//生成一個讓用戶以圖形方式在有界區間內經過移動滑塊來選擇值的組件類對象。 
         textField.setText("" + source.getValue());//將此 TextComponent 文本設置爲指定文本
      };

 

      JSlider slider = new JSlider();//建立一個水平滑塊。
      addSlider(slider, "Plain");


      slider = new JSlider();
      slider.setPaintTicks(true);//在滑塊上繪製刻度標記
      slider.setMajorTickSpacing(20);//設置主刻度標記的間隔
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Ticks");

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setSnapToTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Snap to ticks");

      // 添加沒有軌跡的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      slider.setPaintTrack(false);
      addSlider(slider, "No track");

      //添加倒置的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      slider.setInverted(true);
      addSlider(slider, "Inverted");

      // 添加一個數字標籤的平滑快

      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);//在滑塊上繪製標籤。
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);
      addSlider(slider, "Labels");

      // 添加一個字母標籤的平滑快

      slider = new JSlider();
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);//在滑塊上繪製刻度標記
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(5);

      Dictionary<Integer, Component> labelTable = new Hashtable<>();
      labelTable.put(0, new JLabel("A"));//建立 JLabel 實例。
      labelTable.put(20, new JLabel("B"));
      labelTable.put(40, new JLabel("C"));
      labelTable.put(60, new JLabel("D"));
      labelTable.put(80, new JLabel("E"));
      labelTable.put(100, new JLabel("F"));

      slider.setLabelTable(labelTable);//在給定值處繪製標籤
      addSlider(slider, "Custom labels");


      slider = new JSlider();
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      slider.setSnapToTicks(true);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(20);

      labelTable = new Hashtable<Integer, Component>();//構造一個新的空哈希表。 

      // 添加圖像

      labelTable.put(0, new JLabel(new ImageIcon("nine.gif")));
      labelTable.put(20, new JLabel(new ImageIcon("ten.gif")));
      labelTable.put(40, new JLabel(new ImageIcon("jack.gif")));
      labelTable.put(60, new JLabel(new ImageIcon("queen.gif")));
      labelTable.put(80, new JLabel(new ImageIcon("king.gif")));
      labelTable.put(100, new JLabel(new ImageIcon("ace.gif")));

      slider.setLabelTable(labelTable);//用於指定將在給定值處繪製標籤
      addSlider(slider, "Icon labels");

      // 添加文本域

      textField = new JTextField();
      add(sliderPanel, BorderLayout.CENTER);
      add(textField, BorderLayout.SOUTH);
      pack();//調整此窗口的大小,以適合其子組件的首選大小和佈局。
   }

   /**
    * Adds a slider to the slider panel and hooks up the listener
    * @param s the slider
    * @param description the slider description
    */
   public void addSlider(JSlider s, String description)//一個讓用戶以圖形方式在有界區間內經過移動滑塊來選擇值的組件。 
   {
      s.addChangeListener(listener);
      JPanel panel = new JPanel();
      panel.add(s);
      panel.add(new JLabel(description));
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);//設置垂直對齊方式。 
      GridBagConstraints gbc = new GridBagConstraints();//設置組件間的約束關係
      gbc.gridy = sliderPanel.getComponentCount();
      gbc.anchor = GridBagConstraints.WEST;
      sliderPanel.add(panel, gbc);
   }
}

.setMajorTickSpacing(10); //設置長刻度線間隔
setMinorTickSpacing(1); //設置短刻度線間隔
setPaintTicks(true); //設置刻度線
setPaintLabels(true); //設置刻度數

實驗3:組間協同窗習:在本班課程QQ羣內,各位同窗對實驗1中存在的問題進行提問,提問時註明實驗1中的測試程序編號,負責對應程序的小組需及時對羣內提問進行回答。

 實驗總結:

學習總結:

佈局管理器

1.爲了設計美觀合理的GUI界面,須要考慮組件在容器組件中的位置和相互關係,就須要學習佈局設計的知識。

2.在java的GUI應用程序界面設計中,佈局控制經過爲容器設置佈局管理器來實現的

5種佈局管理器

(1)FlowLayout: 流佈局(Applet和Panel的默認佈局管理器)

(2)BorderLayout:邊框佈局( Window、Frame和Dialog的默認佈局管理器)

(3)GridLayout: 網格佈局

1.Swing和MVC設計模式

a.設計模式(Design pattern)是設計者一種流行的思考設計問題的方法,是一套被反覆使用,多數人知曉的,通過分類編目的,代碼設計經驗的總結。使用設計模式是爲了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性。

b.每個模式描述了一個不斷重複發生的設計問題,以及該問題的核心解決方案

c.模型-視圖-控制器設計模式(Model –ViewController )是Java EE平臺下建立 Web 應用程序的重要設計模式。

MVC設計模式

– Model(模型):是程序中用於處理程序數據邏輯的部分,一般模型負責在數據庫中存取數據。

– View(視圖):是程序中處理數據顯示的部分,一般視圖依據模型存取的數據建立。

– Controller(控制器):是程序中處理用戶交互的部分。一般控制器負責從視圖讀取數據,控制用戶輸入,並向模型發送數據。

 感覺:咱們採用了自小組協做學習模式,本身存在的問題和學習感悟記錄下來,以後在小組學習時討論解決。經過本週的小組學習,咱們收穫了自主學習所得不到的好處,那就是學習理解別人的學習感悟和知識理解。可是在本章的學習中也存在不少問題,知識容量過大,自主學習需花費較多時間。課下應該增強學習。

 

相關文章
相關標籤/搜索