1 Importjavax.swing.*;2 Importjava.awt.event.*;3 Importjava.awt.*;4 5 classMypanel_0extendsJPanel {6 Public voidpaintcomponent (Graphics g) {7 G.setcolor (color.orange);8G.fillrect (20, 50, 100, 100);9 }Ten } One A classMypanel_1extendsJPanel { - Public voidpaintcomponent (Graphics g) { -Image image =NewImageIcon ("Winter.jpg"). GetImage (); theG.drawimage (Image, 3, 4, This); - } - } - + classMypanel_2extendsJPanel { - Public voidpaintcomponent (Graphics g) { +G.fillrect (0, 0, This. GetWidth (), This. GetHeight ()); A intRed = (int) (Math.random () * 255); at intGreen = (int) (Math.random () * 255); - intBlue = (int) (Math.random () * 255); -Color Randomcolor =NewColor (red, green, blue); - G.setcolor (randomcolor); -G.filloval (70, 40, 100, 100); - } in } - to classMypanel_3extendsJPanel { + Public voidpaintcomponent (Graphics g) { -Graphics2D g2d =(graphics2d) G; theGradientpaint gradient =NewGradientpaint (Color.Blue, 150, 150, color.orange); * g2d.setpaint (gradient); $G2d.filloval (70, 70, 100, 100);Panax Notoginseng } - } the + classGuiImplementsactionlistener{ A theJButton button =NewJButton ("Click Me"); +JFrame frame =NewJFrame (); - $ Private voidSet_frame () { $ frame.setdefaultcloseoperation (jframe.exit_on_close); -Frame.setsize (300, 200); -Frame.setvisible (true); the } - Wuyi Public voidShow_button () { the set_frame (); - Frame.getcontentpane (). Add (button); WuButton.addactionlistener ( This); - } About $ Public voidShow_my_panel (String index) { - set_frame (); - if(Index.equals ("0") ) { -Frame.getcontentpane (). Add (Newmypanel_0 ()); A } + Else if(Index.equals ("1") ) { theFrame.getcontentpane (). Add (NewMypanel_1 ()); - } $ Else if(Index.equals ("2") ) { theFrame.getcontentpane (). Add (Newmypanel_2 ()); the } the Else if(Index.equals ("3") ) { theFrame.getcontentpane (). Add (Newmypanel_3 ()); - } in } the the Public voidactionperformed (ActionEvent event) { AboutButton.settext ("I ' ve been clicked!"); the } the } the + classGuitest { - Public Static voidMain (string[] args) { theGUI GUI =NewGui ();Bayi if(Args.length > 0 ) { theGui.show_my_panel (args[0] ); the } - Else { - Gui.show_button (); the } the } the}
This program demonstrates several different methods of drawing, etc.
Import javax.swing.*;
Import java.awt.event.*;
Import java.awt.*;
Class Mypanel_0 extends JPanel {
public void Paintcomponent (Graphics g) {
G.setcolor (Color.orange);
G.fillrect (20, 50, 100, 100);
}
}
Class Mypanel_1 extends JPanel {
public void Paintcomponent (Graphics g) {
Image image = new ImageIcon ("Winter.jpg"). GetImage ();
G.drawimage (Image, 3, 4, this);
}
}
Class Mypanel_2 extends JPanel {
public void Paintcomponent (Graphics g) {
G.fillrect (0, 0, this.getwidth (), This.getheight ());
int red = (int) (Math.random () * 255);
int green = (int) (Math.random () * 255);
int blue = (int) (Math.random () * 255);
Color Randomcolor = new color (red, green, blue);
G.setcolor (Randomcolor);
G.filloval (70, 40, 100, 100);
}
}
Class Mypanel_3 extends JPanel {
public void Paintcomponent (Graphics g) {
Graphics2D g2d = (graphics2d) g;
Gradientpaint gradient = new Gradientpaint (all, Color.Blue, color.orange);
G2d.setpaint (gradient);
G2d.filloval (70, 70, 100, 100);
}
}
Class Gui implements actionlistener{
JButton button = new JButton ("click Me");
JFrame frame = new JFrame ();
private void Set_frame () {
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setsize (300, 200);
Frame.setvisible (TRUE);
}
public void Show_button () {
Set_frame ();
Frame.getcontentpane (). Add (button);
Button.addactionlistener (this);
}
public void Show_my_panel (String index) {
Set_frame ();
if (Index.equals ("0")) {
Frame.getcontentpane (). Add (New Mypanel_0 ());
}
else if (index.equals ("1")) {
Frame.getcontentpane (). Add (New Mypanel_1 ());
}
else if (Index.equals ("2")) {
Frame.getcontentpane (). Add (New mypanel_2 ());
}
else if (Index.equals ("3")) {
Frame.getcontentpane (). Add (New Mypanel_3 ());
}
}
public void actionperformed (ActionEvent event) {
Button.settext ("I ' ve been clicked!");
}
}
Class Guitest {
public static void Main (string[] args) {
GUI GUI = new Gui ();
if (Args.length > 0) {
Gui.show_my_panel (Args[0]);
}
else {
Gui.show_button ();
}
}
}
Java Learning Note _gui (4)