march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          寫了一個FrameWork類實現了菜單欄,并且為菜單欄里的Item添加事件監聽,實現了選擇文件的功能。
          import java.awt.FlowLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.io.File;

          import javax.swing.JFileChooser;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JMenu;
          import javax.swing.JMenuBar;
          import javax.swing.JMenuItem;


          public class FrameWork extends JFrame {
              
              private static final int Width = 1000;
              private static final int Height = 600;
              private static JFrame frame = null;
              private static FlowLayout flowLayout = null;
              
              public FrameWork() {
                  frame = new JFrame("Java菜單欄");
                  flowLayout = new FlowLayout(FlowLayout.CENTER);
                  flowLayout.setHgap(20);
                  flowLayout.setVgap(30);
                  frame.setLayout(flowLayout);
                  
                  JMenuBar menuBar = new JMenuBar();
                  frame.setJMenuBar(menuBar);
                  
                  JMenu fileMenu = new JMenu("文件");
                  JMenu openMenu = new JMenu("打開");
                  JMenuItem openItem = new JMenuItem("文件");
                  openMenu.add(openItem);
                  openItem.addActionListener(new MyAction());
                  fileMenu.add(openMenu);
                  menuBar.add(fileMenu);
                  
                  
                  
                  frame.setVisible(true);
                  frame.setSize(Width, Height);
                  frame.setLocation(100, 100);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              }
              
              private class MyAction implements ActionListener {
                  public void actionPerformed(ActionEvent evt) {
                      Object s = evt.getSource();
                      JFileChooser jfc=new JFileChooser();
                      jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                      jfc.showDialog(new JLabel(), "選擇");
                      File file=jfc.getSelectedFile();
                      if(file.isDirectory()){
                          System.out.println("文件夾:"+file.getAbsolutePath());
                      }else if(file.isFile()){
                          System.out.println("文件:"+file.getAbsolutePath());
                      }
                      System.out.println(jfc.getSelectedFile().getName());
                  }
              }
              
              public static void main(String[] args) {
                  new FrameWork();
              }
          }
          posted on 2015-03-18 12:51 marchalex 閱讀(947) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 广水市| 休宁县| 姜堰市| 保亭| 大兴区| 洪湖市| 元氏县| 海晏县| 泾阳县| 南溪县| 利津县| 文安县| 阳朔县| 夹江县| 黑山县| 革吉县| 东海县| 宁南县| 新巴尔虎左旗| 柳江县| 佛学| 金乡县| 方山县| 岱山县| 湘阴县| 泽普县| 铁力市| 获嘉县| 宜宾市| 阿拉尔市| 博野县| 铁岭县| 西充县| 百色市| 乌恰县| 沙坪坝区| 福海县| 安龙县| 荣成市| 奉化市| 南投县|