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 閱讀(950) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 当涂县| 诸暨市| 辽中县| 桂东县| 雅安市| 连城县| 灵璧县| 兴隆县| 衡阳市| 富宁县| 贡觉县| 东港市| 来凤县| 新宾| 桃园县| 永和县| 乌恰县| 宁国市| 兰溪市| 瓦房店市| 乌兰察布市| 天柱县| 昌平区| 兴义市| 安义县| 芦山县| 张家界市| 贵定县| 渭南市| 景洪市| 平南县| 富蕴县| 文山县| 重庆市| 新河县| 黎川县| 青田县| 梧州市| 镇原县| 乌拉特后旗| 枣阳市|