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

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

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

    paulwong

    JFrame+Spring學(xué)習(xí)

    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();
            // 設(shè)置「檔案」選單
            JMenu fileMenu = new JMenu("檔案");
            menuOpen = new JMenuItem("開啟舊檔");
            // 快速鍵設(shè)置
            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("關(guān)閉");
            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);
            // 設(shè)置「編輯」選單
            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);
            // 設(shè)置「關(guān)於」選單
            JMenu aboutMenu = new JMenu("關(guān)於");
            menuAbout = new JMenuItem("關(guān)於 JNotePad");
            aboutMenu.add(menuAbout);
            menuBar.add(fileMenu);
            menuBar.add(editMenu);
            menuBar.add(aboutMenu);
            // 設(shè)置選單列
            setJMenuBar(menuBar);

            // 文字編輯區(qū)域
            textArea = new JTextArea();
            textArea.setFont(new Font("細(xì)明體", 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);
            // 狀態(tài)列
            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);
            // 按下視窗關(guān)閉鈕事件處理
            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();
                }
            });

            // 選單 - 關(guān)閉檔案
            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();
                }
            });

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

            // 編輯區(qū)滑鼠事件
            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()) { // 文件是否為儲存狀態(tài)
                open(); // 開啟舊檔
            } else {
                // 顯示對話方塊
                int option = JOptionPane.showConfirmDialog(null, "檔案已修改,是否儲存?",
                        "儲存檔案?", JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE, null);
                switch (option) {
                // 確認(rèn)檔案儲存
                case JOptionPane.YES_OPTION:
                    saveFile(); // 儲存檔案
                    break;
                // 放棄檔案儲存
                case JOptionPane.NO_OPTION:
                    open();
                    break;
                }
            }
        }

        private void open() {
            // fileChooser 是 JFileChooser 的實(shí)例
            
    // 顯示檔案選取的對話方塊
            int option = fileChooser.showDialog(nullnull);
            // 使用者按下確認(rèn)鍵
            if (option == JFileChooser.APPROVE_OPTION) {
                try {
                    // 開啟選取的檔案
                    BufferedReader buf = new BufferedReader(new FileReader(
                            fileChooser.getSelectedFile()));
                    // 設(shè)定文件標(biāo)題
                    setTitle(fileChooser.getSelectedFile().toString());
                    // 清除前一次文件
                    textArea.setText("");
                    // 設(shè)定狀態(tài)列
                    stateBar.setText("未修改");
                    // 取得系統(tǒng)相依的換行字元
                    String lineSeparator = System.getProperty("line.separator");
                    // 讀取檔案並附加至文字編輯區(qū)
                    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() {
            // 從標(biāo)題列取得檔案名稱
            File file = new File(getTitle());
            // 若指定的檔案不存在
            if (!file.exists()) {
                // 執(zhí)行另存新檔
                saveFileAs();
            } else {
                try {
                    // 開啟指定的檔案
                    BufferedWriter buf = new BufferedWriter(new FileWriter(file));
                    // 將文字編輯區(qū)的文字寫入檔案
                    buf.write(textArea.getText());
                    buf.close();
                    // 設(shè)定狀態(tài)列為未修改
                    stateBar.setText("未修改");
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e.toString(), "寫入檔案失敗",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }

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

        private void closeFile() {
            // 是否已儲存文件19-36
            if (isCurrentFileSaved()) {
                // 釋放視窗資源,而後關(guān)閉程式
                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結(jié)合:
    http://stackoverflow.com/questions/15090453/converting-code-to-spring-annotations

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

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

    主站蜘蛛池模板: 亚洲欧美第一成人网站7777| 国产精品亚洲lv粉色| 亚洲高清视频免费| 亚洲毛片在线观看| 日本卡1卡2卡三卡免费| 国产∨亚洲V天堂无码久久久| 99久久99这里只有免费的精品| 亚洲中久无码永久在线观看同| 国产免费A∨在线播放| 亚洲日韩小电影在线观看| 毛片基地看看成人免费| 亚洲精品自产拍在线观看| 三级黄色片免费看| 亚洲成A人片在线观看无码不卡| 久操视频在线免费观看| 18亚洲男同志videos网站| 中文字幕免费视频一| 亚洲综合无码一区二区三区| 免费一级做a爰片久久毛片潮| 亚洲第一成人影院| 国产高清视频免费在线观看| 亚洲三区在线观看无套内射| 久久成人免费电影| 亚洲成年人电影网站| 毛片免费观看的视频在线| 亚洲一本到无码av中文字幕| 国产嫩草影院精品免费网址| 四虎永久在线精品免费一区二区| 在线亚洲97se亚洲综合在线| 你懂的免费在线观看网站| 亚洲欧洲日本精品| 成人毛片18女人毛片免费视频未| 日韩成人精品日本亚洲| 久久久久一级精品亚洲国产成人综合AV区 | 亚洲天堂2016| 日本一道在线日本一道高清不卡免费| 美女免费视频一区二区三区| 在线播放亚洲第一字幕| 99久久国产免费中文无字幕| 在线观看亚洲AV日韩A∨| 又粗又大又长又爽免费视频|