// Simple Frame example which paints and closes!
// with simple button handler which opens a file dialog too!
// note: default layout for Frame is Border.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test extends Frame implements WindowListener, ActionListener {
Button b;
// constructor
public Test() {
setTitle( "Hi there");
setBounds( 100, 150, 300, 200); // position & size
addWindowListener( this);
b = new Button( "OK");
add( "South", b);
b.addActionListener( this);
setVisible(true);
}
// restores screen after window has been covered, minimised, etc.
public void paint (Graphics g) {
g.drawRect( 50, 50, 200, 100);
g.drawString( "Hello", 125, 100 );
}
// called when button pressed...
public void actionPerformed( ActionEvent e) {
getGraphics().drawString( "Yes...", 75, 75);
// Open modal file dialog...
// example shows how to distinguish events from diff. components
if ( e.getSource() == b) {
FileDialog d = new FileDialog(this, "Test", FileDialog.LOAD);
d.setVisible(true);
if ( d.getFile() == null)
System.out.println( "Cancel button pressed");
else
System.out.println( "Selected: " + d.getFile() );
}
}
// Listener class to shut application!
public void windowClosing( WindowEvent e) {
dispose();
System.exit(0);
}
// Listener interface classes which must be implemented,
// but which we don't need right now!
public void windowOpened( WindowEvent e) {}
public void windowClosed( WindowEvent e) {}
public void windowIconified( WindowEvent e) {}
public void windowDeiconified( WindowEvent e) {}
public void windowActivated( WindowEvent e) {}
public void windowDeactivated( WindowEvent e) {}
// creates instance of this class when run...
public static void main (String[] args) throws IOException {
new Test();
}
}
// with simple button handler which opens a file dialog too!
// note: default layout for Frame is Border.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test extends Frame implements WindowListener, ActionListener {
Button b;
// constructor
public Test() {
setTitle( "Hi there");
setBounds( 100, 150, 300, 200); // position & size
addWindowListener( this);
b = new Button( "OK");
add( "South", b);
b.addActionListener( this);
setVisible(true);
}
// restores screen after window has been covered, minimised, etc.
public void paint (Graphics g) {
g.drawRect( 50, 50, 200, 100);
g.drawString( "Hello", 125, 100 );
}
// called when button pressed...
public void actionPerformed( ActionEvent e) {
getGraphics().drawString( "Yes...", 75, 75);
// Open modal file dialog...
// example shows how to distinguish events from diff. components
if ( e.getSource() == b) {
FileDialog d = new FileDialog(this, "Test", FileDialog.LOAD);
d.setVisible(true);
if ( d.getFile() == null)
System.out.println( "Cancel button pressed");
else
System.out.println( "Selected: " + d.getFile() );
}
}
// Listener class to shut application!
public void windowClosing( WindowEvent e) {
dispose();
System.exit(0);
}
// Listener interface classes which must be implemented,
// but which we don't need right now!
public void windowOpened( WindowEvent e) {}
public void windowClosed( WindowEvent e) {}
public void windowIconified( WindowEvent e) {}
public void windowDeiconified( WindowEvent e) {}
public void windowActivated( WindowEvent e) {}
public void windowDeactivated( WindowEvent e) {}
// creates instance of this class when run...
public static void main (String[] args) throws IOException {
new Test();
}
}
设置setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
就可以了。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;public class FileChooserDemo extends JFrame {
static private final String newline = "\n"; public FileChooserDemo() {
super("FileChooserDemo"); //Create the log first, because the action listeners
//need to refer to it.
final JTextArea log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log); //Create a file chooser
final JFileChooser fc = new JFileChooser();
//Directories only
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //Create the open button
ImageIcon openIcon = new ImageIcon("images/open.gif");
JButton openButton = new JButton("Open a File...", openIcon);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
}
}); //Create the save button
ImageIcon saveIcon = new ImageIcon("images/save.gif");
JButton saveButton = new JButton("Save a File...", saveIcon);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
}
}); //For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton); //Explicitly set the focus sequence.
openButton.setNextFocusableComponent(saveButton);
saveButton.setNextFocusableComponent(openButton); //Add the buttons and the log to the frame
Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(logScrollPane, BorderLayout.CENTER);
} public static void main(String[] args) {
JFrame frame = new FileChooserDemo(); frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); frame.pack();
frame.setVisible(true);
}
}