Java Swing Меню и панели инструментов

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Creating a menubar)
(нарушало авторские права)
 
(8 промежуточных версий не показаны.)
Строка 1: Строка 1:
-
== Создаем menubar ==
 
-
Menubar это одна из многих составляющих кашерного приложения с GUI, хотя люди, придумавшие слово "кашерный" могут со мной не согласиться. Сразу же приношу им свои изменения. Что же до menubar, сие есть сгрупированный набор команд. В то время как при использовании консольного приложения вы должны были запоминать все эти загадочные команды ''(странно, а я думал, что загадочным линуксоидам нравится запоминать загадочные команды и от этого становиться еще более загадочными - прим. переводчика)'' arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application.
 
-
In Java Swing, to implement a menubar, we use three objects. A <b>JMenuBar</b>, a
 
-
 
-
<b>JMenu</b> and a <b>JMenuItem</b>.
 
-
 
-
== Simple menubar ==
 
-
We begin with a simple menubar example.
 
-
 
-
<source lang="cpp">
 
-
import java.awt.event.ActionEvent;
 
-
import java.awt.event.ActionListener;
 
-
import java.awt.event.KeyEvent;
 
-
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JMenu;
 
-
import javax.swing.JMenuBar;
 
-
import javax.swing.JMenuItem;
 
-
import javax.swing.UIManager;
 
-
 
-
 
-
public class Menu extends JFrame {
 
-
 
-
    public Menu() {
 
-
 
-
        setTitle("JMenuBar");
 
-
 
-
        JMenuBar menubar = new JMenuBar();
 
-
        ImageIcon icon = new ImageIcon("exit.png");
 
-
 
-
        JMenu file = new JMenu("File");
 
-
        file.setMnemonic(KeyEvent.VK_F);
 
-
 
-
        JMenuItem fileClose = new JMenuItem("Close", icon);
 
-
        fileClose.setMnemonic(KeyEvent.VK_C);
 
-
        fileClose.setToolTipText("Exit application");
 
-
        fileClose.addActionListener(new ActionListener() {
 
-
            public void actionPerformed(ActionEvent event) {
 
-
                System.exit(0);
 
-
 
-
        });
 
-
 
-
        file.add(fileClose);
 
-
 
-
        menubar.add(file);
 
-
 
-
        setJMenuBar(menubar);
 
-
 
-
        setSize(250, 200);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
 
-
        new Menu();
 
-
 
-
    }
 
-
}
 
-
 
-
</source>
 
-
<br>
 
-
 
-
Our example will show a menu with one item. Selecting the close menu item we close the application.
 
-
 
-
<source lang="cpp">
 
-
JMenuBar menubar = new JMenuBar();
 
-
</source>
 
-
 
-
Here we create a menubar.
 
-
 
-
<source lang="cpp">
 
-
 
-
ImageIcon icon = new ImageIcon("exit.png");
 
-
</source>
 
-
 
-
We will display an icon in the menu.
 
-
 
-
<source lang="cpp">
 
-
JMenu file = new JMenu("File");
 
-
file.setMnemonic(KeyEvent.VK_F);
 
-
</source>
 
-
 
-
We create a menu object. The menus can be accessed via the keybord as well. To bind a menu to a particular key, we use the <b>setMnemonic</b> method. In our case, the menu can be opened with the <b>ALT + F</b> shortcut.
 
-
 
-
 
-
<source lang="cpp">
 
-
fileClose.setToolTipText("Exit application");
 
-
</source>
 
-
 
-
This code line creates a tooltip for a menu item.
 
-
 
-
 
-
<br>
 
-
[[image: java_swing_menubar.jpg | center]]
 
-
 
-
== Submenu ==
 
-
Each menu can also have a submenu. This way we can group similar commnads into groups. For example we can place commands that hide/show various toolbars like personal bar, address bar, status bar or navigation bar into a submenu called toolbars. Within a menu, we can seperate commands with a separator. It is a simple line. It is common practice to separate commands like new, open, save from commands like print, print preview with a single separator.
 
-
Menus commands can be launched via keyboard shortcuts. For this, we define menu item accelerators.
 
-
 
-
 
-
<source lang="cpp">
 
-
import java.awt.event.ActionEvent;
 
-
import java.awt.event.ActionListener;
 
-
import java.awt.event.KeyEvent;
 
-
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JMenu;
 
-
import javax.swing.JMenuBar;
 
-
import javax.swing.JMenuItem;
 
-
import javax.swing.KeyStroke;
 
-
 
-
 
-
public class Submenu extends JFrame {
 
-
 
-
    public Submenu() {
 
-
 
-
        setTitle("Submenu");
 
-
 
-
        JMenuBar menubar = new JMenuBar();
 
-
        ImageIcon iconNew = new ImageIcon("new.png");
 
-
        ImageIcon iconOpen = new ImageIcon("open.png");
 
-
        ImageIcon iconSave = new ImageIcon("save.png");
 
-
        ImageIcon iconClose = new ImageIcon("exit.png");
 
-
 
-
        JMenu file = new JMenu("File");
 
-
        file.setMnemonic(KeyEvent.VK_F);
 
-
 
-
        JMenu imp = new JMenu("Import");
 
-
        imp.setMnemonic(KeyEvent.VK_M);
 
-
 
-
        JMenuItem newsf = new JMenuItem("Import newsfeed list...");
 
-
        JMenuItem bookm = new JMenuItem("Import bookmarks...");
 
-
        JMenuItem mail = new JMenuItem("Import mail...");
 
-
 
-
        imp.add(newsf);
 
-
        imp.add(bookm);
 
-
        imp.add(mail);
 
-
 
-
        JMenuItem fileNew = new JMenuItem("New", iconNew);
 
-
        fileNew.setMnemonic(KeyEvent.VK_N);
 
-
 
-
        JMenuItem fileOpen = new JMenuItem("Open", iconOpen);
 
-
        fileNew.setMnemonic(KeyEvent.VK_O);
 
-
 
-
        JMenuItem fileSave = new JMenuItem("Save", iconSave);
 
-
        fileSave.setMnemonic(KeyEvent.VK_S);   
 
-
 
-
        JMenuItem fileClose = new JMenuItem("Close", iconClose);
 
-
        fileClose.setMnemonic(KeyEvent.VK_C);
 
-
        fileClose.setToolTipText("Exit application");
 
-
        fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
 
-
            ActionEvent.CTRL_MASK));
 
-
 
-
        fileClose.addActionListener(new ActionListener() {
 
-
            public void actionPerformed(ActionEvent event) {
 
-
                System.exit(0); 
 
-
            }
 
-
 
-
        });
 
-
 
-
        file.add(fileNew);
 
-
        file.add(fileOpen);
 
-
        file.add(fileSave);
 
-
        file.addSeparator();
 
-
        file.add(imp);
 
-
        file.addSeparator();
 
-
        file.add(fileClose);
 
-
 
-
        menubar.add(file);
 
-
 
-
        setJMenuBar(menubar);
 
-
 
-
        setSize(360, 250);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new Submenu();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In this example, we create a submenu, a menu separator and an accelerator key.
 
-
 
-
<source lang="cpp">
 
-
JMenu imp = new JMenu("Import");
 
-
...
 
-
file.add(imp);
 
-
</source>
 
-
 
-
A submenu is just like any other normal menu. It is created the same way. We simply add a menu to existing
 
-
menu.
 
-
 
-
<source lang="cpp">
 
-
fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
 
-
    ActionEvent.CTRL_MASK));
 
-
</source>
 
-
 
-
An accelerator is a key shortcut that launches a menu item. In our case, by pressing <b>Ctrl + W</b> we close the
 
-
application.
 
-
 
-
 
-
<source lang="cpp">
 
-
file.addSeparator();
 
-
</source>
 
-
 
-
A separator is a vertical line that visually separates the menu items. This way we can group items into some
 
-
logical places.
 
-
 
-
[[image: java_swing_submenu.jpg | center]]
 
-
 
-
== JCheckBoxMenuItem ==
 
-
A menu item that can be selected or deselected. If selected, the menu item typically appears with a checkmark next to it. If unselected or deselected, the menu item appears without a checkmark. Like a regular menu item, a check box menu item can have either text or a graphic icon associated with it, or both.
 
-
 
-
<source lang="cpp">
 
-
import java.awt.BorderLayout;
 
-
import java.awt.event.ActionEvent;
 
-
import java.awt.event.ActionListener;
 
-
import java.awt.event.KeyEvent;
 
-
 
-
import javax.swing.BorderFactory;
 
-
import javax.swing.JCheckBoxMenuItem;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JLabel;
 
-
import javax.swing.JMenu;
 
-
import javax.swing.JMenuBar;
 
-
import javax.swing.UIManager;
 
-
import javax.swing.border.EtchedBorder;
 
-
 
-
 
-
public class CheckMenuItem extends JFrame {
 
-
 
-
 
-
    private JLabel statusbar;
 
-
 
-
    public CheckMenuItem() {
 
-
 
-
        setTitle("CheckBoxMenuItem");
 
-
 
-
        JMenuBar menubar = new JMenuBar();
 
-
        JMenu file = new JMenu("File");
 
-
        file.setMnemonic(KeyEvent.VK_F);
 
-
 
-
        JMenu view = new JMenu("View");
 
-
        view.setMnemonic(KeyEvent.VK_V);
 
-
 
-
        JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar");
 
-
        sbar.setState(true);
 
-
 
-
        sbar.addActionListener(new ActionListener() {
 
-
            public void actionPerformed(ActionEvent event) {
 
-
                if (statusbar.isVisible()) {
 
-
                    statusbar.setVisible(false);
 
-
                } else {
 
-
                    statusbar.setVisible(true);
 
-
                }
 
-
            }
 
-
 
-
        });
 
-
 
-
        view.add(sbar);
 
-
 
-
        menubar.add(file);
 
-
        menubar.add(view);
 
-
 
-
        setJMenuBar(menubar);
 
-
 
-
        statusbar = new JLabel(" Statusbar");
 
-
        statusbar.setBorder(BorderFactory.createEtchedBorder(
 
-
EtchedBorder.RAISED));
 
-
        add(statusbar, BorderLayout.SOUTH);
 
-
 
-
 
-
        setSize(360, 250);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new CheckMenuItem();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
The example shows a <b>JCheckBoxMenuItem.</b>. By selecting the menu item, we toggle the visibility of
 
-
the statusbar.
 
-
 
-
<source lang="cpp">
 
-
JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar");
 
-
sbar.setState(true);
 
-
</source>
 
-
 
-
We create the  <b>JCheckBoxMenuItem</b> and check it by default. The statusbar is initially visible.
 
-
 
-
<source lang="cpp">
 
-
if (statusbar.isVisible()) {
 
-
statusbar.setVisible(false);
 
-
} else {
 
-
statusbar.setVisible(true);
 
-
}
 
-
</source>
 
-
 
-
Here we toggle the visibility of the statusbar.
 
-
 
-
<source lang="cpp">
 
-
statusbar = new JLabel(" Statusbar");
 
-
statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
 
-
</source>
 
-
 
-
The statusbar is a simple <b>JLabel</b> component. We put a raised <b>EtchedBorder</b> around the label, so that it is discernible.
 
-
 
-
[[image: java_swing_checkboxmenuitem.jpg | center]]
 
-
 
-
== A popup menu ==
 
-
Another type of a menu is a popup menu. It is sometimes called a context menu. It is usually shown, when we right click on a component. The idea is to provide only the commands, that are relevant to the current context. Say we have an image. By right clicking on the image, we get a window with commands to save, rescale, move etc the image.
 
-
 
-
 
-
<source lang="cpp">
 
-
import java.awt.Toolkit;
 
-
 
-
import javax.swing.*;
 
-
import java.awt.event.*;
 
-
 
-
public class PopupMenu {
 
-
 
-
    private JPopupMenu menu;
 
-
    private Toolkit toolkit;
 
-
 
-
  public PopupMenu(){
 
-
 
-
 
-
    JFrame frame = new JFrame("JPopupMenu");
 
-
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
 
-
    toolkit = frame.getToolkit();
 
-
 
-
    menu = new JPopupMenu();
 
-
    JMenuItem menuItemBeep = new JMenuItem("Beep");
 
-
 
-
    menuItemBeep.addActionListener(new ActionListener() {
 
-
        public void actionPerformed(ActionEvent e) {
 
-
            toolkit.beep();
 
-
        }
 
-
    });
 
-
 
-
    menu.add(menuItemBeep);
 
-
 
-
    JMenuItem menuItemClose = new JMenuItem("Close");
 
-
    menuItemClose.addActionListener(new ActionListener() {
 
-
        public void actionPerformed(ActionEvent e) {
 
-
            System.exit(0);
 
-
        }
 
-
 
-
    });
 
-
 
-
    menu.add(menuItemClose);
 
-
 
-
    frame.addMouseListener(new MouseAdapter() {
 
-
        public void mouseReleased(MouseEvent e) {
 
-
            if (e.getButton() == e.BUTTON3) {
 
-
                menu.show(e.getComponent(), e.getX(), e.getY());
 
-
            }
 
-
        }
 
-
    });
 
-
 
-
    frame.setSize(250, 200);
 
-
    frame.setLocationRelativeTo(null);
 
-
    frame.setVisible(true);
 
-
  }
 
-
 
-
 
-
 
-
  public static void main(String[] args) {
 
-
        new PopupMenu();
 
-
  }
 
-
}
 
-
</source>
 
-
 
-
Our example shows a demonstrational popup menu with two commands. The first option of the popup menu will beep a sound, the second one will close the window.
 
-
 
-
In our example, we create a submenu, menu separators and create an accelerator key.
 
-
 
-
<source lang="cpp">
 
-
menu = new JPopupMenu();
 
-
</source>
 
-
 
-
To create a popup menu, we have a class called <b>JPopupMenu</b>.
 
-
 
-
<source lang="cpp">
 
-
JMenuItem menuItemBeep = new JMenuItem("Beep");
 
-
</source>
 
-
 
-
The menu item is the same, as with the standard <b>JMenu</b>
 
-
 
-
<source lang="cpp">
 
-
frame.addMouseListener(new MouseAdapter() {
 
-
    public void mouseReleased(MouseEvent e) {
 
-
        if (e.getButton() == e.BUTTON3) {
 
-
            menu.show(e.getComponent(), e.getX(), e.getY());
 
-
        }
 
-
    }
 
-
});
 
-
</source>
 
-
 
-
The popup menu is shown, where we clicked with the mouse button. The <b>BUTTON3</b> constant is here to enable
 
-
the popup menu only for the mouse right click.
 
-
 
-
[[image: java_swing_popupmenu.jpg | center]]
 
-
 
-
== JToolbar ==
 
-
Menus group commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands. In Java Swing, the <b>JToolBar</b> class creates a toolbar in an application.
 
-
 
-
<source lang="cpp">
 
-
import java.awt.BorderLayout;
 
-
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.JMenu;
 
-
import javax.swing.JMenuBar;
 
-
import javax.swing.JToolBar;
 
-
 
-
 
-
public class SimpleToolbar extends JFrame {
 
-
 
-
 
-
    public SimpleToolbar() {
 
-
 
-
        setTitle("SimpleToolbar");
 
-
 
-
        JMenuBar menubar = new JMenuBar();
 
-
        JMenu file = new JMenu("File");
 
-
        menubar.add(file);
 
-
        setJMenuBar(menubar);
 
-
 
-
        JToolBar toolbar = new JToolBar();
 
-
 
-
        ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
 
-
 
-
        JButton exit = new JButton(icon);
 
-
        toolbar.add(exit);
 
-
        exit.addActionListener(new ActionListener() {
 
-
            public void actionPerformed(ActionEvent event) {
 
-
                System.exit(0);
 
-
            }
 
-
 
-
        });
 
-
 
-
 
-
        add(toolbar, BorderLayout.NORTH);
 
-
 
-
        setSize(300, 200);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new SimpleToolbar();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
 
-
The example creates a toolbar with one exit button.
 
-
 
-
<source lang="cpp">
 
-
JToolBar toolbar = new JToolBar();
 
-
</source>
 
-
 
-
This is the <b>JToolBar</b> constructor.
 
-
 
-
<source lang="cpp">
 
-
JButton exit = new JButton(icon);
 
-
toolbar.add(exit);
 
-
 
-
</source>
 
-
 
-
We create a button and add it to the toolbar.
 
-
 
-
[[image: java_swing_simpletoolbar.jpg | center]]
 
-
 
-
=== Toolbars ===
 
-
 
-
Say, we wanted to create two toolbars. The next example shows, how we could do it.
 
-
 
-
 
-
<source lang="cpp">
 
-
import java.awt.BorderLayout;
 
-
import java.awt.event.ActionEvent;
 
-
import java.awt.event.ActionListener;
 
-
 
-
import javax.swing.BoxLayout;
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JPanel;
 
-
import javax.swing.JToolBar;
 
-
 
-
 
-
public class Toolbars extends JFrame {
 
-
 
-
    public Toolbars() {
 
-
 
-
        setTitle("Toolbars");
 
-
 
-
        JToolBar toolbar1 = new JToolBar();
 
-
        JToolBar toolbar2 = new JToolBar();
 
-
 
-
        JPanel panel = new JPanel();
 
-
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 
-
 
-
        ImageIcon newi = new ImageIcon(
 
-
getClass().getResource("new.png"));
 
-
        ImageIcon open = new ImageIcon(
 
-
getClass().getResource("open.png"));
 
-
        ImageIcon save = new ImageIcon(
 
-
getClass().getResource("save.png"));
 
-
        ImageIcon exit = new ImageIcon(
 
-
getClass().getResource("exit.png"));
 
-
 
-
        JButton newb = new JButton(newi);
 
-
        JButton openb = new JButton(open);
 
-
        JButton saveb = new JButton(save);
 
-
 
-
        toolbar1.add(newb);
 
-
        toolbar1.add(openb);
 
-
        toolbar1.add(saveb);
 
-
        toolbar1.setAlignmentX(0);
 
-
 
-
        JButton exitb = new JButton(exit);
 
-
        toolbar2.add(exitb);
 
-
        toolbar2.setAlignmentX(0);
 
-
 
-
        exitb.addActionListener(new ActionListener() {
 
-
            public void actionPerformed(ActionEvent event) {
 
-
                System.exit(0);
 
-
            }
 
-
 
-
        });
 
-
 
-
        panel.add(toolbar1);
 
-
        panel.add(toolbar2);
 
-
 
-
        add(panel, BorderLayout.NORTH);
 
-
 
-
        setSize(300, 200);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new Toolbars();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
We show only one way, how we could create toolbars. Of course, there are several possibilities.
 
-
We put a <b>JPanel</b> to the north of the <b>BorderLayout</b> manager. The panel has a vertical
 
-
<b>BoxLayout</b>. We add the two toolbars into this panel.
 
-
 
-
<source lang="cpp">
 
-
JToolBar toolbar1 = new JToolBar();
 
-
JToolBar toolbar2 = new JToolBar();
 
-
</source>
 
-
 
-
Creation of two toolbars.
 
-
 
-
<source lang="cpp">
 
-
JPanel panel = new JPanel();
 
-
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 
-
</source>
 
-
 
-
The panel has a vertical <b>BoxLayout</b>.
 
-
 
-
 
-
<source lang="cpp">
 
-
toolbar1.setAlignmentX(0);
 
-
</source>
 
-
 
-
The toolbar is left aligned.
 
-
 
-
 
-
<source lang="cpp">
 
-
panel.add(toolbar1);
 
-
panel.add(toolbar2);
 
-
 
-
add(panel, BorderLayout.NORTH);
 
-
</source>
 
-
 
-
We add the toolbars to the panel. Finally, the panel is located into the north part of the frame.
 
-
 
-
[[image: java_swing_toolbars.jpg | center]]
 
-
 
-
=== A vertical toobar ===
 
-
 
-
The following example shows a vertical toobar.
 
-
 
-
 
-
<source lang="cpp">
 
-
import java.awt.BorderLayout;
 
-
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JToolBar;
 
-
import javax.swing.UIManager;
 
-
 
-
 
-
public class VerticalToolbar extends JFrame {
 
-
 
-
 
-
    public VerticalToolbar() {
 
-
 
-
        setTitle("Vertical toolbar");
 
-
 
-
        JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
 
-
 
-
        ImageIcon select = new ImageIcon(
 
-
getClass().getResource("select.gif"));
 
-
        ImageIcon freehand = new ImageIcon(
 
-
getClass().getResource("freehand.gif"));
 
-
        ImageIcon shapeed = new ImageIcon(
 
-
getClass().getResource("shapeed.gif"));
 
-
        ImageIcon pen = new ImageIcon(
 
-
getClass().getResource("pen.gif"));
 
-
        ImageIcon rectangle = new ImageIcon(
 
-
getClass().getResource("rectangle.gif"));
 
-
        ImageIcon ellipse = new ImageIcon(
 
-
getClass().getResource("ellipse.gif"));
 
-
        ImageIcon qs = new ImageIcon(
 
-
getClass().getResource("qs.gif"));
 
-
        ImageIcon text = new ImageIcon(
 
-
getClass().getResource("text.gif"));
 
-
 
-
        JButton selectb = new JButton(select);
 
-
        JButton freehandb = new JButton(freehand);
 
-
        JButton shapeedb = new JButton(shapeed);
 
-
        JButton penb = new JButton(pen);
 
-
        JButton rectangleb = new JButton(rectangle);
 
-
        JButton ellipseb = new JButton(ellipse);
 
-
        JButton qsb = new JButton(qs);
 
-
        JButton textb = new JButton(text);
 
-
 
-
        toolbar.add(selectb);
 
-
        toolbar.add(freehandb);
 
-
        toolbar.add(shapeedb);
 
-
        toolbar.add(penb);
 
-
        toolbar.add(rectangleb);
 
-
        toolbar.add(ellipseb);
 
-
        toolbar.add(qsb);
 
-
        toolbar.add(textb);
 
-
 
-
        add(toolbar, BorderLayout.WEST);
 
-
 
-
        setSize(250, 350);
 
-
        setLocationRelativeTo(null);
 
-
        setDefaultCloseOperation(EXIT_ON_CLOSE);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        try {
 
-
            UIManager.setLookAndFeel(
 
-
                UIManager.getSystemLookAndFeelClassName());
 
-
        }
 
-
        catch (Exception e) {
 
-
            System.out.println("Error:" + e.getStackTrace());
 
-
        }
 
-
        new VerticalToolbar();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In the example, we put a vertical toolbar to the left side of the window. This is typical for a graphics applications like <b>Xara Extreme</b> or <b>Inkscape</b>. In our example, we use icons from Xara Extreme application.
 
-
 
-
<source lang="cpp">
 
-
JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
 
-
 
-
</source>
 
-
 
-
We create a vertical toolbar.
 
-
 
-
<source lang="cpp">
 
-
add(toolbar, BorderLayout.WEST);
 
-
</source>
 
-
 
-
The toolbar is placed into the left part of the window.
 
-
 
-
<source lang="cpp">
 
-
UIManager.setLookAndFeel(
 
-
    UIManager.getSystemLookAndFeelClassName());
 
-
 
-
</source>
 
-
 
-
I used the system look and feel. In my case, it was the gtk theme. The icons are not transparent, and they
 
-
would not look ok on a different theme.
 
-
 
-
[[image: java_swing_verticaltoolbar.jpg | center]]
 
-
 
-
[[Категория:Java]]
 

Текущая версия на 11:26, 7 апреля 2009