2014-09-01 65 views
-1

试图写一个简单的程序,我想要一个按钮,当点击打开文件浏览器窗口,以便用户可以选择一个文件上传到程序...如何链接Jbutton和代码以打开文件浏览器来选择文件?

我目前有以下代码,打开一个浏览器窗口...

如何将此代码链接到我现有的按钮代码,以便单击按钮时文件浏览器打开?

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 

import javax.swing.JFileChooser; 
import javax.swing.JFrame; 

public class MainClass { 

    public static void main(String[] a) { 
    JFrame frame = new JFrame("JFileChooser Popup"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JFileChooser fileChooser = new JFileChooser("."); 
    fileChooser.setControlButtonsAreShown(false); 
    frame.add(fileChooser, BorderLayout.CENTER); 

    ActionListener actionListener = new ActionListener() { 
     public void actionPerformed(ActionEvent actionEvent) { 
     JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource(); 
     String command = actionEvent.getActionCommand(); 
     if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
      File selectedFile = theFileChooser.getSelectedFile(); 
      System.out.println(selectedFile.getParent()); 
      System.out.println(selectedFile.getName()); 
     } else if (command.equals(JFileChooser.CANCEL_SELECTION)) { 
      System.out.println(JFileChooser.CANCEL_SELECTION); 
     } 
     } 
    }; 
    fileChooser.addActionListener(actionListener); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
} 

感谢

有人可以把这个变成一个MVC我吗?我需要做一个考试,我不知道该怎么做...

import java.awt.BorderLayout;

公共类片延伸的JFrame {

private JPanel contentPane; 
private JTextField textField; 
private JTextField textField_1; 
private JTextField textField_2; 
private JTextField textField_3; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Tab frame = new Tab(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

{ 
    this.setTitle("Cipher Toolkit"); 
} 

// set the title 
/** 
* Create the frame. 
*/ 
public Tab() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(500, 400, 700, 600); 

    JMenuBar menuBar = new JMenuBar(); 
    setJMenuBar(menuBar); 

    JMenu mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    JMenuItem mntmPrint = new JMenuItem("Print"); 
    mnFile.add(mntmPrint); 

    JMenuItem mntmExit = new JMenuItem("Exit"); 
    mnFile.add(mntmExit); 

    JMenu mnAbout = new JMenu("About"); 
    menuBar.add(mnAbout); 

    JMenuItem mntmAboutTheAuthors = new JMenuItem("About the Authors"); 

    mnAbout.add(mntmAboutTheAuthors); 

    JMenuItem mntmProgramVersion = new JMenuItem("Program Version"); 
    mnAbout.add(mntmProgramVersion); 

    JMenu mnHelp = new JMenu("Help"); 
    menuBar.add(mnHelp); 

    JMenuItem mntmFaqs = new JMenuItem("FAQ's"); 
    mnHelp.add(mntmFaqs); 
    contentPane = new JPanel(); 
    contentPane.setBackground(Color.LIGHT_GRAY); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
    tabbedPane.setBounds(20, 10, 655, 520); 
    contentPane.add(tabbedPane); 

    JPanel panel = new JPanel(); 
    panel.setBackground(Color.LIGHT_GRAY); 
    tabbedPane.addTab("Ceasar Encrypt", null, panel, null); 
    panel.setBorder(new TitledBorder(new LineBorder(Color.blue, 2, true))); 

    JButton btnNewButton_1 = new JButton("LOAD INPUT FILE"); 
    btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 

    panel.add(btnNewButton_1); 
    btnNewButton_1.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_1.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_2 = new JButton("SELECT OUTPUT FILE"); 
    btnNewButton_2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 

    panel.add(btnNewButton_2); 
    btnNewButton_2.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_2.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton = new JButton("ENCRYPT!"); 
    panel.add(btnNewButton); 
    btnNewButton.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton.setFont(new Font("Arial", Font.BOLD, 40)); 

    JLabel lblSelectShiftAmount = new JLabel("SELECT SHIFT AMOUNT"); 
    panel.add(lblSelectShiftAmount); 

    textField = new JTextField(); 
    panel.add(textField); 
    textField.setColumns(10); 

    JPanel panel_1 = new JPanel(); 
    panel_1.setBackground(Color.LIGHT_GRAY); 
    tabbedPane.addTab("Ceasar Decrypt", null, panel_1, null); 
    panel_1.setBorder(new TitledBorder(new LineBorder(Color.green, 2, true))); 

    JButton btnNewButton_4 = new JButton("LOAD INPUT FILE"); 
    btnNewButton_4.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 

     } 
    }); 
    panel_1.add(btnNewButton_4); 
    btnNewButton_4.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_4.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_5 = new JButton("SELECT OUTPUT FILE"); 
    btnNewButton_5.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 

     } 
    }); 
    panel_1.add(btnNewButton_5); 
    btnNewButton_5.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_5.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_3 = new JButton("DECRYPT!"); 
    panel_1.add(btnNewButton_3); 
    btnNewButton_3.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_3.setFont(new Font("Arial", Font.BOLD, 40)); 

    JLabel lblSelectShiftAmount_1 = new JLabel("SELECT SHIFT AMOUNT"); 
    panel_1.add(lblSelectShiftAmount_1); 

    textField_1 = new JTextField(); 
    panel_1.add(textField_1); 
    textField_1.setColumns(10); 

    JPanel panel_2 = new JPanel(); 
    panel_2.setBackground(Color.LIGHT_GRAY); 
    tabbedPane.addTab("Substitution Encrypt", null, panel_2, null); 
    panel_2.setBorder(new TitledBorder(new LineBorder(Color.red, 2, true))); 

    JButton btnNewButton_6 = new JButton("LOAD INPUT FILE"); 
    btnNewButton_6.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 
    panel_2.add(btnNewButton_6); 
    btnNewButton_6.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_6.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_8 = new JButton("SELECT OUTPUT FILE"); 
    btnNewButton_8.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 

     } 
    }); 
    panel_2.add(btnNewButton_8); 
    btnNewButton_8.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_8.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_7 = new JButton("ENCRYPT!"); 
    panel_2.add(btnNewButton_7); 
    btnNewButton_7.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_7.setFont(new Font("Arial", Font.BOLD, 40)); 

    JLabel lblSelectShiftAmount_2 = new JLabel("SELECT SHIFT AMOUNT"); 
    panel_2.add(lblSelectShiftAmount_2); 

    textField_2 = new JTextField(); 
    panel_2.add(textField_2); 
    textField_2.setColumns(10); 

    JPanel panel_3 = new JPanel(); 
    panel_3.setBackground(Color.LIGHT_GRAY); 
    tabbedPane.addTab("Substitution Decrypt", null, panel_3, null); 
    panel_3.setBorder(new TitledBorder(
      new LineBorder(Color.yellow, 2, true))); 

    JButton btnNewButton_10 = new JButton("LOAD INPUT FILE"); 
    btnNewButton_10.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 
    panel_3.add(btnNewButton_10); 
    btnNewButton_10.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_10.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_11 = new JButton("SELECT OUTPUT FILE"); 
    btnNewButton_11.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFrame frame = new JFrame("File Browser"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JFileChooser fileChooser = new JFileChooser("."); 
      fileChooser.setControlButtonsAreShown(false); 
      frame.getContentPane().add(fileChooser, BorderLayout.CENTER); 

      ActionListener actionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent actionEvent) { 
        JFileChooser theFileChooser = (JFileChooser) actionEvent 
          .getSource(); 
        String command = actionEvent.getActionCommand(); 
        if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
         File selectedFile = theFileChooser 
           .getSelectedFile(); 
         System.out.println(selectedFile.getParent()); 
         System.out.println(selectedFile.getName()); 
        } else if (command 
          .equals(JFileChooser.CANCEL_SELECTION)) { 
         System.out.println(JFileChooser.CANCEL_SELECTION); 
        } 
       } 
      }; 
      fileChooser.addActionListener(actionListener); 
      frame.pack(); 
      frame.setVisible(true); 

     } 
    }); 
    panel_3.add(btnNewButton_11); 
    btnNewButton_11.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_11.setFont(new Font("Arial", Font.BOLD, 40)); 

    JButton btnNewButton_9 = new JButton("DECRYPT!"); 
    panel_3.add(btnNewButton_9); 
    btnNewButton_9.setPreferredSize(new Dimension(600, 100)); 
    btnNewButton_9.setFont(new Font("Arial", Font.BOLD, 40)); 

    JLabel lblSelectShiftAmount_3 = new JLabel("SELECT SHIFT AMOUNT"); 
    panel_3.add(lblSelectShiftAmount_3); 

    textField_3 = new JTextField(); 
    panel_3.add(textField_3); 
    textField_3.setColumns(10); 
} 

}

尼斯一个盖子

+0

您已经**选择了**文件。当你说“打开文件”时,你是什么意思? – 2014-09-01 13:13:39

回答

3

你的问题有些宽泛和模糊,因为你只显示代码借来的和不显示在那里你已经试图自己综合了一些东西,所以我只能发表一些建议内容的大纲。如果您需要更具体的帮助,请告诉我们您的代码,然后告诉我们您的代码可能会遇到什么问题。

  1. 创建的JButton
  2. 您的JButton
  3. 添加一个ActionListener在此的ActionListener的actionPerformed代码,打开你的JFileChooser。
  4. 如果用户按下JFileChooser上的“打开”按钮(通过检查从文件选择器的showOpenDialg(...)方法返回的int进行测试),从JFileChooser中获取选定的文件。
  5. 完成。

本教程将为您提供详细信息,然后您需要编写代码。

链接:


编辑
在给予一个JButton所需操作的关键是一个ActionListener添加到它。我经常用一个匿名的内部类来调用外部类的另一种方法。例如:

myButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     myButtonAction(); 
    } 
}); 

private void myButtonAction() { 
    // here put in code that you want to happen when the button is pressed 
} 

但同样,有您的阅读教程,并与您的代码实验,写很多很多的代码,看看是什么在起作用,什么不起作用,你喜欢什么不可替代的。

+0

这是我已经拥有的按钮的代码。文件浏览器的代码已经在网上找到。点击按钮时,我想打开文件浏览器。我只是不知道如何将文件浏览器代码链接到按钮。 JButton btnNewButton_1 = new JButton(“LOAD INPUT FILE”); \t \t panel.add(btnNewButton_1); \t \t btnNewButton_1.setPreferredSize(new Dimension(600,100)); \t \t btnNewButton_1.setFont(new Font(“Arial”,Font.BOLD,40)); – itchyhippo 2014-09-01 13:54:14

+0

@itchyppo:请注意,在注释中发布代码时会丢失格式,为此,大多数代码都应该发布在您的问题中。您可以编辑您的问题并将该代码放在那里。但更重要的是我的关键建议是,您可以查看教程链接,并且首先尝试综合自己的代码来解决此问题。如果这不起作用,那么请发布**这个代码**作为您的问题的编辑,让我们来处理它。学习编程意味着学习使用一般信息并将其应用于您的具体情况,因此请务必遵守。 – 2014-09-01 13:58:13

+0

@itchyppo:最重要的是,在您的注释中没有看到代码中的任何位置,您尚未尝试将ActionListener添加到按钮中。请先尝试一下。你认为自己没有能力做到这一点就会侮辱自己,而且我强烈地感到你确实有能力,所以请证明我是对的。 – 2014-09-01 13:59:54

相关问题