📄 choosers.java
字号:
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/** An example class used to demonstrate the
* use of the color and file choosers
*/
public class Choosers extends JFrame {
private JTextArea text = new JTextArea();
private JFileChooser fileChoose
= new JFileChooser();
private JDialog colorDlg;
private JColorChooser colorChoose
= new JColorChooser();
/** Class constructor
* @param titleText Title bar text
*/
public Choosers( String titleText ) {
super( titleText );
addWindowListener( new WindowCloser() );
setJMenuBar( buildMenuBar() );
text.setEditable( false );
Container cp = getContentPane();
cp.add( new JScrollPane( text ),
BorderLayout.CENTER );
setSize( 500, 400 );
setVisible( true );
}
/** Present a dialog box to have the user select
* the file for browsing */
public void loadFile() {
int result = fileChoose.showOpenDialog(
Choosers.this );
File file = fileChoose.getSelectedFile();
if ( file != null
&& result == JFileChooser.APPROVE_OPTION ) try {
FileReader fr = new FileReader( file );
text.setText( "" );
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 );
}
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
/** Build the menu bar, menus, and menu items for
* the file browser */
public JMenuBar buildMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu( "File" );
JMenu editMenu = new JMenu( "Edit" );
JMenuItem exitItem = new JMenuItem( "Exit" );
JMenuItem fileOpenItem
= new JMenuItem( "File open..." );
JMenuItem colorsItem
= new JMenuItem( "Change Color..." );
fileMenu.setMnemonic( KeyEvent.VK_F );
editMenu.setMnemonic( KeyEvent.VK_E );
fileOpenItem.setMnemonic( KeyEvent.VK_O );
exitItem.setMnemonic( KeyEvent.VK_X );
colorsItem.setMnemonic( KeyEvent.VK_C );
fileOpenItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
loadFile();
}
}
);
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
dispose();
System.exit( 0 );
}
}
);
colorsItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
if ( colorDlg == null ) {
colorDlg = JColorChooser.createDialog(
Choosers.this,
"Select Text Color",
true,
colorChoose,
new ColorOKListener(),
null
);
}
colorChoose.setColor(
text.getForeground() );
colorDlg.setVisible( true );
}
}
);
menuBar.add( fileMenu );
menuBar.add( editMenu );
fileMenu.add( fileOpenItem );
fileMenu.add( exitItem );
editMenu.add( colorsItem );
return menuBar;
}
class ColorOKListener implements ActionListener {
public void actionPerformed( ActionEvent e ) {
Color c = colorChoose.getColor();
text.setForeground( c );
text.repaint();
}
}
/** The test method for the class
* @param args not used
*/
public static void main( String[] args ) {
new Choosers( "File and Color Choosers" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -