JToolBar vToolBar = new JToolBar(JToolBar.VERTICAL);
以下代碼將按鈕添加到工具欄。我們通過將其邊距設(shè)置為零來使JButton變小。
JButton newButton = new JButton("New");
newButton.setMargin(new Insets(0, 0, 0, 0));
newButton.setToolTipText("Add a new file");
toolBar.add(newButton);
以下代碼將按鈕添加到工具欄。我們通過將其邊距設(shè)置為零來使JButton變小。...
class ExitAction extends AbstractAction {
public ExitAction(String action) {
super(action);
// Set tooltip text for the toolbar
this.putValue(SHORT_DESCRIPTION, "Exit the application");
// Set a mnemonic key
this.putValue(MNEMONIC_KEY, KeyEvent.VK_E);
}
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
ExitAction exitAction = new ExitAction("Exit");
JButton exitButton = new JButton(ExitAction);
JMenuItem exitMenuItem = new JMenuItem(exitAction);
JButton exitToolBarButton = new JButton(exitAction);
exitToolBarButton.setMargin(new Insets(0,0,0,0));
import java.awt.BorderLayout;
import java.awt.Container;
/*from w w w . ja v a2 s . c o m*/
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("JToolbar Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JToolBar toolbar = new JToolBar();
Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
JToggleButton button = new JToggleButton(icon);
toolbar.add(button);
icon = MetalIconFactory.getFileChooserHomeFolderIcon();
button = new JToggleButton(icon);
toolbar.add(button);
icon = MetalIconFactory.getFileChooserListViewIcon();
button = new JToggleButton(icon);
toolbar.add(button);
content.add(toolbar, BorderLayout.NORTH);
f.setSize(300, 100);
f.setVisible(true);
}
}
顯示垂直工具欄
import java.awt.BorderLayout;
//from w w w . j a va2s .c o m
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
public class Main {
public static void main(String[] args) {
JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
JButton selectb = new JButton(new ImageIcon("select.gif"));
JButton freehandb = new JButton(new ImageIcon("freehand.gif"));
JButton shapeedb = new JButton(new ImageIcon("shapeed.gif"));
JButton penb = new JButton(new ImageIcon("pen.gif"));
toolbar.add(selectb);
toolbar.add(freehandb);
toolbar.add(shapeedb);
toolbar.add(penb);
JFrame f = new JFrame();
f.add(toolbar, BorderLayout.WEST);
f.setSize(250, 350);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
更多建議: