Java by API/javax.swing/SwingUtilities — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:21, 31 мая 2010
Содержание
- 1 SwingUtilities: convertPointFromScreen(Point p, Component c)
- 2 SwingUtilities: convertPointToScreen(Point p, Component c)
- 3 SwingUtilities: getRoot(Component c)
- 4 SwingUtilities: getWindowAncestor(Component c)
- 5 SwingUtilities: invokeAndWait(Runnable run)
- 6 SwingUtilities.invokeLater(Runnable doRun)
- 7 SwingUtilities.isEventDispatchThread()
- 8 SwingUtilities.isLeftMouseButton(MouseEvent mouseEvent)
- 9 SwingUtilities.isMiddleMouseButton(MouseEvent mouseEvent)
- 10 SwingUtilities.isRightMouseButton(MouseEvent mouseEvent)
- 11 SwingUtilities: updateComponentTreeUI(Component c)
SwingUtilities: convertPointFromScreen(Point p, Component c)
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] argv) {
JButton component = new JButton();
Point pt = new Point(component.getLocation());
SwingUtilities.convertPointFromScreen(pt, component);
}
}
SwingUtilities: convertPointToScreen(Point p, Component c)
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] argv) {
JButton component = new JButton();
Point pt = new Point(component.getLocation());
SwingUtilities.convertPointToScreen(pt, component);
}
}
SwingUtilities: getRoot(Component c)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public Main() {
this.setSize(400, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Change Frame Color");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
JFrame frame = (JFrame) SwingUtilities.getRoot(component);
frame.setBackground(Color.RED);
}
});
this.getContentPane().add(button);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
SwingUtilities: getWindowAncestor(Component c)
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] a) {
JPanel panel = new JPanel();
JDialog dialog = new JDialog((JFrame) SwingUtilities.getWindowAncestor(panel), true);
}
}
SwingUtilities: invokeAndWait(Runnable run)
import java.awt.ruponent;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainClass extends JPanel {
public MainClass() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel jl = new JLabel(new MyIcon(), JLabel.CENTER);
add(jl);
}
});
} catch (Exception exc) {
System.out.println("Can"t create because of " + exc);
}
}
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);
}
}
SwingUtilities.invokeLater(Runnable doRun)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class InvokeExample {
private static JButton good = new JButton("Good");
private static JButton bad = new JButton("Bad");
private static JButton bad2 = new JButton("Bad2");
private static JLabel resultLabel = new JLabel("Ready", JLabel.CENTER);
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(good);
p.add(bad);
p.add(bad2);
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
c.add(p, BorderLayout.CENTER);
c.add(resultLabel, BorderLayout.SOUTH);
good.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
resultLabel.setText("Working . . .");
setEnabled(false);
Thread worker = new Thread() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
resultLabel.setText("Ready");
setEnabled(true);
}
});
}
};
worker.start();
}
});
bad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
resultLabel.setText("Working . . .");
setEnabled(false);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
resultLabel.setText("Ready");
setEnabled(true);
}
});
bad2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
resultLabel.setText("Working . . . ");
setEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.sleep(5000); // Dispatch thread is starving!
} catch (InterruptedException ex) {
}
resultLabel.setText("Ready");
setEnabled(true);
}
});
}
});
f.setSize(300, 100);
f.setVisible(true);
}
// Allows us to turn the buttons on or off while we work.
static void setEnabled(boolean b) {
good.setEnabled(b);
bad.setEnabled(b);
bad2.setEnabled(b);
}
}
SwingUtilities.isEventDispatchThread()
/*
* Copyright (c) 2007, Romain Guy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
* OWNER 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.
*/
import java.awt.*;
import javax.swing.*;
/**
* @author Romain Guy
*/
public class SafeRepaint extends JFrame {
private SafeComponent safeComponent;
public SafeRepaint() {
super("Safe Repaint");
safeComponent = new SafeComponent();
add(safeComponent);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
new Thread(new Runnable() {
public void run() {
while (true) {
safeComponent.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SafeRepaint repaint = new SafeRepaint();
repaint.setVisible(true);
}
});
}
}
/*
* Copyright (c) 2007, Romain Guy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
* OWNER 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.
*/
/**
* @author Romain Guy
*/
class SafeComponent extends JLabel {
public SafeComponent() {
super("Safe Repaint");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(SwingUtilities.isEventDispatchThread());
}
}
SwingUtilities.isLeftMouseButton(MouseEvent mouseEvent)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Button Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Me");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
};
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
int modifiers = mouseEvent.getModifiers();
if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
System.out.println("Left button pressed.");
}
if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
System.out.println("Middle button pressed.");
}
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
System.out.println("Right button pressed.");
}
}
public void mouseReleased(MouseEvent mouseEvent) {
if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
System.out.println("Left button released.");
}
if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
System.out.println("Middle button released.");
}
if (SwingUtilities.isRightMouseButton(mouseEvent)) {
System.out.println("Right button released.");
}
System.out.println();
}
};
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
SwingUtilities.isMiddleMouseButton(MouseEvent mouseEvent)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Button Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Me");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
};
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
int modifiers = mouseEvent.getModifiers();
if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
System.out.println("Left button pressed.");
}
if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
System.out.println("Middle button pressed.");
}
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
System.out.println("Right button pressed.");
}
}
public void mouseReleased(MouseEvent mouseEvent) {
if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
System.out.println("Left button released.");
}
if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
System.out.println("Middle button released.");
}
if (SwingUtilities.isRightMouseButton(mouseEvent)) {
System.out.println("Right button released.");
}
System.out.println();
}
};
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
SwingUtilities.isRightMouseButton(MouseEvent mouseEvent)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Button Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Me");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
};
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
int modifiers = mouseEvent.getModifiers();
if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
System.out.println("Left button pressed.");
}
if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
System.out.println("Middle button pressed.");
}
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
System.out.println("Right button pressed.");
}
}
public void mouseReleased(MouseEvent mouseEvent) {
if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
System.out.println("Left button released.");
}
if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
System.out.println("Middle button released.");
}
if (SwingUtilities.isRightMouseButton(mouseEvent)) {
System.out.println("Right button released.");
}
System.out.println();
}
};
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
SwingUtilities: updateComponentTreeUI(Component c)
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class MainClass {
protected JFrame theFrame = new JFrame("LNF Switcher");
protected Container cp;
protected String curLF = "javax.swing.plaf.metal.MetalLookAndFeel";
protected JRadioButton previousButton;
public MainClass() {
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = theFrame.getContentPane();
cp.setLayout(new FlowLayout());
ButtonGroup bg = new ButtonGroup();
JRadioButton bJava = new JRadioButton("Java");
bJava.addActionListener(new LNFSetter("javax.swing.plaf.metal.MetalLookAndFeel", bJava));
bg.add(bJava);
cp.add(bJava);
JRadioButton bMSW = new JRadioButton("MS-Windows");
bMSW.addActionListener(new LNFSetter("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", bMSW));
bg.add(bMSW);
cp.add(bMSW);
JRadioButton bMotif = new JRadioButton("Motif");
bMotif.addActionListener(new LNFSetter("com.sun.java.swing.plaf.motif.MotifLookAndFeel", bMotif));
bg.add(bMotif);
cp.add(bMotif);
JRadioButton bMac = new JRadioButton("Sun-MacOS");
bMac.addActionListener(new LNFSetter("com.sun.java.swing.plaf.mac.MacLookAndFeel", bMac));
bg.add(bMac);
cp.add(bMac);
String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
JRadioButton bDefault = new JRadioButton("Default");
bDefault.addActionListener(new LNFSetter(defaultLookAndFeel, bDefault));
bg.add(bDefault);
cp.add(bDefault);
(previousButton = bDefault).setSelected(true);
theFrame.pack();
theFrame.setVisible(true);
}
class LNFSetter implements ActionListener {
String theLNFName;
JRadioButton thisButton;
LNFSetter(String lnfName, JRadioButton me) {
theLNFName = lnfName;
thisButton = me;
}
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(theLNFName);
SwingUtilities.updateComponentTreeUI(theFrame);
theFrame.pack();
} catch (Exception evt) {
JOptionPane.showMessageDialog(null, "setLookAndFeel didn"t work: " + evt, "UI Failure",
JOptionPane.INFORMATION_MESSAGE);
previousButton.setSelected(true); // reset the GUI to agree
}
previousButton = thisButton;
}
}
public static void main(String[] argv) {
new MainClass();
}
}