📄 typingapplication.java
字号:
// Tutorial 22: TypingApplication.java
// Application enables users to practice typing
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
public class TypingApplication extends JFrame
{
// JMenuBar for display and format options
private JMenuBar typingJMenuBar;
// JMenu to show display options clear, invert colors and color
private JMenu displayJMenu;
// JMenuItems to clear the JTextArea and choose color
private JMenuItem clearJMenuItem;
private JMenuItem colorJMenuItem;
// JMenu to display format options style and size
private JMenu formatJMenu;
// JMenu and array of JCheckBoxMenuItems to display style options
private JMenu styleJMenu;
private JCheckBoxMenuItem styleMenuItems[];
// JMenu, array of JRadioButtonMenuItems and ButtonGroup to
// display size options
private JMenu sizeJMenu;
private JRadioButtonMenuItem sizeMenuItems[];
private ButtonGroup sizeButtonGroup;
// JLabel and JTextArea to display text output
private JLabel prompt1JLabel, prompt2JLabel;
private JTextArea outputJTextArea;
// JButtons to represent first row of keys
private JButton tildeJButton, oneJButton, twoJButton,
threeJButton, fourJButton, fiveJButton, sixJButton,
sevenJButton, eightJButton, nineJButton, zeroJButton,
hyphenJButton, plusJButton, backspaceJButton;
// JButtons to represent second row of keys
private JButton tabJButton, qJButton, wJButton, eJButton,
rJButton, tJButton, yJButton, uJButton, iJButton, oJButton,
pJButton, leftBraceJButton, rightBraceJButton, slashJButton;
// JButtons to represent third row of keys
private JButton capsJButton, aJButton, sJButton, dJButton,
fJButton, gJButton, hJButton, jJButton, kJButton, lJButton,
colonJButton, quoteJButton, enterJButton;
// JButtons to represent fourth row of keys
private JButton shiftLeftJButton, zJButton, xJButton, cJButton,
vJButton, bJButton, nJButton, mJButton, commaJButton,
periodJButton, questionJButton, upJButton;
// JButtons to represent fifth row of keys
private JButton spaceJButton, leftJButton, downJButton,
rightJButton;
// JButton to store the last JButton typed
private JButton lastJButton;
// array of JButtons
private JButton[] keyJButtons =
new JButton[ KeyEvent.KEY_LAST + 1 ];
// Font of outputJTextArea
private Font outputFont;
// String array of font sizes
String sizeNames[] = { "12", "16", "20" };
// String array of font styles
String styleNames[] = { "Bold", "Italic" };
// no-argument constructor
public TypingApplication()
{
createUserInterface();
}
// set and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up typingJMenuBar
typingJMenuBar = new JMenuBar();
setJMenuBar( typingJMenuBar );
// set up displayJMenu
displayJMenu = new JMenu( "Display" );
displayJMenu.setMnemonic( KeyEvent.VK_D );
typingJMenuBar.add( displayJMenu );
// set up clearJMenuItem
clearJMenuItem = new JMenuItem( "Clear Text" );
clearJMenuItem.setMnemonic( KeyEvent.VK_C );
displayJMenu.add( clearJMenuItem );
displayJMenu.addSeparator();
clearJMenuItem.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJMenuItem is selected
public void actionPerformed( ActionEvent event )
{
clearJMenuItemActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up colorJMenuItem
colorJMenuItem = new JMenuItem( "Color..." );
colorJMenuItem.setMnemonic( KeyEvent.VK_O );
displayJMenu.add( colorJMenuItem );
colorJMenuItem.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when colorJMenuItem is selected
public void actionPerformed( ActionEvent event )
{
colorJMenuItemActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up formatJMenu
formatJMenu = new JMenu( "Format" );
formatJMenu.setMnemonic( KeyEvent.VK_F );
typingJMenuBar.add( formatJMenu );
// set up styleJMenu
styleJMenu = new JMenu( "Style" );
styleJMenu.setMnemonic( KeyEvent.VK_S );
formatJMenu.add( styleJMenu );
styleMenuItems = new JCheckBoxMenuItem[ styleNames.length ];
// set up styleMenuItems
for ( int count = 0; count < styleMenuItems.length; count++ )
{
styleMenuItems[ count ] = new JCheckBoxMenuItem(
styleNames[ count ] );
styleJMenu.add( styleMenuItems[ count ] );
styleMenuItems[ count ].addItemListener(
new ItemListener() // anonymous inner class
{
// event handler called when styleMenuItems selected
public void itemStateChanged( ItemEvent event )
{
styleMenuItemsStateChanged( event );
}
} // end anonymous inner class
); // end call to addItemListener
} // end for
// set up sizeJMenu
sizeJMenu = new JMenu( "Size" );
sizeJMenu.setMnemonic( KeyEvent.VK_Z );
formatJMenu.add( sizeJMenu );
sizeMenuItems = new JRadioButtonMenuItem[ sizeNames.length ];
sizeButtonGroup = new ButtonGroup();
// set up sizeMenuItems
for ( int count = 0; count < sizeMenuItems.length; count++ )
{
sizeMenuItems[ count ] = new JRadioButtonMenuItem(
sizeNames[ count ] );
sizeJMenu.add( sizeMenuItems[ count ] );
sizeButtonGroup.add( sizeMenuItems[ count ] );
sizeMenuItems[ count ].addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when sizeMenuItems is selected
public void actionPerformed( ActionEvent event )
{
sizeMenuItemsActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
} // end for
// set up prompt1JLabel
prompt1JLabel = new JLabel( "Type some text using your " +
"keyboard. The keys you press will be highlighted and " +
"the text will be displayed." );
prompt1JLabel.setBounds( 15, 5, 725, 20 );
contentPane.add( prompt1JLabel );
// set up prompt2JLabel
prompt2JLabel = new JLabel( "Note: Clicking the buttons " +
"with your mouse will not perform any action." );
prompt2JLabel.setBounds( 15, 20, 725, 25 );
contentPane.add( prompt2JLabel );
// set up outputJTextArea
outputJTextArea = new JTextArea();
outputJTextArea.setBounds( 15, 50, 725, 175 );
outputJTextArea.setLineWrap( true );
contentPane.add( outputJTextArea );
outputFont = outputJTextArea.getFont();
outputJTextArea.addKeyListener(
new KeyListener() // anonymous inner class
{
// event handler called when any key is pressed
public void keyPressed( KeyEvent event )
{
outputJTextAreaKeyPressed( event );
}
// event handler called when any key is released
public void keyReleased( KeyEvent event )
{
outputJTextAreaKeyReleased( event );
}
// event handler called when any key is typed
public void keyTyped( KeyEvent event )
{
}
} // end anonymous inner class
); // end call to addKeyListener
outputJTextArea.addFocusListener(
new FocusAdapter() // anonymous inner class
{
// event handler called when outputJTextArea loses focus
public void focusLost( FocusEvent event )
{
outputJTextAreaFocusLost( event );
}
} // end anonymous inner class
); // end call to addFocusListener
// set up tildeJButton
tildeJButton = new JButton( "~" );
tildeJButton.setBounds( 15, 250, 48, 48 );
contentPane.add( tildeJButton );
keyJButtons[ KeyEvent.VK_BACK_QUOTE ] = tildeJButton;
// set up oneJButton
oneJButton = new JButton( "1" );
oneJButton.setBounds( 63, 250, 48, 48 );
contentPane.add( oneJButton );
keyJButtons[ KeyEvent.VK_1 ] = oneJButton;
// set up twoJButton
twoJButton = new JButton( "2" );
twoJButton.setBounds( 111, 250, 48, 48 );
contentPane.add( twoJButton );
keyJButtons[ KeyEvent.VK_2 ] = twoJButton;
// set up threeJButton
threeJButton = new JButton( "3" );
threeJButton.setBounds( 159, 250, 48, 48 );
contentPane.add( threeJButton );
keyJButtons[ KeyEvent.VK_3 ] = threeJButton;
// set up fourJButton
fourJButton = new JButton( "4" );
fourJButton.setBounds( 207, 250, 48, 48 );
contentPane.add( fourJButton );
keyJButtons[ KeyEvent.VK_4 ] = fourJButton;
// set up fiveJButton
fiveJButton = new JButton( "5" );
fiveJButton.setBounds( 255, 250, 48, 48 );
contentPane.add( fiveJButton );
keyJButtons[ KeyEvent.VK_5 ] = fiveJButton;
// set up sixJButton
sixJButton = new JButton( "6" );
sixJButton.setBounds( 303, 250, 48, 48 );
contentPane.add( sixJButton );
keyJButtons[ KeyEvent.VK_6 ] = sixJButton;
// set up sevenJButton
sevenJButton = new JButton( "7" );
sevenJButton.setBounds( 351, 250, 48, 48 );
contentPane.add( sevenJButton );
keyJButtons[ KeyEvent.VK_7 ] = sevenJButton;
// set up eightJButton
eightJButton = new JButton( "8" );
eightJButton.setBounds( 399, 250, 48, 48 );
contentPane.add( eightJButton );
keyJButtons[ KeyEvent.VK_8 ] = eightJButton;
// set up nineJButton
nineJButton = new JButton( "9" );
nineJButton.setBounds( 447, 250, 48, 48 );
contentPane.add( nineJButton );
keyJButtons[ KeyEvent.VK_9 ] = nineJButton;
// set up zeroJButton
zeroJButton = new JButton( "0" );
zeroJButton.setBounds( 495, 250, 48, 48 );
contentPane.add( zeroJButton );
keyJButtons[ KeyEvent.VK_0 ] = zeroJButton;
// set up hyphenJButton
hyphenJButton = new JButton( "-" );
hyphenJButton.setBounds( 543, 250, 48, 48 );
contentPane.add( hyphenJButton );
keyJButtons[ KeyEvent.VK_MINUS ] = hyphenJButton;
// set up plusJButton
plusJButton = new JButton( "+" );
plusJButton.setBounds( 591, 250, 48, 48 );
contentPane.add( plusJButton );
keyJButtons[ KeyEvent.VK_EQUALS ] = plusJButton;
// set up backspaceJButton
backspaceJButton = new JButton( "Backspace" );
backspaceJButton.setBounds( 639, 250, 100, 48 );
contentPane.add( backspaceJButton );
keyJButtons[ KeyEvent.VK_BACK_SPACE ] = backspaceJButton;
// set up tabJButton
tabJButton = new JButton( "Tab" );
tabJButton.setBounds( 15, 298, 75, 48 );
contentPane.add( tabJButton );
keyJButtons[ KeyEvent.VK_TAB ] = tabJButton;
// set up qJButton
qJButton = new JButton( "Q" );
qJButton.setBounds( 90, 298, 48, 48 );
contentPane.add( qJButton );
keyJButtons[ KeyEvent.VK_Q ] = qJButton;
// set up wJButton
wJButton = new JButton( "W" );
wJButton.setBounds( 138, 298, 48, 48);
contentPane.add( wJButton );
keyJButtons[ KeyEvent.VK_W ] = wJButton;
// set up eJButton
eJButton = new JButton( "E" );
eJButton.setBounds( 186, 298, 48, 48 );
contentPane.add( eJButton );
keyJButtons[ KeyEvent.VK_E ] = eJButton;
// set up rJButton
rJButton = new JButton( "R" );
rJButton.setBounds( 234, 298, 48, 48 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -