How to write a JToolBar application with Java
379 viewsHere's a small demo I wrote that shows you how to use the Java JToolBar. The toolbar doesn't do a lot except for showing a message when the user clicks one of the toolbar items.
Code:
JAVA:
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
-
import javax.swing.ImageIcon;
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.JToolBar;
-
-
int x = 500;
-
public JToolBarDemo(){
-
setLayout(null);
-
-
item1.addActionListener(this);
-
item2.addActionListener(this);
-
item3.addActionListener(this);
-
-
toolBar.add(item1);
-
toolBar.add(item2);
-
toolBar.add(item3);
-
-
toolBar.setFloatable(false);
-
toolBar.setBounds(0,0,x,40);
-
-
message.setBounds(0,40,200,20);
-
-
add(toolBar);
-
add(message);
-
setTitle("Toolbar example By: HappyFace - www.engineeringserver.com");
-
setSize(x,200);
-
setResizable(false);
-
setLocationRelativeTo(null);
-
setVisible(true);
-
}
-
new JToolBarDemo();
-
}
-
if(e.getSource() == item1){
-
message.setText("You clicked: save as..");
-
}
-
if(e.getSource() == item2){
-
message.setText("You clicked: delete");
-
}
-
-
if(e.getSource() == item3){
-
message.setText("You clicked: about us");
-
}
-
}
-
}
The images that were used in this demo application can be freely downloaded here: http://java.sun.com/developer/techDocs/hi/repository/













