王之泰201771010131《面向对象程序设计(java)》第十五周学习总结
第一部分:理論知識學(xué)習(xí)部分
第13 章 部署應(yīng)用程序
1.jar文件
a) java 程序的打包:編譯完成后,員 將.class 文件壓縮打包為 .jar 文件后, GUI 界面 程序就可以直接雙擊圖標(biāo)運(yùn)行。
b) jar .jar文件( Java 歸檔)既可 以包含類文件,也歸檔)既可 以包含類文件,也以包含諸如圖像和聲音這些其它類型的文件。 以包含諸如圖像和聲音這些其它類型的文件。 以包含諸如圖像和聲音這些其它類型的文件。 以包含諸如圖像和聲音這些其它類型的文件。
c) JAR 文件是壓縮的,它使用 ZIP 壓縮格式。 壓縮格式。 壓縮格式。 壓縮格式。 壓縮格式
1) jar命令
a)jar命令格式:
jar {ctxu} [vfm0Me] jar-file] [manifest file]? [entry -point] [-C dir ] files ...
2)運(yùn)行 JAR 文件
a) 用戶可以通過下面的命令來啟動(dòng)應(yīng)程序:
java –jar MyProgram.jar
b) 窗口操作系統(tǒng),可通過雙擊 JAR 文件圖標(biāo)來啟動(dòng)應(yīng)用程序。
3)?資源
a) Java 中,應(yīng)用程序使的類通常需要一些相關(guān)數(shù) 中,應(yīng)用程序使的類通常需要一些相關(guān)數(shù) 據(jù)文件,這些稱為資源 (Resource) 。
–圖像和聲音文件。
–帶有消息字符串和按鈕標(biāo)簽的文本件。 帶有消息字符串和按鈕標(biāo)簽的文本件。 帶有消息字符串和按鈕標(biāo)簽的文本件。
–二進(jìn)制數(shù)據(jù)文件,如:描述地圖布局的。 二進(jìn)制數(shù)據(jù)文件,如:描述地圖布局的。 二進(jìn)制數(shù)據(jù)文件,如:描述地圖布局的。
2.應(yīng)用程序首選項(xiàng)存儲
?
3.Java web start
?
第二部分:實(shí)驗(yàn)部分——GUI編程練習(xí)與應(yīng)用程序部署
實(shí)驗(yàn)時(shí)間?2018-12-6
1、實(shí)驗(yàn)?zāi)康呐c要求
(1)?掌握J(rèn)ava應(yīng)用程序的打包操作;
(2)?了解應(yīng)用程序存儲配置信息的兩種方法;
(3)?掌握基于JNLP協(xié)議的java?Web?Start應(yīng)用程序的發(fā)布方法;
(5)?掌握J(rèn)ava?GUI?編程技術(shù)。
2、實(shí)驗(yàn)內(nèi)容和步驟
實(shí)驗(yàn)1:?導(dǎo)入第13章示例程序,測試程序并進(jìn)行代碼注釋。
測試程序1
1.在elipse?IDE中調(diào)試運(yùn)行教材585頁程序13-1,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.將所生成的JAR文件移到另外一個(gè)不同的目錄中,再運(yùn)行該歸檔文件,以便確認(rèn)程序是從JAR文件中,而不是從當(dāng)前目錄中讀取的資源。
3.掌握創(chuàng)建JAR文件的方法;
?
1 package resource; 2 3 import java.awt.*; 4 import java.io.*; 5 import java.net.*; 6 import java.util.*; 7 import javax.swing.*; 8 9 /** 10 * @version 1.41 2015-06-12 11 * @author Cay Horstmann 12 */ 13 public class ResourceTest 14 { 15 public static void main(String[] args) 16 { 17 EventQueue.invokeLater(() -> { 18 JFrame frame = new ResourceTestFrame(); 19 frame.setTitle("ResourceTest"); 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.setVisible(true); 22 }); 23 } 24 } 25 26 /** 27 * 一個(gè)加載圖像和文本資源的框架。 28 */ 29 class ResourceTestFrame extends JFrame 30 { 31 private static final int DEFAULT_WIDTH = 300; 32 private static final int DEFAULT_HEIGHT = 300; 33 34 public ResourceTestFrame() 35 { 36 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 37 URL aboutURL = getClass().getResource("about.gif"); 38 Image img = new ImageIcon(aboutURL).getImage(); 39 setIconImage(img); 40 41 JTextArea textArea = new JTextArea(); 42 InputStream stream = getClass().getResourceAsStream("about.txt"); 43 try (Scanner in = new Scanner(stream, "UTF-8")) 44 { 45 while (in.hasNext()) 46 textArea.append(in.nextLine() + "\n"); 47 } 48 add(textArea); 49 } 50 }?
?
測試程序2
1.在elipse?IDE中調(diào)試運(yùn)行教材583頁-584程序13-2,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.了解Properties類中常用的方法;
?
1 package properties; 2 3 import java.awt.EventQueue; 4 import java.awt.event.*; 5 import java.io.*; 6 import java.util.Properties; 7 8 import javax.swing.*; 9 10 /** 11 * 一個(gè)測試屬性的程序。 程序記住幀的位置、大小和標(biāo)題 12 * @version 1.01 2015-06-16 13 * @author Cay Horstmann 14 */ 15 public class PropertiesTest 16 { 17 public static void main(String[] args) 18 { 19 EventQueue.invokeLater(() -> { 20 PropertiesFrame frame = new PropertiesFrame(); 21 frame.setVisible(true); 22 }); 23 } 24 } 25 26 /** 27 * 從屬性文件和更新恢復(fù)位置和大小的框架。退出時(shí)的屬性。 28 */ 29 class PropertiesFrame extends JFrame 30 { 31 private static final int DEFAULT_WIDTH = 300; 32 private static final int DEFAULT_HEIGHT = 200; 33 34 private File propertiesFile; 35 private Properties settings; 36 37 public PropertiesFrame() 38 { 39 // 從屬性獲取位置、大小、標(biāo)題 40 41 String userDir = System.getProperty("user.home"); 42 File propertiesDir = new File(userDir, ".corejava"); 43 if (!propertiesDir.exists()) propertiesDir.mkdir(); 44 propertiesFile = new File(propertiesDir, "program.properties"); 45 46 Properties defaultSettings = new Properties(); 47 defaultSettings.setProperty("left", "0"); 48 defaultSettings.setProperty("top", "0"); 49 defaultSettings.setProperty("width", "" + DEFAULT_WIDTH); 50 defaultSettings.setProperty("height", "" + DEFAULT_HEIGHT); 51 defaultSettings.setProperty("title", ""); 52 53 settings = new Properties(defaultSettings); 54 55 if (propertiesFile.exists()) 56 try (InputStream in = new FileInputStream(propertiesFile)) 57 { 58 settings.load(in); 59 } 60 catch (IOException ex) 61 { 62 ex.printStackTrace(); 63 } 64 65 int left = Integer.parseInt(settings.getProperty("left")); 66 int top = Integer.parseInt(settings.getProperty("top")); 67 int width = Integer.parseInt(settings.getProperty("width")); 68 int height = Integer.parseInt(settings.getProperty("height")); 69 setBounds(left, top, width, height); 70 71 // 如果沒有標(biāo)題,請?jiān)儐栍脩?/span> 72 73 String title = settings.getProperty("title"); 74 if (title.equals("")) 75 title = JOptionPane.showInputDialog("Please supply a frame title:"); 76 if (title == null) title = ""; 77 setTitle(title); 78 79 addWindowListener(new WindowAdapter() 80 { 81 public void windowClosing(WindowEvent event) 82 { 83 settings.setProperty("left", "" + getX()); 84 settings.setProperty("top", "" + getY()); 85 settings.setProperty("width", "" + getWidth()); 86 settings.setProperty("height", "" + getHeight()); 87 settings.setProperty("title", getTitle()); 88 try (OutputStream out = new FileOutputStream(propertiesFile)) 89 { 90 settings.store(out, "Program Properties"); 91 } 92 catch (IOException ex) 93 { 94 ex.printStackTrace(); 95 } 96 System.exit(0); 97 } 98 }); 99 } 100 }?
?
測試程序3
1.在elipse?IDE中調(diào)試運(yùn)行教材593頁-594程序13-3,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.了解Preferences類中常用的方法;
?
1 package preferences; 2 3 import java.awt.*; 4 import java.io.*; 5 import java.util.prefs.*; 6 7 import javax.swing.*; 8 import javax.swing.filechooser.*; 9 10 /** 11 * 一個(gè)測試偏好設(shè)置的程序。程序記住框架。位置、大小和標(biāo)題。 12 * @version 1.03 2015-06-12 13 * @author Cay Horstmann 14 */ 15 public class PreferencesTest 16 { 17 public static void main(String[] args) 18 { 19 EventQueue.invokeLater(() -> { 20 PreferencesFrame frame = new PreferencesFrame(); 21 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 frame.setVisible(true); 23 }); 24 } 25 } 26 27 /** 28 * 從用戶偏好恢復(fù)位置和大小并在退出時(shí)更新首選項(xiàng)的框架。 29 */ 30 class PreferencesFrame extends JFrame 31 { 32 private static final int DEFAULT_WIDTH = 300; 33 private static final int DEFAULT_HEIGHT = 200; 34 private Preferences root = Preferences.userRoot(); 35 private Preferences node = root.node("/com/horstmann/corejava"); 36 37 public PreferencesFrame() 38 { 39 // 從偏好獲得位置、大小、標(biāo)題 40 41 int left = node.getInt("left", 0); 42 int top = node.getInt("top", 0); 43 int width = node.getInt("width", DEFAULT_WIDTH); 44 int height = node.getInt("height", DEFAULT_HEIGHT); 45 setBounds(left, top, width, height); 46 47 // 如果沒有標(biāo)題,請?jiān)儐栍脩?/span> 48 49 String title = node.get("title", ""); 50 if (title.equals("")) 51 title = JOptionPane.showInputDialog("Please supply a frame title:"); 52 if (title == null) title = ""; 53 setTitle(title); 54 55 // 設(shè)置顯示XML文件的文件選擇器 56 57 final JFileChooser chooser = new JFileChooser(); 58 chooser.setCurrentDirectory(new File(".")); 59 chooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml")); 60 61 // 設(shè)置菜單 62 63 JMenuBar menuBar = new JMenuBar(); 64 setJMenuBar(menuBar); 65 JMenu menu = new JMenu("File"); 66 menuBar.add(menu); 67 68 JMenuItem exportItem = new JMenuItem("Export preferences"); 69 menu.add(exportItem); 70 exportItem 71 .addActionListener(event -> { 72 if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) 73 { 74 try 75 { 76 savePreferences(); 77 OutputStream out = new FileOutputStream(chooser 78 .getSelectedFile()); 79 node.exportSubtree(out); 80 out.close(); 81 } 82 catch (Exception e) 83 { 84 e.printStackTrace(); 85 } 86 } 87 }); 88 89 JMenuItem importItem = new JMenuItem("Import preferences"); 90 menu.add(importItem); 91 importItem 92 .addActionListener(event -> { 93 if (chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) 94 { 95 try 96 { 97 InputStream in = new FileInputStream(chooser 98 .getSelectedFile()); 99 Preferences.importPreferences(in); 100 in.close(); 101 } 102 catch (Exception e) 103 { 104 e.printStackTrace(); 105 } 106 } 107 }); 108 109 JMenuItem exitItem = new JMenuItem("Exit"); 110 menu.add(exitItem); 111 exitItem.addActionListener(event -> { 112 savePreferences(); 113 System.exit(0); 114 }); 115 } 116 117 public void savePreferences() 118 { 119 node.putInt("left", getX()); 120 node.putInt("top", getY()); 121 node.putInt("width", getWidth()); 122 node.putInt("height", getHeight()); 123 node.put("title", getTitle()); 124 } 125 }?
?
測試程序4
1.在elipse?IDE中調(diào)試運(yùn)行教材619頁-622程序13-6,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.掌握基于JNLP協(xié)議的java?Web?Start應(yīng)用程序的發(fā)布方法。
?
1 package webstart; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * 具有可作為Java Web啟動(dòng)應(yīng)用程序部署的計(jì)算歷史的計(jì)算器。 8 * @version 1.04 2015-06-12 9 * @author Cay Horstmann 10 */ 11 public class Calculator 12 { 13 public static void main(String[] args) 14 { 15 EventQueue.invokeLater(() -> { 16 CalculatorFrame frame = new CalculatorFrame(); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 }?
1 package webstart; 2 3 import java.io.BufferedReader; 4 import java.io.ByteArrayInputStream; 5 import java.io.ByteArrayOutputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.OutputStream; 11 import java.io.PrintStream; 12 import java.net.MalformedURLException; 13 import java.net.URL; 14 15 import javax.jnlp.BasicService; 16 import javax.jnlp.FileContents; 17 import javax.jnlp.FileOpenService; 18 import javax.jnlp.FileSaveService; 19 import javax.jnlp.PersistenceService; 20 import javax.jnlp.ServiceManager; 21 import javax.jnlp.UnavailableServiceException; 22 import javax.swing.JFrame; 23 import javax.swing.JMenu; 24 import javax.swing.JMenuBar; 25 import javax.swing.JMenuItem; 26 import javax.swing.JOptionPane; 27 28 /** 29 * 一個(gè)帶有計(jì)算器面板和菜單的框架,用來載入和保存計(jì)算器歷史。 30 */ 31 public class CalculatorFrame extends JFrame 32 { 33 private CalculatorPanel panel; 34 35 public CalculatorFrame() 36 { 37 setTitle(); 38 panel = new CalculatorPanel(); 39 add(panel); 40 41 JMenu fileMenu = new JMenu("File"); 42 JMenuBar menuBar = new JMenuBar(); 43 menuBar.add(fileMenu); 44 setJMenuBar(menuBar); 45 46 JMenuItem openItem = fileMenu.add("Open"); 47 openItem.addActionListener(event -> open()); 48 JMenuItem saveItem = fileMenu.add("Save"); 49 saveItem.addActionListener(event -> save()); 50 51 pack(); 52 } 53 54 /** 55 * 從持久存儲中獲取標(biāo)題,或者在沒有以前的條目的情況下向用戶請求標(biāo)題。 56 */ 57 public void setTitle() 58 { 59 try 60 { 61 String title = null; 62 63 BasicService basic = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); 64 URL codeBase = basic.getCodeBase(); 65 66 PersistenceService service = (PersistenceService) ServiceManager 67 .lookup("javax.jnlp.PersistenceService"); 68 URL key = new URL(codeBase, "title"); 69 70 try 71 { 72 FileContents contents = service.get(key); 73 InputStream in = contents.getInputStream(); 74 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 75 title = reader.readLine(); 76 } 77 catch (FileNotFoundException e) 78 { 79 title = JOptionPane.showInputDialog("Please supply a frame title:"); 80 if (title == null) return; 81 82 service.create(key, 100); 83 FileContents contents = service.get(key); 84 OutputStream out = contents.getOutputStream(true); 85 PrintStream printOut = new PrintStream(out); 86 printOut.print(title); 87 } 88 setTitle(title); 89 } 90 catch (UnavailableServiceException | IOException e) 91 { 92 JOptionPane.showMessageDialog(this, e); 93 } 94 } 95 96 /** 97 * 打開歷史文件并更新顯示。 98 */ 99 public void open() 100 { 101 try 102 { 103 FileOpenService service = (FileOpenService) ServiceManager 104 .lookup("javax.jnlp.FileOpenService"); 105 FileContents contents = service.openFileDialog(".", new String[] { "txt" }); 106 107 JOptionPane.showMessageDialog(this, contents.getName()); 108 if (contents != null) 109 { 110 InputStream in = contents.getInputStream(); 111 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 112 String line; 113 while ((line = reader.readLine()) != null) 114 { 115 panel.append(line); 116 panel.append("\n"); 117 } 118 } 119 } 120 catch (UnavailableServiceException e) 121 { 122 JOptionPane.showMessageDialog(this, e); 123 } 124 catch (IOException e) 125 { 126 JOptionPane.showMessageDialog(this, e); 127 } 128 } 129 130 /** 131 * 將計(jì)算器歷史保存到文件中。 132 */ 133 public void save() 134 { 135 try 136 { 137 ByteArrayOutputStream out = new ByteArrayOutputStream(); 138 PrintStream printOut = new PrintStream(out); 139 printOut.print(panel.getText()); 140 InputStream data = new ByteArrayInputStream(out.toByteArray()); 141 FileSaveService service = (FileSaveService) ServiceManager 142 .lookup("javax.jnlp.FileSaveService"); 143 service.saveFileDialog(".", new String[] { "txt" }, data, "calc.txt"); 144 } 145 catch (UnavailableServiceException e) 146 { 147 JOptionPane.showMessageDialog(this, e); 148 } 149 catch (IOException e) 150 { 151 JOptionPane.showMessageDialog(this, e); 152 } 153 } 154 }?
1 package webstart; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.text.*; 7 8 /** 9 具有計(jì)算器按鈕和結(jié)果顯示的面板。 10 */ 11 public class CalculatorPanel extends JPanel 12 { 13 private JTextArea display; 14 private JPanel panel; 15 private double result; 16 private String lastCommand; 17 private boolean start; 18 19 /** 20 列出面板。 21 */ 22 public CalculatorPanel() 23 { 24 setLayout(new BorderLayout()); 25 26 result = 0; 27 lastCommand = "="; 28 start = true; 29 30 // 添加顯示 31 display = new JTextArea(10, 20); 32 33 add(new JScrollPane(display), BorderLayout.NORTH); 34 35 ActionListener insert = new InsertAction(); 36 ActionListener command = new CommandAction(); 37 38 // 在4×4網(wǎng)格中添加按鈕 39 40 panel = new JPanel(); 41 panel.setLayout(new GridLayout(4, 4)); 42 43 addButton("7", insert); 44 addButton("8", insert); 45 addButton("9", insert); 46 addButton("/", command); 47 48 addButton("4", insert); 49 addButton("5", insert); 50 addButton("6", insert); 51 addButton("*", command); 52 53 addButton("1", insert); 54 addButton("2", insert); 55 addButton("3", insert); 56 addButton("-", command); 57 58 addButton("0", insert); 59 addButton(".", insert); 60 addButton("=", command); 61 addButton("+", command); 62 63 add(panel, BorderLayout.CENTER); 64 } 65 66 /** 67 獲取歷史文本。 68 @return the calculator history 69 */ 70 public String getText() 71 { 72 return display.getText(); 73 } 74 75 /** 76 將字符串追加到歷史文本中。 77 @param s the string to append 78 */ 79 public void append(String s) 80 { 81 display.append(s); 82 } 83 84 /** 85 向中心面板添加一個(gè)按鈕。 86 @param label the button label 87 @param listener the button listener 88 */ 89 private void addButton(String label, ActionListener listener) 90 { 91 JButton button = new JButton(label); 92 button.addActionListener(listener); 93 panel.add(button); 94 } 95 96 /** 97 此操作將按鈕操作字符串插入到顯示文本結(jié)束。 98 */ 99 private class InsertAction implements ActionListener 100 { 101 public void actionPerformed(ActionEvent event) 102 { 103 String input = event.getActionCommand(); 104 start = false; 105 display.append(input); 106 } 107 } 108 109 /** 110 此操作執(zhí)行按鈕的命令。動(dòng)作字符串表示。 111 */ 112 private class CommandAction implements ActionListener 113 { 114 public void actionPerformed(ActionEvent event) 115 { 116 String command = event.getActionCommand(); 117 118 if (start) 119 { 120 if (command.equals("-")) 121 { 122 display.append(command); 123 start = false; 124 } 125 else 126 lastCommand = command; 127 } 128 else 129 { 130 try 131 { 132 int lines = display.getLineCount(); 133 int lineStart = display.getLineStartOffset(lines - 1); 134 int lineEnd = display.getLineEndOffset(lines - 1); 135 String value = display.getText(lineStart, lineEnd - lineStart); 136 display.append(" "); 137 display.append(command); 138 calculate(Double.parseDouble(value)); 139 if (command.equals("=")) 140 display.append("\n" + result); 141 lastCommand = command; 142 display.append("\n"); 143 start = true; 144 } 145 catch (BadLocationException e) 146 { 147 e.printStackTrace(); 148 } 149 } 150 } 151 } 152 153 /** 154 執(zhí)行懸而未決的計(jì)算。 155 @param x the value to be accumulated with the prior result. 156 */ 157 public void calculate(double x) 158 { 159 if (lastCommand.equals("+")) result += x; 160 else if (lastCommand.equals("-")) result -= x; 161 else if (lastCommand.equals("*")) result *= x; 162 else if (lastCommand.equals("/")) result /= x; 163 else if (lastCommand.equals("=")) result = x; 164 } 165 }?
實(shí)驗(yàn)2:GUI綜合編程練習(xí)
按實(shí)驗(yàn)十四分組名單,組內(nèi)討論完成以下編程任務(wù):
練習(xí)1:采用GUI界面設(shè)計(jì)以下程序,并進(jìn)行部署與發(fā)布:
1.編制一個(gè)程序,將身份證號.txt?中的信息讀入到內(nèi)存中;
2.按姓名字典序輸出人員信息;
3.查詢最大年齡的人員信息;
4.查詢最小年齡人員信息;
5.輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;
6.查詢?nèi)藛T中是否有你的同鄉(xiāng)。
7.輸入身份證信息,查詢所提供身份證號的人員信息,要求輸入一個(gè)身份證數(shù)字時(shí),查詢界面就顯示滿足查詢條件的查詢結(jié)果,且隨著輸入的數(shù)字的增多,查詢匹配的范圍逐漸縮小。
?
1 package shiwuzhou; 2 3 import java.awt.Dimension; 4 import java.awt.EventQueue; 5 import java.awt.Toolkit; 6 7 import javax.swing.JFrame; 8 9 public class Out { 10 11 public static void main (String args[]) 12 { 13 Toolkit t=Toolkit.getDefaultToolkit(); 14 Dimension s=t.getScreenSize(); 15 EventQueue.invokeLater(() -> { 16 JFrame frame = new Main1(); 17 frame.setBounds(0, 0,(int)s.getWidth(),(int)s.getHeight()); 18 frame.setTitle("第四組"); 19 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 frame.setVisible(true); 21 }); 22 } 23 24 } 1 package shiwuzhou; 2 3 import java.awt.BorderLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.io.BufferedReader; 7 import java.io.File; 8 import java.io.FileInputStream; 9 import java.io.FileNotFoundException; 10 import java.io.IOException; 11 import java.io.InputStreamReader; 12 import java.util.*; 13 import java.util.Timer; 14 import javax.swing.*; 15 16 public class Main1 extends JFrame 17 { 18 private static ArrayList<Person> Personlist; 19 20 21 Scanner scanner = new Scanner(System.in); 22 File file = new File("D:\\身份證號.txt"); 23 24 private JPanel Panel; 25 private JLabel JLabel1; 26 private JButton Button,Button2,Button3; 27 private JTextArea text,text1,text2,text3; 28 boolean tru=true; 29 30 31 32 public Main1() { 33 34 35 Panel = new JPanel();Panel.setLayout(null); 36 Button = new JButton("1:按姓名字典序輸出人員信息"); 37 Button2 = new JButton("2:查詢最大年齡與最小年齡人員信息"); 38 Button3 = new JButton("查詢相近年齡"); 39 JLabel1 = new JLabel("輸入身份證號或者地址查詢"); 40 JLabel1.setBounds(900, 50, 400, 30); 41 42 text=new JTextArea(30,80);text.setBounds(50, 180, 700, 700); 43 text1=new JTextArea(1,30);text1.setBounds(900, 80, 400, 30); 44 text2=new JTextArea(30,80);text2.setBounds(900,180,700, 700); 45 text3=new JTextArea(30,80);text3.setBounds(420,100,200,40); 46 47 Button.addActionListener(new Action());Button.setBounds(50,50,300,40); 48 Button2.addActionListener(new Action1());Button2.setBounds(50,100,300,40); 49 Button3.addActionListener(new Action2());Button3.setBounds(650,100,120,40); 50 Panel.add(JLabel1); 51 Panel.add(Button); 52 Panel.add(Button2); 53 Panel.add(Button3); 54 Panel.add(text); 55 Panel.add(text2); 56 Panel.add(text1); 57 Panel.add(text3); 58 add(Panel); 59 60 61 Timer timer = new Timer(); 62 TimerTask timeTask=new TimerTask() { 63 64 @Override 65 public void run() 66 { 67 // TODO Auto-generated method stub 68 text2.setText(null); 69 String place=text1.getText().toString().trim(); 70 for (int i = 0; i <Personlist.size(); i++) 71 { 72 73 String Str=(String)Personlist.get(i).getbirthplace(); 74 if(Str.contains(place)&&!place.equals("")) 75 { 76 text2.append(Personlist.get(i).toString()); 77 } 78 } 79 for (int i = 0; i <Personlist.size(); i++) 80 { 81 82 String Str=(String)Personlist.get(i).getID(); 83 if(Str.contains(place)&&!place.equals("")) 84 { 85 text2.append(Personlist.get(i).toString()); 86 } 87 } 88 89 } 90 91 };timer.schedule(timeTask, 0,100); 92 93 Personlist = new ArrayList<>(); 94 try { 95 FileInputStream fis = new FileInputStream(file); 96 BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 97 String temp = null; 98 while ((temp = in.readLine()) != null) { 99 Scanner linescanner = new Scanner(temp); 100 linescanner.useDelimiter(" "); 101 String name = linescanner.next(); 102 String ID = linescanner.next(); 103 String sex = linescanner.next(); 104 String age = linescanner.next(); 105 String place =linescanner.nextLine(); 106 Person Person = new Person(); 107 Person.setname(name); 108 Person.setID(ID); 109 Person.setsex(sex); 110 int a = Integer.parseInt(age); 111 Person.setage(a); 112 Person.setbirthplace(place); 113 Personlist.add(Person); 114 115 } 116 } catch (FileNotFoundException e) { 117 System.out.println("查找不到信息"); 118 e.printStackTrace(); 119 } catch (IOException e) { 120 System.out.println("信息讀取有誤"); 121 e.printStackTrace(); 122 } 123 124 125 } 126 127 128 129 130 private class Action implements ActionListener 131 { 132 public void actionPerformed(ActionEvent event) 133 { 134 text.setText(null); 135 Collections.sort(Personlist); 136 text.append(Personlist.toString()); 137 } 138 139 } 140 141 private class Action1 implements ActionListener 142 { 143 public void actionPerformed(ActionEvent event) 144 { 145 text.setText(null); 146 int max=0,min=100;int j,k1 = 0,k2=0; 147 for(int i=1;i<Personlist.size();i++) 148 { 149 j=Personlist.get(i).getage(); 150 if(j>max) 151 { 152 max=j; 153 k1=i; 154 } 155 if(j<min) 156 { 157 min=j; 158 k2=i; 159 } 160 } 161 text.append("年齡最大: "+Personlist.get(k1)+"\n"+"年齡最小: "+Personlist.get(k2)); 162 } 163 164 } 165 166 private class Action2 implements ActionListener 167 { 168 public void actionPerformed(ActionEvent event) 169 { 170 text.setText(null); 171 int a = Integer.parseInt(text3.getText().toString().trim()); 172 int d_value=a-Personlist.get(agenear(a)).getage(); 173 174 for (int i = 0; i < Personlist.size(); i++) 175 { 176 int p=Personlist.get(i).getage()-a; 177 178 if(p==d_value||-p==d_value) text.append(Personlist.get(i).toString()); 179 } 180 } 181 182 } 183 184 185 public static int agenear(int age) { 186 187 int j=0,min=53,d_value=0,k=0; 188 for (int i = 0; i < Personlist.size(); i++) 189 { 190 d_value=Personlist.get(i).getage()-age; 191 if(d_value<0) d_value=-d_value; 192 if (d_value<min) 193 { 194 min=d_value; 195 k=i; 196 } 197 198 } return k; 199 200 } 201 202 }?
1 package shiwuzhou; 2 3 public class Person implements Comparable<Person> { 4 private String name; 5 private String ID; 6 private int age; 7 private String sex; 8 private String birthplace; 9 10 public String getname() 11 { 12 return name; 13 } 14 public void setname(String name) 15 { 16 this.name = name; 17 } 18 public String getID() 19 { 20 return ID; 21 } 22 public void setID(String ID) 23 { 24 this.ID= ID; 25 } 26 public int getage() 27 { 28 return age; 29 } 30 public void setage(int age) 31 { 32 this.age= age; 33 } 34 public String getsex() 35 { 36 return sex; 37 } 38 public void setsex(String sex) 39 { 40 this.sex= sex; 41 } 42 public String getbirthplace() 43 { 44 return birthplace; 45 } 46 public void setbirthplace(String birthplace) 47 { 48 this.birthplace= birthplace; 49 } 50 51 public int compareTo(Person o) 52 { 53 return this.name.compareTo(o.getname()); 54 } 55 56 public String toString() 57 { 58 return name+"\t"+sex+"\t"+age+"\t"+ID+"\t"+birthplace+"\n"; 59 60 } 61 62 63 64 }?
?
練習(xí)2:采用GUI界面設(shè)計(jì)以下程序,并進(jìn)行部署與發(fā)布
1.編寫一個(gè)計(jì)算器類,可以完成加、減、乘、除的操作
2.利用計(jì)算機(jī)類,設(shè)計(jì)一個(gè)小學(xué)生100以內(nèi)數(shù)的四則運(yùn)算練習(xí)程序,由計(jì)算機(jī)隨機(jī)產(chǎn)生10道加減乘除練習(xí)題,學(xué)生輸入答案,由程序檢查答案是否正確,每道題正確計(jì)10分,錯(cuò)誤不計(jì)分,10道題測試結(jié)束后給出測試總分;
3.將程序中測試練習(xí)題及學(xué)生答題結(jié)果輸出到文件,文件名為test.txt。
?
1 package shiwuzhou; 2 3 import java.awt.Dimension; 4 import java.awt.EventQueue; 5 import java.awt.Toolkit; 6 7 import javax.swing.JFrame; 8 9 public class New { 10 11 public static void main (String args[]) 12 { 13 Toolkit t=Toolkit.getDefaultToolkit(); 14 Dimension s=t.getScreenSize(); 15 EventQueue.invokeLater(() -> { 16 JFrame frame = new Demo(); 17 frame.setBounds(0, 0,(int)s.getWidth()/2,(int)s.getHeight()/2); 18 frame.setTitle("第四組"); 19 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 frame.setVisible(true); 21 }); 22 } 23 24 }?
?
1 package shiwuzhou; 2 3 import java.awt.Font; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.io.FileNotFoundException; 7 import java.io.PrintWriter; 8 import java.util.Collections; 9 import java.util.Scanner; 10 11 import javax.swing.*; 12 13 import java.math.*; 14 15 16 public class Demo extends JFrame { 17 18 private String[] c=new String[10]; 19 private String[] c1=new String[10]; 20 private int[] list=new int[10]; 21 int i=0,i1=0,sum = 0; 22 private PrintWriter out = null; 23 private JTextArea text,text1; 24 private int counter; 25 26 public Demo() { 27 JPanel Panel = new JPanel(); 28 Panel.setLayout(null); 29 JLabel JLabel1=new JLabel(""); 30 JLabel1.setBounds(500, 800, 400, 30); 31 JLabel1.setFont(new Font("Courier",Font.PLAIN,35)); 32 JButton Button = new JButton("生成題目"); 33 Button.setBounds(50,150,150,50); 34 Button.setFont(new Font("Courier",Font.PLAIN,20)); 35 Button.addActionListener(new Action()); 36 JButton Button2 = new JButton("確定答案"); 37 Button2.setBounds(300,150,150,50); 38 Button2.setFont(new Font("Courier",Font.PLAIN,20)); 39 Button2.addActionListener(new Action1()); 40 JButton Button3 = new JButton("讀出文件"); 41 Button3.setBounds(500,150,150,50); 42 Button3.setFont(new Font("Courier",Font.PLAIN,20)); 43 Button3.addActionListener(new Action2()); 44 text=new JTextArea(30,80);text.setBounds(30, 50, 200, 50); 45 text.setFont(new Font("Courier",Font.PLAIN,35)); 46 text1=new JTextArea(30,80); 47 text1.setBounds(270, 50, 200, 50); 48 text1.setFont(new Font("Courier",Font.PLAIN,35)); 49 50 Panel.add(text); 51 Panel.add(text1); 52 53 Panel.add(Button); 54 Panel.add(Button2); 55 Panel.add(Button3); 56 Panel.add(JLabel1); 57 add(Panel); 58 59 60 61 62 63 64 65 } 66 67 private class Action implements ActionListener 68 { 69 public void actionPerformed(ActionEvent event) 70 { 71 text1.setText("0"); 72 if(i<10) { 73 74 int a = 1+(int)(Math.random() * 99); 75 int b = 1+(int)(Math.random() * 99); 76 int m= (int) Math.round(Math.random() * 3); 77 switch(m) 78 { 79 case 0: 80 while(a<b){ 81 b = (int) Math.round(Math.random() * 100); 82 a = (int) Math.round(Math.random() * 100); 83 } 84 c[i]=(i+":"+a+"/"+b+"="); 85 list[i]=Math.floorDiv(a, b); 86 text.setText(i+":"+a+"/"+b+"="); 87 i++; 88 break; 89 case 1: 90 c[i]=(i+":"+a+"*"+b+"="); 91 list[i]=Math.multiplyExact(a, b); 92 text.setText(i+":"+a+"*"+b+"="); 93 i++; 94 break; 95 case 2: 96 c[i]=(i+":"+a+"+"+b+"="); 97 list[i]=Math.addExact(a, b); 98 text.setText(i+":"+a+"+"+b+"="); 99 i++; 100 break ; 101 case 3: 102 while(a<=b){ 103 b = (int) Math.round(Math.random() * 100); 104 a = (int) Math.round(Math.random() * 100); 105 } 106 c[i]=(i+":"+a+"-"+b+"="); 107 text.setText(i+":"+a+"-"+b+"="); 108 list[i]=Math.subtractExact(a, b); 109 i++; 110 break ; 111 } 112 } 113 } 114 } 115 private class Action1 implements ActionListener 116 { 117 public void actionPerformed(ActionEvent event) 118 { 119 if(i<10) { 120 text.setText(null); 121 String daan=text1.getText().toString().trim(); 122 int a = Integer.parseInt(daan); 123 if(text1.getText()!="") { 124 if(list[i1]==a) sum+=10; 125 } 126 c1[i1]=daan; 127 i1++; 128 } 129 } 130 } 131 132 133 private class Action2 implements ActionListener 134 { 135 public void actionPerformed(ActionEvent event) 136 { 137 138 try { 139 out = new PrintWriter("text.txt"); 140 } catch (FileNotFoundException e) { 141 // TODO Auto-generated catch block 142 e.printStackTrace(); 143 } 144 for(int counter=0;counter<10;counter++) 145 { 146 out.println(c[counter]+c1[counter]); 147 } 148 out.println("成績"+sum); 149 out.close(); 150 151 } 152 153 } 154 }?
?
第三部分:總結(jié)
本周的學(xué)習(xí)中,雖然通過組內(nèi)學(xué)習(xí),解決了很多問題,但還是有些難題無法解決,我們遇到的問題意是對于Java Web Start部署應(yīng)用程序這一部分的內(nèi)容理解還不很透徹。GUI界面設(shè)計(jì)過程中如何使界面更美觀也是 需要努力的地方。
轉(zhuǎn)載于:https://www.cnblogs.com/hackerZT-7/p/10078113.html
總結(jié)
以上是生活随笔為你收集整理的王之泰201771010131《面向对象程序设计(java)》第十五周学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 短暂的人生、脆弱的生命
- 下一篇: -HTML常用标签