📄 typingapplication.java
字号:
contentPane.add( rJButton );
keyJButtons[ KeyEvent.VK_R ] = rJButton;
// set up tJButton
tJButton = new JButton( "T" );
tJButton.setBounds( 282, 298, 48, 48 );
contentPane.add( tJButton );
keyJButtons[ KeyEvent.VK_T ] = tJButton;
// set up yJButton
yJButton = new JButton( "Y" );
yJButton.setBounds( 330, 298, 48, 48 );
contentPane.add( yJButton );
keyJButtons[ KeyEvent.VK_Y ] = yJButton;
// set up uJButton
uJButton = new JButton( "U" );
uJButton.setBounds( 378, 298, 48, 48 );
contentPane.add( uJButton );
keyJButtons[ KeyEvent.VK_U ] = uJButton;
// set up iJButton
iJButton = new JButton( "I" );
iJButton.setBounds( 426, 298, 48, 48 );
contentPane.add( iJButton );
keyJButtons[ KeyEvent.VK_I ] = iJButton;
// set up oJButton
oJButton = new JButton( "O" );
oJButton.setBounds( 474, 298, 48, 48 );
contentPane.add( oJButton );
keyJButtons[ KeyEvent.VK_O ] = oJButton;
// set up pJButton
pJButton = new JButton( "P" );
pJButton.setBounds( 522, 298, 48, 48 );
contentPane.add( pJButton );
keyJButtons[ KeyEvent.VK_P ] = pJButton;
// set up leftBraceJButton
leftBraceJButton = new JButton( "[" );
leftBraceJButton.setBounds( 570, 298, 48, 48 );
contentPane.add( leftBraceJButton );
keyJButtons[ KeyEvent.VK_OPEN_BRACKET ] = leftBraceJButton;
// set up rightBraceJButton
rightBraceJButton = new JButton( "]" );
rightBraceJButton.setBounds( 618, 298, 48, 48 );
contentPane.add( rightBraceJButton );
keyJButtons[ KeyEvent.VK_CLOSE_BRACKET ] = rightBraceJButton;
// set up slashJButton
slashJButton = new JButton( "\\" );
slashJButton.setBounds( 666, 298, 48, 48 );
contentPane.add( slashJButton );
keyJButtons[ KeyEvent.VK_BACK_SLASH ] = slashJButton;
// set up capsJButton
capsJButton = new JButton( "Caps" );
capsJButton.setBounds( 15, 346, 75, 48 );
contentPane.add( capsJButton );
keyJButtons[ KeyEvent.VK_CAPS_LOCK ] = capsJButton;
// set up aJButton
aJButton = new JButton( "A" );
aJButton.setBounds( 90, 346, 48, 48 );
contentPane.add( aJButton );
keyJButtons[ KeyEvent.VK_A ] = aJButton;
// set up sJButton
sJButton = new JButton( "S" );
sJButton.setBounds( 138, 346, 48, 48 );
contentPane.add( sJButton );
keyJButtons[ KeyEvent.VK_S ] = sJButton;
// set up dJButton
dJButton = new JButton( "D" );
dJButton.setBounds( 186, 346, 48, 48 );
contentPane.add( dJButton );
keyJButtons[ KeyEvent.VK_D ] = dJButton;
// set up fJButton
fJButton = new JButton( "F" );
fJButton.setBounds( 234, 346, 48, 48 );
contentPane.add( fJButton );
keyJButtons[ KeyEvent.VK_F ] = fJButton;
// set up gJButton
gJButton = new JButton( "G" );
gJButton.setBounds( 282, 346, 48, 48 );
contentPane.add( gJButton );
keyJButtons[ KeyEvent.VK_G ] = gJButton;
// set up hJButton
hJButton = new JButton( "H" );
hJButton.setBounds( 330, 346, 48, 48 );
contentPane.add( hJButton );
keyJButtons[ KeyEvent.VK_H ] = hJButton;
// set up jJButton
jJButton = new JButton( "J" );
jJButton.setBounds( 378, 346, 48, 48 );
contentPane.add( jJButton );
keyJButtons[ KeyEvent.VK_J ] = jJButton;
// set up kJButton
kJButton = new JButton( "K" );
kJButton.setBounds( 426, 346, 48, 48 );
contentPane.add( kJButton );
keyJButtons[ KeyEvent.VK_K ] = kJButton;
// set up lJButton
lJButton = new JButton( "L" );
lJButton.setBounds( 474, 346, 48, 48 );
contentPane.add( lJButton );
keyJButtons[ KeyEvent.VK_L ] = lJButton;
// set up colonJButton
colonJButton = new JButton( ":" );
colonJButton.setBounds( 522, 346, 48, 48 );
contentPane.add( colonJButton );
keyJButtons[ KeyEvent.VK_SEMICOLON ] = colonJButton;
// set up quoteJButton
quoteJButton = new JButton( "\"" );
quoteJButton.setBounds( 570, 346, 48, 48 );
contentPane.add( quoteJButton );
keyJButtons[ KeyEvent.VK_QUOTE ] = quoteJButton;
// set up enterJButton
enterJButton = new JButton( "Enter" );
enterJButton.setBounds( 618, 346, 96, 48 );
contentPane.add( enterJButton );
keyJButtons[ KeyEvent.VK_ENTER ] = enterJButton;
// set up shiftLeftJButton
shiftLeftJButton = new JButton( "Shift" );
shiftLeftJButton.setBounds( 15, 394, 100, 48 );
contentPane.add( shiftLeftJButton );
keyJButtons[ KeyEvent.VK_SHIFT ] = shiftLeftJButton;
// set up zJButton
zJButton = new JButton( "Z" );
zJButton.setBounds( 115, 394, 48, 48 );
contentPane.add( zJButton );
keyJButtons[ KeyEvent.VK_Z ] = zJButton;
// set up xJButton
xJButton = new JButton( "X" );
xJButton.setBounds( 163, 394, 48, 48 );
contentPane.add( xJButton );
keyJButtons[ KeyEvent.VK_X ] = xJButton;
// set up cJButton
cJButton = new JButton( "C" );
cJButton.setBounds( 211, 394, 48, 48 );
contentPane.add( cJButton );
keyJButtons[ KeyEvent.VK_C ] = cJButton;
// set up vJButton
vJButton = new JButton( "V" );
vJButton.setBounds( 259, 394, 48, 48 );
contentPane.add( vJButton );
keyJButtons[ KeyEvent.VK_V ] = vJButton;
// set up bJButton
bJButton = new JButton( "B" );
bJButton.setBounds( 307, 394, 48, 48 );
contentPane.add( bJButton );
keyJButtons[ KeyEvent.VK_B ] = bJButton;
// set up nJButton
nJButton = new JButton( "N" );
nJButton.setBounds( 355, 394, 48, 48 );
contentPane.add( nJButton );
keyJButtons[ KeyEvent.VK_N ] = nJButton;
// set up mJButton
mJButton = new JButton( "M" );
mJButton.setBounds( 403, 394, 48, 48 );
contentPane.add( mJButton );
keyJButtons[ KeyEvent.VK_M ] = mJButton;
// set up commaJButton
commaJButton = new JButton( "," );
commaJButton.setBounds( 451, 394, 48, 48 );
contentPane.add( commaJButton );
keyJButtons[ KeyEvent.VK_COMMA ] = commaJButton;
// set up periodJButton
periodJButton = new JButton( "." );
periodJButton.setBounds( 499, 394, 48, 48 );
contentPane.add( periodJButton );
keyJButtons[ KeyEvent.VK_PERIOD ] = periodJButton;
// set up questionJButton
questionJButton = new JButton( "?" );
questionJButton.setBounds( 547, 394, 48, 48 );
contentPane.add( questionJButton );
keyJButtons[ KeyEvent.VK_SLASH ] = questionJButton;
// set up upJButton
upJButton = new JButton( "^" );
upJButton.setBounds( 618, 394, 48, 48 );
contentPane.add( upJButton );
keyJButtons[ KeyEvent.VK_UP ] = upJButton;
// set up spaceJButton
spaceJButton = new JButton( "" );
spaceJButton.setBounds( 208, 442, 300, 48 );
contentPane.add( spaceJButton );
keyJButtons[ KeyEvent.VK_SPACE ] = spaceJButton;
// set up leftJButton
leftJButton = new JButton( "<" );
leftJButton.setBounds( 570, 442, 48, 48 );
contentPane.add( leftJButton );
keyJButtons[ KeyEvent.VK_LEFT ] = leftJButton;
// set up downJButton
downJButton = new JButton( "v" );
downJButton.setBounds( 618, 442, 48, 48 );
contentPane.add( downJButton );
keyJButtons[ KeyEvent.VK_DOWN ] = downJButton;
// set up rightJButton
rightJButton = new JButton( ">" );
rightJButton.setBounds( 666, 442, 48, 48 );
contentPane.add( rightJButton );
keyJButtons[ KeyEvent.VK_RIGHT ] = rightJButton;
// set properties of application's window
setTitle( "Typing Application" ); // set title bar string
setSize( 760, 550 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// reset the color of the lastJButton
private void outputJTextAreaFocusLost( FocusEvent event )
{
resetColor();
} // end method outputJTextAreaFocusLost
// clear text
private void clearJMenuItemActionPerformed( ActionEvent event )
{
outputJTextArea.setText( "" );
} // end method clearJMenuItemActionPerformed
// highlight JButton passed as argument
private void changeColor( JButton changeJButton )
{
if ( changeJButton != null )
{
resetColor();
changeJButton.setBackground( Color.YELLOW );
lastJButton = changeJButton;
}
} // end method changeColor
// changes lastJButton's color back to default
private void resetColor()
{
if ( lastJButton != null )
{
lastJButton.setBackground( this.getBackground() );
}
} // end method resetColor
// change text color when user selects Colors... JMenuItem
private void colorJMenuItemActionPerformed( ActionEvent event )
{
Color foregroundColor = JColorChooser.showDialog(
this, "Choose a color", Color.BLACK );
// if the user selected a color
if ( foregroundColor != null )
{
// set foreground color of outputJTextArea
outputJTextArea.setForeground( foregroundColor );
}
} // end method colorMenuItemActionPerformed
// change font when user selects an item from Style submenu
private void styleMenuItemsStateChanged( ItemEvent event )
{
int style = Font.PLAIN;
// check for bold selection
if ( styleMenuItems[ 0 ].isSelected() )
{
style += Font.BOLD;
}
// check for italic selection
if ( styleMenuItems[ 1 ].isSelected() )
{
style += Font.ITALIC;
}
// create a new Font with the specified style
outputFont = new Font( outputFont.getName(), style,
outputFont.getSize() );
outputJTextArea.setFont( outputFont ); // set the font
} // end method styleMenuItemsStateChanged
// change font size when user selects an item from size submenu
private void sizeMenuItemsActionPerformed( ActionEvent event )
{
JRadioButtonMenuItem sizeMenuItem =
( JRadioButtonMenuItem ) event.getSource();
// create a new Font with the specified size
outputFont = new Font( outputFont.getName(),
outputFont.getStyle(),
Integer.parseInt( sizeMenuItem.getText() ) );
outputJTextArea.setFont( outputFont ); // set the font
} // end method sizeMenuItemsActionPerformed
// highlight corresponding JButton when a key is pressed
private void outputJTextAreaKeyPressed( KeyEvent event )
{
// get the key code for this event
int buttonIndex = event.getKeyCode();
// change the color of the associated JButton
changeColor( keyJButtons[ buttonIndex ] );
} // end method outputJTextAreaKeyPressed
// reset the color of the pressed key's JButton
private void outputJTextAreaKeyReleased( KeyEvent event )
{
resetColor();
} // end method outputJTextAreaKeyReleased
// main method
public static void main( String[] args )
{
TypingApplication application = new TypingApplication();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class TypingApplication
/**************************************************************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -