📄 filebrowser.java
字号:
package examples.windows;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/** An example class used to demonstrate the
* use of menus, menu items, dialog boxes,
* etc. using AWT components
*/
public class FileBrowser extends Frame {
private FontDialog fontDialog;
private TextField statusLine;
private TextArea text;
/** Class constructor
* @param titleText Title bar text
*/
public FileBrowser( String titleText ) {
super( titleText );
addWindowListener( new WindowCloser() );
setLayout( new BorderLayout() );
setMenuBar( buildMenuBar() );
text = new TextArea();
text.setEditable( false );
text.setBackground( Color.white );
add( text, BorderLayout.CENTER );
statusLine = new TextField();
add( statusLine, BorderLayout.SOUTH );
setSize( 500, 400 );
setVisible( true );
}
/** Present a dialog box to have the user select
* the file for browsing */
public void loadFile() {
FileDialog fileDialog
= new FileDialog( this, "File Open",
FileDialog.LOAD );
fileDialog.setVisible( true );
if ( fileDialog.getFile() != null ) try {
FileReader fr = new FileReader(
fileDialog.getDirectory() +
fileDialog.getFile() );
text.setText( "" );
text.setVisible( false );
char[] charBuffer = new char[4096];
int charsRead = fr.read( charBuffer, 0,
charBuffer.length );
while ( charsRead != -1 ) {
text.append( new String( charBuffer, 0,
charsRead ) );
charsRead = fr.read( charBuffer, 0,
charBuffer.length );
}
text.setVisible( true );
} catch( IOException ioe ) {
statusLine.setText( ioe.toString() );
}
fileDialog.dispose();
}
/** Build the menu bar, menus, and menu items for
* the file browser */
public MenuBar buildMenuBar() {
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu( "File" );
Menu editMenu = new Menu( "Edit" );
MenuItem exitItem = new MenuItem( "Exit" );
MenuItem fileOpenItem
= new MenuItem( "File open..." );
MenuItem fontsItem
= new MenuItem( "Fonts..." );
fileOpenItem.setShortcut(
new MenuShortcut( KeyEvent.VK_O ) );
fontsItem.setShortcut(
new MenuShortcut( KeyEvent.VK_F ) );
fileOpenItem.addActionListener(
new ActionListener() {
public void
actionPerformed( ActionEvent event ) {
loadFile();
}
}
);
exitItem.addActionListener(
new ActionListener() {
public void
actionPerformed( ActionEvent event ) {
dispose();
System.exit( 0 );
}
}
);
fontsItem.addActionListener(
new ActionListener() {
public void
actionPerformed( ActionEvent event ) {
updateFont();
}
}
);
menuBar.add( fileMenu );
menuBar.add( editMenu );
fileMenu.add( fileOpenItem );
fileMenu.add( exitItem );
editMenu.add( fontsItem );
return menuBar;
}
/** Allow font selection and update */
public void updateFont() {
if ( fontDialog == null ) {
fontDialog
= new FontDialog( this, text.getFont() );
} else {
fontDialog.setFont( text.getFont() );
fontDialog.setVisible( true );
}
Font f = text.getFont();
String chosenFont = fontDialog.getFontName();
if ( chosenFont != null ) {
text.setFont( new Font( chosenFont,
f.getStyle(),
f.getSize() ) );
}
}
/** The test method for the class
* @param args not used
*/
public static void main( String[] args ) {
new FileBrowser( "File Browser Sample" );
}
}
class FontDialog extends Dialog {
private List fontChoices;
private String fontName;
private GraphicsEnvironment ge;
private String[] availableFonts;
private static final int VISIBLE_CHOICES = 12;
/** Class constructor
* @param parent The dialog box's parent window
* (the main browser window)
* @param currentFont Font being used in
* the main browser window
*/
public FontDialog( Frame parent, Font initialFont ){
super( parent, "Fonts", true );
addWindowListener( new WindowAdapter() {
/* Disappear when the window is closed */
public void
windowClosing( WindowEvent e ) {
e.getWindow().setVisible( false );
}
}
);
setLayout( new BorderLayout() );
final Button okay = new Button( "Apply" );
okay.setEnabled( false );
okay.addActionListener(
new ActionListener() {
public void
actionPerformed( ActionEvent event ) {
fontName
= fontChoices.getSelectedItem();
setVisible( false );
}
}
);
final Button cancel = new Button( "Cancel" );
cancel.addActionListener(
new ActionListener() {
public void
actionPerformed( ActionEvent event ) {
fontName = null;
dispose();
}
}
);
if ( ge == null ) {
ge = ge.getLocalGraphicsEnvironment();
}
if ( availableFonts == null ) {
availableFonts
= ge.getAvailableFontFamilyNames();
}
fontChoices = new List( VISIBLE_CHOICES, false );
for ( int i=0; i<availableFonts.length; i++ ) {
fontChoices.add( availableFonts[i] );
if ( availableFonts[i].equals(
initialFont.getName() ) ) {
fontChoices.select( i );
}
}
fontChoices.addItemListener(
new ItemListener() {
public void itemStateChanged(
ItemEvent event ) {
if ( event.getStateChange() ==
ItemEvent.SELECTED ) {
fontName =
fontChoices.getSelectedItem();
okay.setEnabled( true );
}
}
}
);
add( new Label( "Font name choices:" ),
BorderLayout.NORTH );
add( fontChoices, BorderLayout.CENTER );
Panel buttonPanel = new Panel();
buttonPanel.add( okay );
buttonPanel.add( cancel );
add( buttonPanel, BorderLayout.SOUTH );
Insets i = getParent().getInsets();
setLocation( i.left, i.top );
pack();
setVisible( true );
}
/** Retrieve the font name selected by the user
* @return The name of the font selected
*/
public String getFontName() {
return fontName;
}
/** Set the initial font name selected by the user
* @param initialFont the name of the
* initial font selected
*/
public void setFontName( Font initialFont ) {
for ( int i=0; i<availableFonts.length; i++ ) {
if ( availableFonts[i].equals(
initialFont.getName() ) ) {
fontChoices.select( i );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -