Java by API/javax.swing/JPanel — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:19, 31 мая 2010
Содержание
- 1 extends JPanel
- 2 JPanel: addAncestorListener(AncestorListener lis)
- 3 JPanel: addContainerListener(ContainerListener l)
- 4 JPanel: addKeyListener(KeyListener l)
- 5 JPanel: addMouseListener(MouseListener mous)
- 6 JPanel: addMouseMotionListener(MouseMotionListener lis)
- 7 JPanel: getFocusTraversalKeys(int id)
- 8 JPanel: isFocusCycleRoot()
- 9 JPanel: paintComponent(Graphics g)
- 10 JPanel: repaint()
- 11 JPanel: setBackground(Color c)
- 12 JPanel: setBorder(Border border)
- 13 JPanel: setComponentPopupMenu(JPopupMenu popup)
- 14 JPanel: setFocusCycleRoot(boolean value)
- 15 JPanel: setFocusTraversalPolicyProvider(boolean value)
- 16 JPanel: setLayout(null)
- 17 JPanel: setOpaque(boolean isOpaque)
- 18 JPanel: setPreferredSize(Dimension preferredSize)
- 19 JPanel: setToolTipText(String htmlString)
extends JPanel
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
Color color;
public MainClass(Color color) {
this.color = color;
}
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(color);
g.drawOval(0, 0, width, height);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Oval Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 2));
Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 4; i++) {
MainClass panel = new MainClass(colors[i]);
frame.add(panel);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JPanel: addAncestorListener(AncestorListener lis)
import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame("Ancestor Sampler");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AncestorListener ancestorListener = new AncestorListener() {
public void ancestorAdded(AncestorEvent ancestorEvent) {
System.out.println("Added");
}
public void ancestorMoved(AncestorEvent ancestorEvent) {
System.out.println("Moved");
}
public void ancestorRemoved(AncestorEvent ancestorEvent) {
System.out.println("Removed");
}
};
frame.getRootPane().addAncestorListener(ancestorListener);
frame.setSize(300, 200);
frame.setVisible(true);
frame.getRootPane().setVisible(false);
frame.getRootPane().setVisible(true);
}
}
JPanel: addContainerListener(ContainerListener l)
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
buttonPanel.addContainerListener(new ContainerListener() {
public void componentAdded(ContainerEvent e) {
displayMessage(" added to ", e);
}
public void componentRemoved(ContainerEvent e) {
displayMessage(" removed from ", e);
}
void displayMessage(String action, ContainerEvent e) {
System.out.println(((JButton) e.getChild()).getText() + " was" + action
+ e.getContainer().getClass().getName());
}
});
buttonPanel.add(new JButton("A"));
frame.add(buttonPanel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JPanel: addKeyListener(KeyListener l)
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel implements KeyListener {
private Point startPoint = new Point(0, 0);
private Point endPoint = new Point(0, 0);
public Main() {
addKeyListener(this);
}
public void keyPressed(KeyEvent evt) {
int keyCode = evt.getKeyCode();
int d;
if (evt.isShiftDown())
d = 5;
else
d = 1;
if (keyCode == KeyEvent.VK_LEFT)
add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT)
add(d, 0);
else if (keyCode == KeyEvent.VK_UP)
add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN)
add(0, d);
}
public void keyReleased(KeyEvent evt) {
}
public void keyTyped(KeyEvent evt) {
}
public boolean isFocusTraversable() {
return true;
}
public void add(int dx, int dy) {
endPoint.x += dx;
endPoint.y += dy;
Graphics g = getGraphics();
g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
g.dispose();
startPoint.x = endPoint.x;
startPoint.y = endPoint.y;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Sketch");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.add(new Main());
frame.setVisible(true);
}
}
JPanel: addMouseListener(MouseListener mous)
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
}
});
}
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);
}
}
JPanel: addMouseMotionListener(MouseMotionListener lis)
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.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setBounds(200, 200, 200, 200);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = aWindow.getContentPane();
content.add(new MouseMotionEvents());
aWindow.setVisible(true);
}
}
class MouseMotionEvents extends JPanel implements MouseListener,
MouseMotionListener {
Point p;
public MouseMotionEvents() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
p = me.getPoint();
repaint();
}
public void mouseReleased(MouseEvent me) {
p = null;
repaint();
}
public void mouseDragged(MouseEvent me) {
p = me.getPoint();
repaint();
}
public void mouseMoved(MouseEvent me) {
}
public void paint(Graphics g) {
if (p != null) {
Dimension d = getSize();
int xc = d.width / 2;
int yc = d.height / 2;
g.drawLine(xc, yc, p.x, p.y);
}
}
}
JPanel: getFocusTraversalKeys(int id)
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] args) {
JFrame aWindow = new JFrame("This is a Border Layout");
aWindow.setBounds(30, 30, 300, 300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(new JTextField(10));
p.add(new JTextField(10));
p.add(new JTextField(10));
p.add(new JTextField(10));
p.add(new JTextField(10));
p.add(new JTextField(10));
Set<AWTKeyStroke> set = p.getFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
//set = new HashSet(set);
KeyStroke up = KeyStroke.getKeyStroke("A");
set.add(up);
System.out.println(set);
p.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);
aWindow.add(p);
aWindow.setVisible(true);
}
}
JPanel: isFocusCycleRoot()
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Cycle Sample");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(3,3));
for (int i = 0; i < 8; i++) {
JButton button = new JButton("" + i);
contentPane.add(button);
}
JPanel panel = new FocusCycleConstrainedJPanel();
panel.setLayout(new GridLayout(1, 3));
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + (i + 3));
panel.add(button);
}
contentPane.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class FocusCycleConstrainedJPanel extends JPanel {
public boolean isFocusCycleRoot() {
return true;
}
}
JPanel: paintComponent(Graphics g)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
Color color;
public MainClass(Color color) {
this.color = color;
}
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(color);
g.drawOval(0, 0, width, height);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Oval Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 2));
Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 4; i++) {
MainClass panel = new MainClass(colors[i]);
frame.add(panel);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JPanel: repaint()
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setBounds(200, 200, 200, 200);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = aWindow.getContentPane();
content.add(new MouseMotionAdapterDemo());
aWindow.setVisible(true);
}
}
class MouseMotionAdapterDemo extends JPanel {
public MouseMotionAdapterDemo() {
setBackground(Color.white);
addMouseListener(new Adapter1(this));
addMouseMotionListener(new Adapter2(this));
}
}
class Adapter1 extends MouseAdapter {
MouseMotionAdapterDemo mmad;
public Adapter1(MouseMotionAdapterDemo mmad) {
this.mmad = mmad;
}
public void mouseReleased(MouseEvent me) {
mmad.setBackground(Color.white);
mmad.repaint();
}
}
class Adapter2 extends MouseMotionAdapter {
MouseMotionAdapterDemo mmad;
public Adapter2(MouseMotionAdapterDemo mmad) {
this.mmad = mmad;
}
public void mouseDragged(MouseEvent me) {
mmad.setBackground(Color.cyan);
mmad.repaint();
}
}
JPanel: setBackground(Color c)
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setBounds(200, 200, 200, 200);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = aWindow.getContentPane();
content.add(new MouseMotionAdapterDemo());
aWindow.setVisible(true);
}
}
class MouseMotionAdapterDemo extends JPanel {
public MouseMotionAdapterDemo() {
setBackground(Color.white);
addMouseListener(new Adapter1(this));
addMouseMotionListener(new Adapter2(this));
}
}
class Adapter1 extends MouseAdapter {
MouseMotionAdapterDemo mmad;
public Adapter1(MouseMotionAdapterDemo mmad) {
this.mmad = mmad;
}
public void mouseReleased(MouseEvent me) {
mmad.setBackground(Color.white);
mmad.repaint();
}
}
class Adapter2 extends MouseMotionAdapter {
MouseMotionAdapterDemo mmad;
public Adapter2(MouseMotionAdapterDemo mmad) {
this.mmad = mmad;
}
public void mouseDragged(MouseEvent me) {
mmad.setBackground(Color.cyan);
mmad.repaint();
}
}
JPanel: setBorder(Border border)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame {
public static void main(String[] args) {
String lyrics = "<html>Line<br>line<br>line</html>";
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(10, 10));
JLabel label = new JLabel(lyrics);
label.setFont(new Font("Georgia", Font.PLAIN, 14));
label.setForeground(new Color(50, 50, 25));
panel.add(label, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JFrame f = new JFrame();
f.add(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
JPanel: setComponentPopupMenu(JPopupMenu popup)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main implements ActionListener {
JTextArea textArea = new JTextArea();
public Main() {
final JPopupMenu contextMenu = new JPopupMenu("Edit");
contextMenu.add(makeMenuItem("Save"));
contextMenu.add(makeMenuItem("Save As"));
contextMenu.add(makeMenuItem("Close"));
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
frame.add(panel);
panel.setComponentPopupMenu(contextMenu);
textArea.setInheritsPopupMenu(true);
panel.add(BorderLayout.CENTER, textArea);
JTextField textField = new JTextField();
textField.setInheritsPopupMenu(true);
panel.add(BorderLayout.SOUTH, textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textArea.append(e.getActionCommand() + "\n");
}
private JMenuItem makeMenuItem(String label) {
JMenuItem item = new JMenuItem(label);
item.addActionListener(this);
return item;
}
public static void main(String[] args) {
new Main();
}
}
JPanel: setFocusCycleRoot(boolean value)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Cycle Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3,3));
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + i);
frame.add(button);
}
JPanel panel = new JPanel();
panel.setFocusCycleRoot(true);
panel.setFocusTraversalPolicyProvider(true);
panel.setLayout(new GridLayout(1, 3));
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + (i + 3));
panel.add(button);
}
frame.add(panel);
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + (i + 6));
frame.add(button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JPanel: setFocusTraversalPolicyProvider(boolean value)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Cycle Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3,3));
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + i);
frame.add(button);
}
JPanel panel = new JPanel();
panel.setFocusCycleRoot(true);
panel.setFocusTraversalPolicyProvider(true);
panel.setLayout(new GridLayout(1, 3));
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + (i + 3));
panel.add(button);
}
frame.add(panel);
for (int i = 0; i < 3; i++) {
JButton button = new JButton("" + (i + 6));
frame.add(button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JPanel: setLayout(null)
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);
}
}
JPanel: setOpaque(boolean isOpaque)
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame {
public MainClass() {
super("Opaque JPanel Demo");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel opaque = createNested(true);
JPanel notOpaque = createNested(false);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(opaque);
getContentPane().add(notOpaque);
}
public static void main(String[] args) {
MainClass oe = new MainClass();
oe.setVisible(true);
}
public JPanel createNested(boolean opaque) {
JPanel outer = new JPanel(new FlowLayout());
JPanel inner = new JPanel(new FlowLayout());
outer.setBackground(Color.white);
inner.setBackground(Color.black);
inner.setOpaque(opaque);
inner.setBorder(BorderFactory.createLineBorder(Color.gray));
inner.add(new JButton("Button"));
outer.add(inner);
return outer;
}
}
JPanel: setPreferredSize(Dimension preferredSize)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("Label Demo");
f.setLayout(new FlowLayout());
f.setSize(200, 360);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label= new JLabel("asdf");
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setPreferredSize(new Dimension(150, 100));
f.add(label);
f.setVisible(true);
}
}
JPanel: setToolTipText(String htmlString)
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolTip;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tooltip");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setToolTipText("<HtMl>Tooltip<br>Message");
frame.add(panel, BorderLayout.CENTER);
JButton button = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
public boolean contains(int x, int y) {
if (x < 100) {
setToolTipText("x < 100");
} else {
setToolTipText("else");
}
return super.contains(x, y);
}
};
button.setToolTipText("Hello, World");
frame.add(button, BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}