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小程序
          主站蜘蛛池模板: 肥城市| 收藏| 天台县| 柳林县| 海兴县| 缙云县| 昌黎县| 象山县| 万荣县| 白沙| 阜康市| 齐齐哈尔市| 博湖县| 时尚| 建瓯市| 苏州市| 刚察县| 海盐县| 城步| 太康县| 安达市| 曲靖市| 辽阳市| 渝中区| 秀山| 浙江省| 新乡县| 景泰县| 新巴尔虎左旗| 碌曲县| 崇明县| 鲁山县| 云龙县| 堆龙德庆县| 房山区| 元朗区| 贵南县| 天门市| 九台市| 木兰县| 疏勒县|