Home
 Swing Technology
 GUI Features
 Source code
 Enhancements
 Sample Code

ToolBar Example:

/**Toolbar used in the inner panel of TicTacToe game.
*/
private JToolBar createToolBar() {
JToolBar toolbar = new JToolBar();
JButton jb;

/** create a button and add an event handler to the button.
Add the button to the toolbar.
 */

toolbar.add (jb = new JButton ("New Game", NewIcon));
jb.setToolTipText ("New Game");
jb.setActionCommand("New");
jb.addActionListener(this);
toolbar.addSeparator();

toolbar.add(jb=new JButton("Quit", DukeWaveSmallIcon));
jb.setToolTipText ("Quit");
jb.setActionCommand("Quit");
jb.setHorizontalAlignment(JButton.RIGHT);
jb.addActionListener(this);

return toolbar;
}
 

MenuBar Example:

/** create the menu bar inside the TicTacToe panel */

private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenuItem item;

/** create file menu, add Action Even handler, and add it to menubari*/

file = new JMenu ("Game");
file.setMnemonic ('G');
file.add (item = new JMenuItem ("New Game",NewSmallIcon));
item.setMnemonic ('N');
item.setActionCommand("New");
item.addActionListener(this);


JMenu colormenu = new JMenu("Change Color");

ButtonGroup colorButtonGroup = new ButtonGroup();
JRadioButtonMenuItem redButton = new JRadioButtonMenuItem("Red");
JRadioButtonMenuItem blueButton = new JRadioButtonMenuItem("Blue");

redButton.setSelected(true);
redButton.setMnemonic('R');
redButton.addItemListener(this);

blueButton.setMnemonic('B');
blueButton.addItemListener(this);

colorButtonGroup.add(redButton);
colorButtonGroup.add(blueButton);
colormenu.add(redButton);
colormenu.add(blueButton);
options.add(colormenu);

/** create compute go first checkbox */

JCheckBoxMenuItem compGoFirstCheckBox = new JCheckBoxMenuItem("Computer go first");

compGoFirstCheckBox.setMnemonic('C');
compGoFirstCheckBox.addItemListener(this);

options.addSeparator();
options.add(compGoFirstCheckBox);

/** create Help menu */

help = new JMenu ("Help");
help.setMnemonic('H');
help.add(item = new JMenuItem("Instruction", HelpIcon));
item.setMnemonic('I');
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doInstruction();
}
});

help.add(item = new JMenuItem("About TicTacToe",DukeMagnifySmallIcon));
item.setMnemonic('A');
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doAboutTicTacToe();
}
});
 

/** add all the menuitems to the menubar */
menuBar.add(file);
menuBar.add(options);
menuBar.add(help);

return menuBar;
}

Tab Panel Example:

public MengGame() {

tabbedPane = new JTabbedPane();

tictactoePanel = makeTicTacToePanel("TicTacToe");
tabbedPane.addTab("TicTacToe-Simple",TicTacToeIcon,tictactoePanel, "Tic Tac Toe Game");
tabbedPane.setSelectedIndex(0);

tictactoePanel2 = makeTicTacToePanel2("Fancy TicTacToe");

tabbedPane.addTab("TicTacToe-Fancy", TicTacToeIcon, tictactoePanel2, "Tic Tac Toe game");

setLayout(new GridLayout(1,0));
add(tabbedPane);

// Add Tab change listener
createTabListener();
}