<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    JFrame+Spring學習

    package com.paul.ui;

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    import javax.swing.BorderFactory;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.SwingConstants;

    @Named
    public class JNotePadUI extends JFrame {

        private static final long serialVersionUID = -7584732501149961175L;
        private JMenuItem menuOpen;
        private JMenuItem menuSave;
        private JMenuItem menuSaveAs;
        private JMenuItem menuClose;
        private JMenuItem menuCut;
        private JMenuItem menuCopy;
        private JMenuItem menuPaste;
        private JMenuItem menuAbout;
        private JTextArea textArea;
        private JMenu editMenu;
        private JPopupMenu popUpMenu;
        private JLabel stateBar;
        private JFileChooser fileChooser;

        public JNotePadUI() {
            super("新增文字檔案");
            setUpUIComponent();
            setUpEventListener();
            setVisible(true);
        }

        private void setUpUIComponent() {
            setSize(640, 480);
            // 選單列
            JMenuBar menuBar = new JMenuBar();
            // 設置「檔案」選單
            JMenu fileMenu = new JMenu("檔案");
            menuOpen = new JMenuItem("開啟舊檔");
            // 快速鍵設置
            menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
                    InputEvent.CTRL_MASK));
            menuSave = new JMenuItem("儲存檔案");
            menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                    InputEvent.CTRL_MASK));
            menuSaveAs = new JMenuItem("另存新檔");
            menuClose = new JMenuItem("關閉");
            menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
                    InputEvent.CTRL_MASK));
            fileMenu.add(menuOpen);
            fileMenu.addSeparator(); // 分隔線
            fileMenu.add(menuSave);
            fileMenu.add(menuSaveAs);
            fileMenu.addSeparator(); // 分隔線
            fileMenu.add(menuClose);
            // 設置「編輯」選單
            editMenu = new JMenu("編輯");
            menuCut = new JMenuItem("剪下");
            menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                    InputEvent.CTRL_MASK));
            menuCopy = new JMenuItem("複製");
            menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
                    InputEvent.CTRL_MASK));
            menuPaste = new JMenuItem("貼上");
            menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
                    InputEvent.CTRL_MASK));
            editMenu.add(menuCut);
            editMenu.add(menuCopy);
            editMenu.add(menuPaste);
            // 設置「關於」選單
            JMenu aboutMenu = new JMenu("關於");
            menuAbout = new JMenuItem("關於 JNotePad");
            aboutMenu.add(menuAbout);
            menuBar.add(fileMenu);
            menuBar.add(editMenu);
            menuBar.add(aboutMenu);
            // 設置選單列
            setJMenuBar(menuBar);

            // 文字編輯區域
            textArea = new JTextArea();
            textArea.setFont(new Font("細明體", Font.PLAIN, 16));
            textArea.setLineWrap(true);
            JScrollPane panel = new JScrollPane(textArea,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            Container contentPane = getContentPane();
            contentPane.add(panel, BorderLayout.CENTER);
            // 狀態列
            stateBar = new JLabel("未修改");
            stateBar.setHorizontalAlignment(SwingConstants.LEFT);
            stateBar.setBorder(BorderFactory.createEtchedBorder());
            contentPane.add(stateBar, BorderLayout.SOUTH);

            popUpMenu = editMenu.getPopupMenu();
            
            fileChooser = new JFileChooser();
        }

        private void setUpEventListener() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // 按下視窗關閉鈕事件處理
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    closeFile();
                }
            });
            // 選單 - 開啟舊檔
            menuOpen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    openFile();
                }
            });
            // 選單 - 儲存檔案
            menuSave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    saveFile();
                }
            });
            // 選單 - 另存新檔
            menuSaveAs.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    saveFileAs();
                }
            });

            // 選單 - 關閉檔案
            menuClose.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    closeFile();
                }
            });
            // 選單 - 剪下
            menuCut.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cut();
                }
            });
            // 選單 - 複製
            menuCopy.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    copy();
                }
            });
            // 選單 - 貼上
            menuPaste.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    paste();
                }
            });

            // 選單 - 關於
            menuAbout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // 顯示對話方塊
                    JOptionPane.showOptionDialog(null, "程式名稱:\n JNotePad \n"
                            + "程式設計:\n 良葛格\n" + "簡介:\n 一個簡單的文字編輯器\n"
                            + " 可作為驗收 Java的實作對象\n" + " 歡迎網友下載研究交流\n\n"
                            + "http://caterpillar.onlyfun.net/", "關於 JNotePad",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.INFORMATION_MESSAGE, nullnullnull);
                }
            });
            // 編輯區鍵盤事件
            textArea.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    processTextArea();
                }
            });

            // 編輯區滑鼠事件
            textArea.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON3)
                        popUpMenu.show(editMenu, e.getX(), e.getY());
                }

                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1)
                        popUpMenu.setVisible(false);
                }
            });

        }

        private void openFile() {
            if (isCurrentFileSaved()) { // 文件是否為儲存狀態
                open(); // 開啟舊檔
            } else {
                // 顯示對話方塊
                int option = JOptionPane.showConfirmDialog(null, "檔案已修改,是否儲存?",
                        "儲存檔案?", JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE, null);
                switch (option) {
                // 確認檔案儲存
                case JOptionPane.YES_OPTION:
                    saveFile(); // 儲存檔案
                    break;
                // 放棄檔案儲存
                case JOptionPane.NO_OPTION:
                    open();
                    break;
                }
            }
        }

        private void open() {
            // fileChooser 是 JFileChooser 的實例
            
    // 顯示檔案選取的對話方塊
            int option = fileChooser.showDialog(nullnull);
            // 使用者按下確認鍵
            if (option == JFileChooser.APPROVE_OPTION) {
                try {
                    // 開啟選取的檔案
                    BufferedReader buf = new BufferedReader(new FileReader(
                            fileChooser.getSelectedFile()));
                    // 設定文件標題
                    setTitle(fileChooser.getSelectedFile().toString());
                    // 清除前一次文件
                    textArea.setText("");
                    // 設定狀態列
                    stateBar.setText("未修改");
                    // 取得系統相依的換行字元
                    String lineSeparator = System.getProperty("line.separator");
                    // 讀取檔案並附加至文字編輯區
                    String text;
                    while ((text = buf.readLine()) != null) {
                        textArea.append(text);
                        textArea.append(lineSeparator);
                    }
                    buf.close();
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e.toString(), "開啟檔案失敗",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }

        private boolean isCurrentFileSaved() {
            if (stateBar.getText().equals("未修改")) {
                return false;
            } else {
                return true;
            }
        }

        private void saveFile() {
            // 從標題列取得檔案名稱
            File file = new File(getTitle());
            // 若指定的檔案不存在
            if (!file.exists()) {
                // 執行另存新檔
                saveFileAs();
            } else {
                try {
                    // 開啟指定的檔案
                    BufferedWriter buf = new BufferedWriter(new FileWriter(file));
                    // 將文字編輯區的文字寫入檔案
                    buf.write(textArea.getText());
                    buf.close();
                    // 設定狀態列為未修改
                    stateBar.setText("未修改");
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e.toString(), "寫入檔案失敗",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }

        private void saveFileAs() {
            // 顯示檔案對話方塊
            int option = fileChooser.showDialog(nullnull);
            // 如果確認選取檔案
            if (option == JFileChooser.APPROVE_OPTION) {
                // 取得選擇的檔案
                File file = fileChooser.getSelectedFile();
                // 在標題列上設定檔案名稱
                setTitle(file.toString());
                try {
                    // 建立檔案
                    file.createNewFile();
                    // 進行檔案儲存
                    saveFile();
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e.toString(), "無法建立新檔",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }

        private void closeFile() {
            // 是否已儲存文件19-36
            if (isCurrentFileSaved()) {
                // 釋放視窗資源,而後關閉程式
                dispose();
            } else {
                int option = JOptionPane.showConfirmDialog(null, "檔案已修改,是否儲存?",
                        "儲存檔案?", JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE, null);
                switch (option) {
                case JOptionPane.YES_OPTION:
                    saveFile();
                    break;
                case JOptionPane.NO_OPTION:
                    dispose();
                }
            }
        }

        private void cut() {
            textArea.cut();
            stateBar.setText("已修改");
            popUpMenu.setVisible(false);
        }

        private void copy() {
            textArea.copy();
            popUpMenu.setVisible(false);
        }

        private void paste() {
            textArea.paste();
            stateBar.setText("已修改");
            popUpMenu.setVisible(false);
        }

        private void processTextArea() {
            stateBar.setText("已修改");
        }

        public static void main(String[] args) {
            new JNotePadUI();
        }
    }


    Main.java
    package com.paul;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.paul.ui.JNotePadUI;

    public class Main {
        
        public static void main(String [] args)
        {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
            JNotePadUI jNotePadUI = (JNotePadUI) context.getBean("JNotePadUI");
            jNotePadUI.setVisible(true);
            
        }

    }


    applicationContext.xml
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context
    ="http://www.springframework.org/schema/context"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >
     
        <context:component-scan base-package="com.paul" />
        
        <context:load-time-weaver/>
        <context:spring-configured />
     
    </beans>


    pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.paul</groupId>
      <artifactId>test</artifactId>
      <version>1.0.0</version>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>${spring.version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>${spring.version}</version>
          </dependency>
          <dependency>
              <groupId>javax.inject</groupId>
              <artifactId>javax.inject</artifactId>
              <version>1</version>
          </dependency>
      </dependencies>
      <properties>
          <spring.version>3.0.5.RELEASE</spring.version>
      </properties>
    </project>


    Spring與JFrame結合:
    http://stackoverflow.com/questions/15090453/converting-code-to-spring-annotations

    點擊菜單時切換表單內容:
    http://stackoverflow.com/questions/5077321/how-could-i-make-the-jframe-content-change-to-corresponding-click

    posted on 2013-03-24 17:33 paulwong 閱讀(1040) 評論(0)  編輯  收藏 所屬分類: J2SE

    主站蜘蛛池模板: 日韩色日韩视频亚洲网站| 久久影视综合亚洲| 亚洲首页在线观看| 久久久久久噜噜精品免费直播| 国产成人免费ā片在线观看| 亚洲欧洲无码一区二区三区| 动漫黄网站免费永久在线观看 | 亚洲欧美第一成人网站7777| 亚洲av无码一区二区乱子伦as| 嫩草在线视频www免费看| 亚洲自偷自偷偷色无码中文| 久久99精品国产免费观看| 67pao强力打造67194在线午夜亚洲| 免费在线黄色网址| 99精品全国免费观看视频..| 久久精品国产亚洲AV电影| 黄页免费的网站勿入免费直接进入| 亚洲人成网站999久久久综合| 久久综合九九亚洲一区| 久久精品国产精品亚洲人人| 国产片免费在线观看| 成人性生交视频免费观看| 亚洲av片在线观看| 亚洲精品无码久久久影院相关影片| 久久午夜夜伦鲁鲁片免费无码影视| 亚洲色www永久网站| 三上悠亚亚洲一区高清| 国产免费131美女视频| 久久成人a毛片免费观看网站| 亚洲国产精品嫩草影院| 亚洲乱码中文字幕久久孕妇黑人| 免费国产污网站在线观看15| 亚洲欧美日韩中文高清www777| 亚洲成人福利网站| 在线观看亚洲精品国产| 久久夜色精品国产亚洲av| 中文字幕专区在线亚洲| 中文字幕精品亚洲无线码二区| 亚洲综合无码精品一区二区三区 | 亚洲AV日韩综合一区尤物| 国产精品亚洲不卡一区二区三区 |