1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import java.awt.*;
import javax.swing.*;
public class PaintDemo extends JFrame {
private final DrawingPanel drawingPanel;
public PaintDemo() {
super("PaintDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
drawingPanel = new DrawingPanel();
add(drawingPanel);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
public void run() {
new PaintDemo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}
class DrawingPanel extends JComponent {
private final JButton paintedButton;
private final JButton realButton;
public DrawingPanel() {
paintedButton = new JButton("Painted JButton");
realButton = new JButton("Real JButton");
JDialog jDialog = new JDialog();
jDialog.setLayout(new FlowLayout());
jDialog.add(paintedButton);
jDialog.pack();
setLayout(new GridBagLayout());
add(realButton);
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawOval(100, 100, 100, 100);
paintedButton.paint(g);
}
} |
Partager