這次再把一個 class 又拆成三個 class,這樣應該算是重構吧,只是還沒進階到 unit test 的階段,還要加油加油。

這次主要是在加入 WindowListener 時,發現在 addWindowListener(new DualBoot()) 會造成 JApplet 產生而浪費很多資源,如果把 WindowListener 獨立成一個 class 又顯得太多餘,我又不喜歡搞一堆 inner class。最後幹脆分成三個 class,這樣也比較符合單一職責原則(Single Responsibility Principle),並且在 applet 及 frame 內加入變體的 Singleton Pattern。為什麼叫做變體呢?因為理論上 applet 及 frame 只有一個單獨存在的個體,而且 applet 的建構函數是用 browser 調用,我也沒法子阻止。所以只是單純建立 getInstance 函數而已,並沒有隱藏建構函數。另外把 applet 的建構函數拿掉,之前都忘了呼叫 super(),雖然都沒事發生,但是為了確保一些問題,還是採用標準 applet 步驟,把所有建立元件動作放到 init()。

透過 jnlp 執行範例程式

AppletLoader.class

import javax.swing.JApplet; @SuppressWarnings("serial") public class AppletLoader extends JApplet { private static AppletLoader instance=null; public static AppletLoader getInstance() { return instance; } /** * applet event */ public void init() { System.out.println("applet init"); instance = this; Main main = new Main(); setJMenuBar(main.getJMenuBar()); setContentPane(main); } public void start() { System.out.println("applet start"); } public void stop() { System.out.println("applet stop"); } public void destroy() { System.out.println("applet destroy"); } }

FrameLoader.class

import java.awt.EventQueue; @SuppressWarnings("serial") public class FrameLoader extends JFrame implements WindowListener { private static FrameLoader instance=null; public static FrameLoader getInstance() { return instance; } /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { FrameLoader frame = new FrameLoader(); JOptionPane.setRootFrame(frame); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public FrameLoader() { instance = this; setTitle("Application Loader Demo"); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setBounds(100, 100, 450, 300); Main contentPane = new Main(); setContentPane(contentPane); setJMenuBar(contentPane.getJMenuBar()); addWindowListener(this); } /** * window event handler */ public void windowOpened(WindowEvent arg0) { System.out.println("window opened"); } public void windowClosed(WindowEvent arg0) { System.out.println("window closed"); System.exit(0); } public void windowClosing(WindowEvent arg0) { System.out.println("window closing"); int close = JOptionPane.showConfirmDialog( null, "確定要結束程式嗎?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (close == JOptionPane.YES_OPTION) { Frame.getFrames()[0].dispose(); } } public void windowActivated(WindowEvent arg0) { System.out.println("window activated"); } public void windowDeactivated(WindowEvent arg0) { System.out.println("window deactivated"); } public void windowDeiconified(WindowEvent arg0) { System.out.println("window deiconified"); } public void windowIconified(WindowEvent arg0) { System.out.println("window iconified"); } }

Main.class

import javax.swing.ButtonGroup; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JRadioButtonMenuItem; import javax.swing.JSeparator; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.border.EmptyBorder; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; @SuppressWarnings("serial") public class Main extends JPanel { JMenuBar menuBar; /** * get the JmenuBar. */ public JMenuBar getJMenuBar() { return menuBar; } /** * Create the JmenuBar. */ private void createJMenuBar() { if (menuBar != null) { System.out.println("menubar is created!!"); return; } menuBar = new JMenuBar(); JMenu mnFile = new JMenu("File"); //$NON-NLS-1$ menuBar.add(mnFile); // exit JSeparator separator = new JSeparator(); mnFile.add(separator); JMenuItem menuItem_2 = new JMenuItem("Exit"); //$NON-NLS-1$ menuItem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Frame.getFrames()[0].dispatchEvent(new WindowEvent(Frame.getFrames()[0], WindowEvent.WINDOW_CLOSING)); } }); mnFile.add(menuItem_2); JMenu mnView = new JMenu("View"); //$NON-NLS-1$ menuBar.add(mnView); // ButtonGroup lookAndFeelGroup = new ButtonGroup(); LookAndFeelInfo lookAndFeelInfo[] = UIManager.getInstalledLookAndFeels(); for(int i=0; i<lookAndFeelInfo.length; i++) { final String lookAndFeel = lookAndFeelInfo[i].getClassName(); JRadioButtonMenuItem rbmiLookAndFeel = new JRadioButtonMenuItem(lookAndFeelInfo[i].getName()); rbmiLookAndFeel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { UIManager.setLookAndFeel(lookAndFeel); SwingUtilities.updateComponentTreeUI(Frame.getFrames()[0]); } catch (Exception e) { System.out.println("lookandfeel set failed"); } } }); if (lookAndFeelInfo[i].getName() == UIManager.getLookAndFeel().getName()) rbmiLookAndFeel.setSelected(true); mnView.add(rbmiLookAndFeel); lookAndFeelGroup.add(rbmiLookAndFeel); } JSeparator separator_1 = new JSeparator(); mnView.add(separator_1); } /** * Create the panel. */ public Main() { createJMenuBar(); setLayout(new BorderLayout(0, 0)); setPreferredSize(new Dimension(400, 300)); JButton button = new JButton("New button"); add(button, BorderLayout.CENTER); JToolBar toolBar = new JToolBar(); toolBar.setRollover(true); toolBar.setFloatable(false); toolBar.setBorder(new EmptyBorder(2, 4, 2, 4)); add(toolBar, BorderLayout.NORTH); JButton button_1 = new JButton("New button"); toolBar.add(button_1); JButton button_2 = new JButton("New button"); toolBar.add(button_2); } }

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()