//Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); if (imageURL != null) { //image found button.setIcon(new ImageIcon(imageURL, altText)); } else { //no image found button.setText(altText); System.err.println("Resource not found: " + imgLocation); } return button; } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); String description = null; // Handle each button. if (PREVIOUS.equals(cmd)) { //first button clicked description = "taken you to the previous <something>."; } else if (UP.equals(cmd)) { // second button clicked description = "taken you up one level to <something>."; } else if (NEXT.equals(cmd)) { // third button clicked description = "taken you to the next <something>."; } displayResult("If this were a real app, it would have " + description); } protected void displayResult(String actionDescription) { textArea.append(actionDescription + newline); textArea.setCaretPosition(textArea.getDocument().getLength()); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("ToolBarDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add content to the window. frame.add(new ToolBarDemo()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { // Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI(); } }); } }
* @param args
*/
private JToolBar toolBar;
private JButton button1,button2;
public JToolBarTest(){
toolBar = new JToolBar("工具箱");
button1 = new JButton("button1,");
button2 = new JButton("button2");
toolBar.setFloatable(true);
toolBar.add(button1);
toolBar.add(button2);
getContentPane().add(toolBar,BorderLayout.NORTH);
setSize(200,300);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
JToolBarTest application = new JToolBarTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
希望对你有帮助
/**
* This method initializes JPanel_left
* 左控制面板
* @return javax.swing.JPanel
*/
private JPanel getJPanel_left() {
if (JPanel_left == null) {
JPanel_left = new JPanel()
{
public void paintComponent(Graphics g)
{
Image img = new ImageIcon("images/leftPanelbg.png").getImage();
g.drawImage(img,0,0, this.getWidth(), this.getHeight(), this);
//this.paintComponent(g);
}
};
JPanel_left.setLayout(null);
JPanel_left.setBounds(new Rectangle(-10, 130, 42, 560));//ԭʼ��С�� 42, 205
JPanel_left.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 0));
JPanel_left.add(getBtn_openLeftPane(), null);
JPanel_left.add(getBtn_closeLeftPane(), null);
JPanel_left.add(getBtn_stockManage(), null);
JPanel_left.add(getBtn_sellManage(), null);
JPanel_left.add(getBtn_storeManage(), null);
JPanel_left.add(getBtn_formShow(), null);
JPanel_left.add(getBtn_dayManage(), null);
JPanel_left.setOpaque(true);
JPanel_left.add(getJButton_foreGoodsManagement(), null);
}
return JPanel_left;
} /**
* This method initializes btn_openLeftPane
* 打开左控制面板
* @return javax.swing.JButton
*/
private JButton getBtn_openLeftPane() {
if (btn_openLeftPane == null) {
btn_openLeftPane = new JButton();
btn_openLeftPane.setBorderPainted(true);
btn_openLeftPane.setContentAreaFilled(true);
btn_openLeftPane.setBounds(new Rectangle(1, 7, 42, 17));
btn_openLeftPane.setText(">");
btn_openLeftPane.addActionListener(this);
btn_openLeftPane.setActionCommand("open");
}
return btn_openLeftPane;
} /**
* This method initializes btn_closeLeftPane
* 关闭左控制面板
* @return javax.swing.JButton
*/
private JButton getBtn_closeLeftPane() {
if (btn_closeLeftPane == null) {
btn_closeLeftPane = new JButton();
btn_closeLeftPane.setBorderPainted(true);
btn_closeLeftPane.setContentAreaFilled(true);
btn_closeLeftPane.setBounds(new Rectangle(1, 30, 42, 17));
btn_closeLeftPane.setText("<");
btn_closeLeftPane.addActionListener(this);
btn_closeLeftPane.setActionCommand("close");
}
return btn_closeLeftPane;
}
菜单栏关闭隐藏一部分代码
// 事件处理
public void actionPerformed(ActionEvent e) {
//打开菜单栏
if(e.getActionCommand().equals("open")){
for(int i=42;i<=205;i++){
JPanel_left.setBounds(-10, 130, i, 560);
}
}
//关闭菜单栏
else if(e.getActionCommand().equals("close")){
stockManage.hide();
sellManage.hide();
storeManage.hide();
dayManage.hide();
formAnalysis.hide();
this.repaint();
for(int i=205;i>=42;i--){
//JPanel_left.setSize(i,560);
JPanel_left.setBounds(-10, 130, i, 560);
}
}
package components;/*
* ToolBarDemo.java requires the following addditional files:
* images/Back24.gif
* images/Forward24.gif
* images/Up24.gif
*/import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;import java.net.URL;import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class ToolBarDemo extends JPanel
implements ActionListener {
protected JTextArea textArea;
protected String newline = "\n";
static final private String PREVIOUS = "previous";
static final private String UP = "up";
static final private String NEXT = "next"; public ToolBarDemo() {
super(new BorderLayout()); //Create the toolbar.
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar); //Create the text area used for output. Request
//enough space for 5 rows and 30 columns.
textArea = new JTextArea(5, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea); //Lay out the main panel.
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
toolBar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("yes, mouse clicked...");
}
});
} protected void addButtons(JToolBar toolBar) {
JButton button = null; //first button
button = makeNavigationButton("Back24", PREVIOUS,
"Back to previous something-or-other",
"Previous");
toolBar.add(button); //second button
button = makeNavigationButton("Up24", UP,
"Up to something-or-other",
"Up");
toolBar.add(button); //third button
button = makeNavigationButton("Forward24", NEXT,
"Forward to something-or-other",
"Next");
toolBar.add(button);
} protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText) {
//Look for the image.
String imgLocation = "images/"
+ imageName
+ ".gif";
URL imageURL = ToolBarDemo.class.getResource(imgLocation);
//Create and initialize the button.
JButton button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(this); if (imageURL != null) { //image found
button.setIcon(new ImageIcon(imageURL, altText));
} else { //no image found
button.setText(altText);
System.err.println("Resource not found: "
+ imgLocation);
} return button;
} public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String description = null; // Handle each button.
if (PREVIOUS.equals(cmd)) { //first button clicked
description = "taken you to the previous <something>.";
} else if (UP.equals(cmd)) { // second button clicked
description = "taken you up one level to <something>.";
} else if (NEXT.equals(cmd)) { // third button clicked
description = "taken you to the next <something>.";
} displayResult("If this were a real app, it would have "
+ description);
} protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
textArea.setCaretPosition(textArea.getDocument().getLength());
} /**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ToolBarDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add content to the window.
frame.add(new ToolBarDemo()); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
/**
* Creates a window which contains the toolbar after it has been
* dragged out from its container
* @return a <code>RootPaneContainer</code> object, containing the toolbar.
* @since 1.4
*/
protected RootPaneContainer createFloatingWindow(JToolBar toolbar) {
class ToolBarDialog extends JDialog {
public ToolBarDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
} public ToolBarDialog(Dialog owner, String title, boolean modal) {
super(owner, title, modal);
} // Override createRootPane() to automatically resize
// the frame when contents change
protected JRootPane createRootPane() {
JRootPane rootPane = new JRootPane() {
private boolean packing = false; public void validate() {
super.validate();
if (!packing) {
packing = true;
pack();
packing = false;
}
}
};
rootPane.setOpaque(true);
return rootPane;
}
} JDialog dialog;
Window window = SwingUtilities.getWindowAncestor(toolbar);
if (window instanceof Frame) {
dialog = new ToolBarDialog((Frame)window, toolbar.getName(), false);
} else if (window instanceof Dialog) {
dialog = new ToolBarDialog((Dialog)window, toolbar.getName(), false);
} else {
dialog = new ToolBarDialog((Frame)null, toolbar.getName(), false);
} dialog.getRootPane().setName("ToolBar.FloatingWindow");
dialog.setTitle(toolbar.getName());
dialog.setResizable(false);
WindowListener wl = createFrameListener();
dialog.addWindowListener(wl);
return dialog;
}