張季躍 201771010139《面向對象程序設計(java)》第十四周學習總結java
第二部分:實驗部分網絡
一、實驗目的與要求ide
(1) 掌握GUI佈局管理器用法;工具
(2) 掌握各種Java Swing組件用途及經常使用API;佈局
實驗1: 導入第12章示例程序,測試程序並進行組內討論。學習
測試程序1測試
l 在elipse IDE中運行教材479頁程序12-1,結合運行結果理解程序;this
l 掌握各類佈局管理器的用法;spa
l 理解GUI界面中事件處理技術的用途。設計
l 在佈局管理應用代碼處添加註釋;
測試結果:
測試程序2
l 在elipse IDE中調試運行教材486頁程序12-2,結合運行結果理解程序;
l 掌握各類文本組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序3
l 在elipse IDE中調試運行教材489頁程序12-3,結合運行結果理解程序;
l 掌握複選框組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序4
l 在elipse IDE中調試運行教材491頁程序12-4,運行結果理解程序;
l 掌握單選按鈕組件的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序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 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序11
l 在elipse IDE中調試運行教材539頁程序12-13、12-14,結合運行結果理解程序;
l 掌握定製佈局管理器的用法。
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果
:
測試程序12
l 在elipse IDE中調試運行教材544頁程序12-15、12-16,結合運行結果理解程序;
l 掌握選項對話框的用法。
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序13
l 在elipse IDE中調試運行教材552頁程序12-17、12-18,結合運行結果理解程序;
l 掌握對話框的建立方法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序14
l 在elipse IDE中調試運行教材556頁程序12-19、12-20,結合運行結果理解程序;
l 掌握對話框的數據交換用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序15
l 在elipse IDE中調試運行教材556頁程序12-2一、12-2212-23,結合程序運行結果理解程序;
l 掌握文件對話框的用法;
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
測試程序16
l 在elipse IDE中調試運行教材570頁程序12-24,結合運行結果理解程序;
l 瞭解顏色選擇器的用法。
l 記錄示例代碼閱讀理解中存在的問題與疑惑。
測試結果:
實驗2:組內討論反思本組負責程序,理解程序整體結構,梳理程序GUI設計中應用的相關組件,整理相關組件的API,對程序中組件應用的相關代碼添加註釋。
實驗:
本組負責的是第十二個實驗,即optionDialog(選項對話框),該測試程序中GUI設計中應用的相關組件有:
LayoutManager: 佈局接口,各類佈局類均爲其子類
frame.setLayout(佈局對象) //設置窗口布局
null:不使用佈局,此時能夠容器內的組件能夠調用方法,經過座標以及尺寸進行組件定位:
組件定位方法爲void setBounds(int x軸,int y軸,int 寬度,int 高度)
Jframe:建立該組件即建立了一個窗口。窗口是一個容器組件,全部內容都可以放入其中。
JPanel:建立一個可加入到JFrame窗口的容器。
JButtton:建立一個按鈕。
JRadioButton:實現一個單選按鈕,此按鈕項可被選擇或取消選擇,並可爲用戶顯示其狀態。與 ButtonGroup 對象配合使用可建立一組按鈕,一次只能選擇其中的一個按鈕。
JLabel:用於短文本字符串或圖像或兩者的顯示區。
實驗代碼:
package optionDialog;
import javax.swing.*;
/**
* A panel with radio buttons inside a titled border.
*/
public class ButtonPanel extends JPanel
//建立一個可加入到JFrame窗體的容器//
{
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();
//加入到ButtonGroup中的JRdioButton只有一個能夠被選中,而每一個JRadioButton獨享一個監聽器,當其被選中時執行監聽器中處理方法。//
// make one radio button for each option
for (String option : options)
{
JRadioButton b = new JRadioButton(option);
//實現一個單選按鈕,此按鈕項可被選擇或取消選擇,並可爲用戶顯示其狀態。與 ButtonGroup 對象配合使用可建立一組按鈕,一次只能選擇其中的一個按鈕。//
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
//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();
//建立一個可加入到JFrame窗體的容器//
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();
//建立一個可加入到JFrame窗體的容器//
JButton showButton = new JButton("Show");
// 建立一個顯示爲「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);
}
}
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);
});
}
}
實驗3:組間協同窗習:在本班課程QQ羣內,各位同窗對實驗1中存在的問題進行提問,提問時註明實驗1中的測試程序編號,負責對應程序的小組需及時對羣內提問進行回答。
第三部分:實驗總結:
在這一週的學習過程當中,我瞭解了Swing用戶界面組件,GUI相關組件。但整體而言,我對這一週學習的知識掌握的並不全面,就算是本身負責的實驗部分,也有許可能是在網絡上查找相關資料後才相對掌握了一些,即便如此,對於實驗內容我瞭解的依然不夠全面。而對於這一週的小組協做學習,我認爲這對於咱們的學習的確有必定的幫助,至少對於每一個負責的部分咱們都有互相討論的人選。