Java by API/javax.swing/JToolBar — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
JToolBar: add(JComponent com)
import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToolBar Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
f.add(toolbar, BorderLayout.NORTH);
f.setSize(300, 100);
f.setVisible(true);
}
}
JToolBar: addPropertyChangeListener(PropertyChangeListener listener)
import javax.swing.JToolBar;
public class Main {
public static void main(String[] argv) throws Exception {
JToolBar toolbar = new JToolBar();
toolbar.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if ("orientation".equals(propName)) {
Integer oldValue = (Integer) evt.getOldValue();
Integer newValue = (Integer) evt.getNewValue();
if (newValue.intValue() == JToolBar.HORIZONTAL) {
System.out.println("horizontal orientation");
} else {
System.out.println("vertical orientation");
}
}
}
});
}
}
JToolBar: addSeparator()
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;
public class MainClass {
public static void main(String[] a) {
final int STRING_POSITION = 1;
Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" },
{ Color.GREEN, "GREEN" }, { Color.BLACK, "BLACK" }, null, // separator
{ Color.CYAN, "CYAN" } };
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new TheActionListener();
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
for (Object[] color : buttonColors) {
if (color == null) {
toolbar.addSeparator();
} else {
Icon icon = MetalIconFactory.getTreeComputerIcon();
JButton button = new JButton(icon);
button.setActionCommand((String) color[STRING_POSITION]);
button.addActionListener(actionListener);
toolbar.add(button);
}
}
Action action = new ShowAction(toolbar);
JButton button = new JButton(action);
toolbar.add(button);
Container contentPane = frame.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(350, 150);
frame.setVisible(true);
}
public static class TheActionListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
}
class ShowAction extends AbstractAction {
Component parentComponent;
public ShowAction(Component parentComponent) {
super("About");
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
putValue(Action.SMALL_ICON, MetalIconFactory.getFileChooserHomeFolderIcon());
this.parentComponent = parentComponent;
}
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(parentComponent, "About Swing",
"About Box V2.0", JOptionPane.INFORMATION_MESSAGE);
}
}
JToolBar: getOrientation()
import javax.swing.JToolBar;
public class Main {
public static void main(String[] argv) throws Exception {
JToolBar toolbar = new JToolBar(null, JToolBar.VERTICAL);
// Get current orientation
int orient = toolbar.getOrientation();
}
}
JToolBar: isFloatable()
import javax.swing.JToolBar;
public class Main {
public static void main(String[] argv) throws Exception {
// Create a horizontal toolbar
JToolBar toolbar = new JToolBar();
// Get current floatability
boolean b = toolbar.isFloatable();
// Disable floatability
toolbar.setFloatable(false);
}
}
JToolBar: setFloatable(boolean b)
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolBar = new JToolBar("Still draggable");
toolBar.setFloatable(false);
toolBar.setRollover(true);
toolBar.add(new JButton("New"));
toolBar.addSeparator();
toolBar.add(new JButton("Open"));
frame.add(toolBar, "North");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JToolBar: setRollover(boolean v)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;
public class MainClass {
public static void main(String[] a) {
final int STRING_POSITION = 1;
Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" },
{ Color.GREEN, "GREEN" }, { Color.BLACK, "BLACK" }, null, // separator
{ Color.CYAN, "CYAN" } };
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new TheActionListener();
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
for (Object[] color : buttonColors) {
if (color == null) {
toolbar.addSeparator();
} else {
Icon icon = MetalIconFactory.getTreeComputerIcon();
JButton button = new JButton(icon);
button.setActionCommand((String) color[STRING_POSITION]);
button.addActionListener(actionListener);
toolbar.add(button);
}
}
Action action = new ShowAction(toolbar);
JButton button = new JButton(action);
toolbar.add(button);
Container contentPane = frame.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(350, 150);
frame.setVisible(true);
}
public static class TheActionListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
}
class ShowAction extends AbstractAction {
Component parentComponent;
public ShowAction(Component parentComponent) {
super("About");
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
putValue(Action.SMALL_ICON, MetalIconFactory.getFileChooserHomeFolderIcon());
this.parentComponent = parentComponent;
}
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(parentComponent, "About Swing",
"About Box V2.0", JOptionPane.INFORMATION_MESSAGE);
}
}