如何使用java中的按钮浏览文件并显示其属性

时间:2018-03-18 12:48:02

标签: java

我想在java中使用按钮选择文件并显示其属性。我已经创建了一个面板和一个按钮,但我不知道如何为按钮增加价值。我的问题是:我该怎么做?

  import javax.swing.*;
  import java.awt.*;
  import javax.swing.JFileChooser;
  import java.io.File;

   public class test{



      private JFrame f;
      private JPanel p;
      private JButton b1;
      private JLabel lab;


      public test(){     //constructor

           gui();
      }

          public void gui(){

            f = new JFrame("Assignment");   //creating a new frame
            f.setVisible(true);
            f.setSize(600,400);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            p=new JPanel();
             p.setBackground (Color.CYAN);
            b1=new JButton("Browse");
            lab =new JLabel("test");
            p.add(b1);    //adding button to panel
            p.add(lab);   //adding label to panel

            f.add(p,BorderLayout.CENTER);

          }



     public static void main(String[] args){

         new test();

}

   }

1 个答案:

答案 0 :(得分:0)

这应该做你想要的。在我看来你需要选择文件然后获取属性。您可能想要更改显示属性的内容或位置,但这是一个开始。

public class FileInfo extends JFrame {
    private static final long serialVersionUID = 1L;
    private static final String TITLE   = "FileInfo" ;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                FileInfo    app = new FileInfo() ;
                app.setVisible(true) ;
            }
        }) ;
    }

    private JTextField  nameField ;
    private JTextField  propertiesField ;

    private FileInfo() {
        super(TITLE) ;
        Container   contentPane = getContentPane() ;
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)) ;

        // Setup button
        contentPane.add(new JButton(new AbstractAction("Get file info") {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent event) { performAction() ; }
        })) ;

        // setup filename
        JPanel  panel   = new JPanel(new BorderLayout()) ;
        panel.add(new JLabel("Filename: "), BorderLayout.WEST) ;
        nameField   = new JTextField(15) ;
        nameField.setEditable(false) ;
        panel.add(nameField, BorderLayout.CENTER) ;
        contentPane.add(panel) ;

        // setup properties
        panel   = new JPanel(new BorderLayout()) ;
        panel.add(new JLabel("Properties: "), BorderLayout.WEST) ;
        propertiesField = new JTextField(35) ;
        propertiesField.setEditable(false) ;
        panel.add(propertiesField, BorderLayout.CENTER) ;
        contentPane.add(panel) ;

        // Position and resize main window
        pack() ;
        Dimension   size    = getSize() ;
        Toolkit toolkit = Toolkit.getDefaultToolkit() ;
        Dimension   screen  = toolkit.getScreenSize() ;
        int x   = (screen.width / 2) - (size.width / 2) ;
        int y   = (screen.height / 2) - (size.height / 2) ;
        setLocation(x, y) ;
    }

    private void performAction() {
        JFileChooser    chooser = new JFileChooser() ;
        chooser.setVisible(true) ;
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File    file    = chooser.getSelectedFile() ;
            nameField.setText(file.getName()) ;
            StringBuffer    buffer  = new StringBuffer(128) ;
            if (file.canExecute())  buffer.append("[Execute]") ;
            if (file.canRead())     buffer.append("[Read]") ;
            if (file.canWrite())    buffer.append("[Write]") ;
            buffer.append("[Size:").append(Long.toString(file.length())).append("]") ;
            propertiesField.setText(buffer.toString()) ;
        }
    }
}