Java 階段學習總結
阿新 • • 發佈:2019-02-20
之前倒是沒有接觸過Java。現在來學Java會被一部分人嗤之以鼻。就對現在而言,語言中種類很多,技術層出不窮,各種框架。尤其前端的應用框架越來越多。哪怕我使用jQuery也會被所謂的前端大牛鄙視一把。.
為什麼要學Java。語言這玩意的爭論沒有意義。匠人會把注意力放在工具上麼?會也只有不到5%的注意力。
用 Java 寫了記事本以及坦克遊戲。
程式碼(記事本):
package javatest; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.UIManager; public class JavaTest extends JFrame implements ActionListener { JTextArea jta = null; JScrollPane scroll = null; JMenuBar jmb = null; JMenu jm1 = null; JMenuItem jmi1 = null; JMenuItem jmi2 = null; JMenuItem jmi3 = null; JMenuItem jmi4 = null; JMenu jm2 = null; public JavaTest() { jta = new JTextArea(); // 滾動條 自動出現 scroll = new JScrollPane(jta); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); jmb = new JMenuBar(); jm1 = new JMenu("檔案"); jm2 = new JMenu("幫助"); jmi1 = new JMenuItem("開啟"); jmi2 = new JMenuItem("儲存"); jmi3 = new JMenuItem("退出"); jmi4 = new JMenuItem("關於 (v 1.0)"); jmi1.addActionListener(this); jmi1.setActionCommand("open"); jmi2.addActionListener(this); jmi2.setActionCommand("save"); jmi3.addActionListener(this); jmi3.setActionCommand("exit"); jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm2.add(jmi4); jmb.add(jm1); jmb.add(jm2); this.setJMenuBar(jmb); this.add(scroll); } public static void main(String[] args) { // 設定字型 Font font = new Font("宋體", Font.PLAIN, 12); String names[] = {"Label", "CheckBox", "PopupMenu", "MenuItem", "CheckBoxMenuItem", "JRadioButtonMenuItem", "ComboBox", "Button", "Tree", "ScrollPane", "TabbedPane", "EditorPane", "TitledBorder", "Menu", "TextArea", "OptionPane", "MenuBar", "ToolBar", "ToggleButton", "ToolTip", "ProgressBar", "TableHeader", "Panel", "List", "ColorChooser", "PasswordField", "TextField", "Table", "Label", "Viewport", "RadioButtonMenuItem", "RadioButton", "DesktopPane", "InternalFrame" }; for (String item : names) { UIManager.put(item + ".font", font); } JavaTest text = new JavaTest(); text.jshow(); } @Override public void actionPerformed(ActionEvent e) { String filepath; switch (e.getActionCommand()) { case "exit": System.out.println("退出"); System.exit(0); case "open": System.out.println("開啟檔案"); JFileChooser jfco = new JFileChooser(); jfco.setDialogTitle("開啟..."); jfco.showOpenDialog(null); if (jfco.getSelectedFile() == null) { return; } filepath = jfco.getSelectedFile().getAbsoluteFile().getPath(); System.out.println(filepath); FileReader fr = null; BufferedReader br = null; String fileCon = ""; { try { fr = new FileReader(filepath); br = new BufferedReader(fr); String tmp; while ((tmp = br.readLine()) != null) { fileCon += tmp + "\r\n"; } } catch (FileNotFoundException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } finally { try { br.close(); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } try { fr.close(); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } } } jta.setText(fileCon); System.out.println(filepath); break; case "save": System.out.println("另存為"); JFileChooser jfcs = new JFileChooser(); jfcs.setDialogTitle("另存為..."); jfcs.showOpenDialog(null); if (jfcs.getSelectedFile() == null) { return; } filepath = jfcs.getSelectedFile().getAbsoluteFile().getPath(); FileWriter fw = null; BufferedWriter bw = null; { try { fw = new FileWriter(filepath); bw = new BufferedWriter(fw); bw.write(jta.getText()); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } finally { try { bw.close(); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } try { fw.close(); } catch (IOException ex) { Logger.getLogger(JavaTest.class.getName()).log(Level.SEVERE, null, ex); } } } break; default: break; } } public void jshow() { this.setTitle("記事本"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
坦克遊戲的git地址:https://github.com/uduemc/TankGame
執行如下圖: