Wednesday, August 10, 2011

Add Mnemonic on JButton

Output:
Code:

/**

* File: jbuttonMnemonic.java

* Tiltle: Add Mnemonic on JButton

* Author: http://java-code-complete.blogspot.com/

*/



//Java Core Package

import javax.swing.*;

//Java Extension Package

import java.awt.*;



public class jbuttonMnemonic extends JFrame {



//Initializing JButton

private JButton button;

private JLabel info;



//Setting up GUI

public jbuttonMnemonic() {



//Setting up the Title of the Window

super("Add Mnemonic on JButton");



//Set Size of the Window (WIDTH, HEIGHT)

setSize(250,80);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JButton

button = new JButton("My New JButton. Press ALT+N");



//Set Mnemonic on JButton. Press ALT+N to click the button.

button.setMnemonic('N');



//Setting up the container ready for the components to be added.

Container pane = getContentPane();

setContentPane(pane);



//Setting up the container layout

FlowLayout flow = new FlowLayout(FlowLayout.CENTER);

pane.setLayout(flow);



//Adding the JButton component to the container

pane.add(button);



/**Set all the Components Visible.

* If it is set to "false", the components in the container will not be visible.

*/

setVisible(true);

}



//Main Method

public static void main (String[] args) {

jbuttonMnemonic jbnm = new jbuttonMnemonic();

}

}


Important Part of the Program:

//Constructing JButton

JButton button = new JButton("My New JButton. Press ALT+N");



//Set Mnemonic on JButton. Press ALT+N to click the button.

button.setMnemonic('N');

No comments:

Post a Comment