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 43 44
| import java.awt.*;
import javax.swing.*;
public class JCanvas extends JPanel {
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillRect(10,10,80,80);
g.setColor(Color.BLUE);
g.fillOval(150,50,80,80);
g.setColor(c);
}
}
**********************************
public class Demo1 {
public static void main(String[] args) {
JCanvas jc = new JCanvas();
jc.setBackground(Color.WHITE);
jc.setPreferredSize(new Dimension(400,200));
GUIHelper.showOnFrame(jc,"test");
}
}
**************************************
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class GUIHelper {
public static void showOnFrame(JComponent component, String frameName) {
JFrame frame = new JFrame(frameName);
WindowAdapter wa = new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0);
}
};
frame.addWindowListener(wa);
frame.getContentPane().add(component); frame.pack(); frame.setVisible(true);
}
} |
Partager