Java by API/java.awt/Component
Содержание
- 1 Component.BOTTOM_ALIGNMENT
- 2 Component.CENTER_ALIGNMENT
- 3 Component: enableEvents(long eventsToEnable)
- 4 Component: getAccessibleContext()
- 5 Component: getLocationOnScreen()
- 6 Component: isFocusable()
- 7 Component: isFocusCycleRoot
- 8 Component.LEFT_ALIGNMENT
- 9 Component: requestFocusInWindow()
- 10 Component.RIGHT_ALIGNMENT
- 11 Component: setBounds(int x,int y,int width,int height)
- 12 Component: setComponentOrientation(ComponentOrientation o)
- 13 Component: setLocation(int x, int y)
- 14 Component: setSize(int width, int height)
- 15 Component.TOP_ALIGNMENT
Component.BOTTOM_ALIGNMENT
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS); container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); button.setAlignmentX(Component.BOTTOM_ALIGNMENT); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
Component.CENTER_ALIGNMENT
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
Component: enableEvents(long eventsToEnable)
<source lang="java">
// This example is from the book _Java AWT Reference_ by John Zukowski. // Written by John Zukowski. Copyright (c) 1997 O"Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or import java.awt.AWTEvent; import java.awt.AWTEventMulticaster; import java.awt.ruponent; import java.awt.Frame; import java.awt.ItemSelectable; import java.awt.SystemColor; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; class ItemEventComponent extends Component implements ItemSelectable {
boolean selected; int i = 0; ItemListener itemListener = null; ItemEventComponent() { enableEvents(AWTEvent.MOUSE_EVENT_MASK); } public Object[] getSelectedObjects() { Object o[] = new Object[1]; o[0] = new Integer(i); return o; } public void addItemListener(ItemListener l) { itemListener = AWTEventMulticaster.add(itemListener, l); } public void removeItemListener(ItemListener l) { itemListener = AWTEventMulticaster.remove(itemListener, l); } public void processEvent(AWTEvent e) { if (e.getID() == MouseEvent.MOUSE_PRESSED) { if (itemListener != null) { selected = !selected; i++; itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED))); } } }
} public class MainClass extends Frame implements ItemListener {
MainClass() { super("Listening In"); ItemEventComponent c = new ItemEventComponent(); add(c, "Center"); c.addItemListener(this); c.setBackground(SystemColor.control); setSize(200, 200); } public void itemStateChanged(ItemEvent e) { Object[] o = e.getItemSelectable().getSelectedObjects(); Integer i = (Integer) o[0]; System.out.println(i); } public static void main(String args[]) { MainClass f = new MainClass(); f.show(); }
}
</source>
Component: getAccessibleContext()
<source lang="java">
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.accessibility.AccessibleContext; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass extends JFrame implements ActionListener {
JButton jb; MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); AccessibleContext ac = getAccessibleContext(); ac.setAccessibleDescription("Accessibility Demo1 description."); ac = getRootPane().getAccessibleContext(); ac.setAccessibleName("Root pane"); ac.setAccessibleDescription("Root pane description"); ac = getGlassPane().getAccessibleContext(); ac.setAccessibleName("Glass pane"); ac.setAccessibleDescription("Glass pane description"); ac = getLayeredPane().getAccessibleContext(); ac.setAccessibleName("Layered pane"); ac.setAccessibleDescription("Layered pane description"); ac = getContentPane().getAccessibleContext(); ac.setAccessibleName("Content pane"); ac.setAccessibleDescription("Content pane description"); JPanel p = new JPanel(); ac = p.getAccessibleContext(); ac.setAccessibleName("Panel"); ac.setAccessibleDescription("Panel description"); jb = new JButton("Press Me"); jb.addActionListener(this); jb.setToolTipText("Press me for accessibility information."); p.add(jb); getContentPane().add(p); setSize(200, 75); setVisible(true); } public void actionPerformed(ActionEvent e) { dumpInfo(getAccessibleContext()); } void dumpInfo(AccessibleContext ac) { System.out.println("Name = " + ac.getAccessibleName()); System.out.println("Description = " + ac.getAccessibleDescription()); int nChildren = ac.getAccessibleChildrenCount(); for (int i = 0; i < nChildren; i++) dumpInfo(ac.getAccessibleChild(i).getAccessibleContext()); } public static void main(String[] args) { new MainClass("Accessibility Demo1"); }
}
</source>
Component: getLocationOnScreen()
<source lang="java">
import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel {
public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setPreferredSize(new Dimension(300, 280)); Main ch = new Main(); frame.getContentPane().add(ch); frame.setUndecorated(true); MoveMouseListener mml = new MoveMouseListener(ch); ch.addMouseListener(mml); ch.addMouseMotionListener(mml); frame.pack(); frame.setVisible(true); }
} class MoveMouseListener implements MouseListener, MouseMotionListener {
JComponent target; Point start_drag; Point start_loc; public MoveMouseListener(JComponent target) { this.target = target; } public static JFrame getFrame(Container target) { if (target instanceof JFrame) { return (JFrame) target; } return getFrame(target.getParent()); } Point getScreenLocation(MouseEvent e) { Point cursor = e.getPoint(); Point target_location = this.target.getLocationOnScreen(); return new Point((int) (target_location.getX() + cursor.getX()), (int) (target_location.getY() + cursor.getY())); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { this.start_drag = this.getScreenLocation(e); this.start_loc = this.getFrame(this.target).getLocation(); } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { Point current = this.getScreenLocation(e); Point offset = new Point((int) current.getX() - (int) start_drag.getX(), (int) current.getY() - (int) start_drag.getY()); JFrame frame = this.getFrame(target); Point new_location = new Point( (int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc .getY() + offset.getY())); frame.setLocation(new_location); } public void mouseMoved(MouseEvent e) { }
}
</source>
Component: isFocusable()
<source lang="java">
import java.awt.AWTKeyStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.KeyboardFocusManager; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JPanel; public class MainClass extends JFrame {
public MainClass() { super("Focus Example"); setDefaultCloseOperation(EXIT_ON_CLOSE); MyPanel mypanel = new MyPanel(); JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); JButton button4 = new JButton("Four"); JButton button5 = new MyButton("Five*"); JButton button6 = new MyButton("Six*"); JButton button7 = new JButton("Seven"); mypanel.add(button2); mypanel.add(button3); JInternalFrame frame1 = new JInternalFrame("Internal Frame 1", true, true, true, true); frame1.setBackground(Color.lightGray); frame1.getContentPane().setLayout(new GridLayout(2, 3)); frame1.setSize(300, 200); frame1.getContentPane().add(button1); frame1.getContentPane().add(mypanel); frame1.getContentPane().add(button4); frame1.getContentPane().add(button5); frame1.getContentPane().add(button6); frame1.getContentPane().add(button7); JDesktopPane desktop = new JDesktopPane(); desktop.add(frame1, new Integer(1)); desktop.setOpaque(true); // Now set up the user interface window. Container contentPane = getContentPane(); contentPane.add(desktop, BorderLayout.CENTER); setSize(new Dimension(400, 300)); frame1.setVisible(true); setVisible(true); } public static void main(String[] args) { new MainClass(); }
} class MyButton extends JButton {
public MyButton(String s) { super(s); } public boolean isFocusable() { return false; }
} class MyPanel extends JPanel {
public MyPanel() { super(true); java.util.Set upKeys = new java.util.HashSet(1); upKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_UP, 0)); setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, upKeys); } public boolean isFocusCycleRoot() { return true; }
}
</source>
Component: isFocusCycleRoot
<source lang="java">
import java.awt.AWTKeyStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.KeyboardFocusManager; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JPanel; public class MainClass extends JFrame {
public MainClass() { super("Focus Example"); setDefaultCloseOperation(EXIT_ON_CLOSE); MyPanel mypanel = new MyPanel(); JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); JButton button4 = new JButton("Four"); JButton button5 = new MyButton("Five*"); JButton button6 = new MyButton("Six*"); JButton button7 = new JButton("Seven"); mypanel.add(button2); mypanel.add(button3); JInternalFrame frame1 = new JInternalFrame("Internal Frame 1", true, true, true, true); frame1.setBackground(Color.lightGray); frame1.getContentPane().setLayout(new GridLayout(2, 3)); frame1.setSize(300, 200); frame1.getContentPane().add(button1); frame1.getContentPane().add(mypanel); frame1.getContentPane().add(button4); frame1.getContentPane().add(button5); frame1.getContentPane().add(button6); frame1.getContentPane().add(button7); JDesktopPane desktop = new JDesktopPane(); desktop.add(frame1, new Integer(1)); desktop.setOpaque(true); // Now set up the user interface window. Container contentPane = getContentPane(); contentPane.add(desktop, BorderLayout.CENTER); setSize(new Dimension(400, 300)); frame1.setVisible(true); setVisible(true); } public static void main(String[] args) { new MainClass(); }
} class MyButton extends JButton {
public MyButton(String s) { super(s); } public boolean isFocusable() { return false; }
} class MyPanel extends JPanel {
public MyPanel() { super(true); java.util.Set upKeys = new java.util.HashSet(1); upKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_UP, 0)); setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, upKeys); } public boolean isFocusCycleRoot() { return true; }
}
</source>
Component.LEFT_ALIGNMENT
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); button.setAlignmentX(Component.LEFT_ALIGNMENT); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
Component: requestFocusInWindow()
<source lang="java">
import java.awt.ruponent; import java.awt.GridLayout; import java.awt.KeyboardFocusManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; public class MainClass {
public static void main(String args[]) { JFrame frame = new JFrame("Focus Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new ActionFocusMover(); MouseListener mouseListener = new MouseEnterFocusMover(); frame.setLayout(new GridLayout(3, 3)); for (int i = 1; i < 10; i++) { JButton button = new JButton(Integer.toString(i)); button.addActionListener(actionListener); button.addMouseListener(mouseListener); if ((i % 2) != 0) { button.setFocusable(false); } frame.add(button); } frame.setSize(300, 200); frame.setVisible(true); }
} class ActionFocusMover implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) { KeyboardFocusManager manager = KeyboardFocusManager .getCurrentKeyboardFocusManager(); manager.focusNextComponent(); }
} class MouseEnterFocusMover extends MouseAdapter {
public void mouseEntered(MouseEvent mouseEvent) { Component component = mouseEvent.getComponent(); if (!component.hasFocus()) { component.requestFocusInWindow(); } }
}
</source>
Component.RIGHT_ALIGNMENT
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); button.setAlignmentX(Component.RIGHT_ALIGNMENT); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
Component: setBounds(int x,int y,int width,int height)
<source lang="java">
import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class MainClass extends JPanel {
public MainClass() { JRadioButton radMarriedYes = new JRadioButton("Yes?", true); JRadioButton radMarriedNo = new JRadioButton("No?", false); JRadioButton radGolfYes = new JRadioButton("Yes?", false); JRadioButton radGolfNo = new JRadioButton("No?", true); ButtonGroup radioGroup1 = new ButtonGroup(); ButtonGroup radioGroup2 = new ButtonGroup(); setLayout(null); add(radMarriedYes); add(radMarriedNo); add(radGolfYes); add(radGolfNo); radioGroup1.add(radMarriedYes); radioGroup1.add(radMarriedNo); radioGroup2.add(radGolfYes); radioGroup2.add(radGolfNo); radMarriedYes.setBounds(30, 50, 50, 20); radMarriedNo.setBounds(30, 80, 50, 20); radGolfYes.setBounds(150, 50, 50, 20); radGolfNo.setBounds(150, 80, 50, 20); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
Component: setComponentOrientation(ComponentOrientation o)
<source lang="java">
import java.awt.ruponent; import java.awt.ruponentOrientation; import java.awt.Container; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JMenu; public class Main extends JFrame {
public Main() { JComboBox itemsComboBox = new JComboBox(new String[]{ "A", "L", "M" }); itemsComboBox.setEditable(true); itemsComboBox.setMaximumRowCount(3); this.getContentPane().add(itemsComboBox); itemsComboBox.setVisible(true); applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT); this.validate(); this.repaint(); } public static void main(String[] args) { JFrame frame = new Main(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } private void applyOrientation(Component c, ComponentOrientation o) { c.setComponentOrientation(o); if (c instanceof JMenu) { JMenu menu = (JMenu) c; int ncomponents = menu.getMenuComponentCount(); for (int i = 0; i < ncomponents; ++i) { applyOrientation(menu.getMenuComponent(i), o); } } else if (c instanceof Container) { Container container = (Container) c; int ncomponents = container.getComponentCount(); for (int i = 0; i < ncomponents; ++i) { applyOrientation(container.getComponent(i), o); } } }
}
</source>
Component: setLocation(int x, int y)
<source lang="java">
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; /**
* Internal Frames Demo * * @version $Id: JIFrameDemo.java,v 1.4 2003/07/15 01:46:47 ian Exp $ */
public class Main {
/* Main View */ public static void main(String[] a) { final JFrame jf = new JFrame("JIFrameDemo Main Window"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenSize.width -= 42; screenSize.height -= 42; jf.setSize(screenSize); jf.setLocation(20, 20); JMenuBar mb = new JMenuBar(); jf.setJMenuBar(mb); JMenu fm = new JMenu("File"); mb.add(fm); JMenuItem mi; fm.add(mi = new JMenuItem("Exit")); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); JDesktopPane dtp = new JDesktopPane(); //dtp.setBackground(Color.GREEN); jf.setContentPane(dtp); JInternalFrame mboxFrame = new JInternalFrame("Mail Reader", true, true, true, true); JLabel reader = new JLabel("Mail Reader Would Be Here"); mboxFrame.setContentPane(reader); mboxFrame.setSize(400, 300); mboxFrame.setLocation(50, 50); mboxFrame.setVisible(true); dtp.add(mboxFrame); JInternalFrame compFrame = new JInternalFrame("Compose Mail", true, true, true, true); JLabel composer = new JLabel("Mail Compose Would Be Here"); compFrame.setContentPane(composer); compFrame.setSize(300, 200); compFrame.setLocation(200, 200); compFrame.setVisible(true); dtp.add(compFrame); JInternalFrame listFrame = new JInternalFrame("Users", true, true, true, true); JLabel list = new JLabel("List of Users Would Be Here"); listFrame.setContentPane(list); listFrame.setLocation(400, 400); listFrame.setSize(500, 200); listFrame.setVisible(true); dtp.add(listFrame); jf.setVisible(true); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.setVisible(false); jf.dispose(); System.exit(0); } }); }
}
</source>
Component: setSize(int width, int height)
<source lang="java">
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; /**
* Internal Frames Demo * * @version $Id: JIFrameDemo.java,v 1.4 2003/07/15 01:46:47 ian Exp $ */
public class Main {
/* Main View */ public static void main(String[] a) { final JFrame jf = new JFrame("JIFrameDemo Main Window"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); screenSize.width -= 42; screenSize.height -= 42; jf.setSize(screenSize); jf.setLocation(20, 20); JMenuBar mb = new JMenuBar(); jf.setJMenuBar(mb); JMenu fm = new JMenu("File"); mb.add(fm); JMenuItem mi; fm.add(mi = new JMenuItem("Exit")); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); JDesktopPane dtp = new JDesktopPane(); //dtp.setBackground(Color.GREEN); jf.setContentPane(dtp); JInternalFrame mboxFrame = new JInternalFrame("Mail Reader", true, true, true, true); JLabel reader = new JLabel("Mail Reader Would Be Here"); mboxFrame.setContentPane(reader); mboxFrame.setSize(400, 300); mboxFrame.setLocation(50, 50); mboxFrame.setVisible(true); dtp.add(mboxFrame); JInternalFrame compFrame = new JInternalFrame("Compose Mail", true, true, true, true); JLabel composer = new JLabel("Mail Compose Would Be Here"); compFrame.setContentPane(composer); compFrame.setSize(300, 200); compFrame.setLocation(200, 200); compFrame.setVisible(true); dtp.add(compFrame); JInternalFrame listFrame = new JInternalFrame("Users", true, true, true, true); JLabel list = new JLabel("List of Users Would Be Here"); listFrame.setContentPane(list); listFrame.setLocation(400, 400); listFrame.setSize(500, 200); listFrame.setVisible(true); dtp.add(listFrame); jf.setVisible(true); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.setVisible(false); jf.dispose(); System.exit(0); } }); }
}
</source>
Component.TOP_ALIGNMENT
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS); container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); button.setAlignmentX(Component.TOP_ALIGNMENT); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>