首页 > 代码库 > java学习笔记_GUI(4)
java学习笔记_GUI(4)
1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 class MyPanel_0 extends JPanel { 6 public void paintComponent( Graphics g ) { 7 g.setColor( Color.orange ); 8 g.fillRect(20, 50, 100, 100); 9 }10 }11 12 class MyPanel_1 extends JPanel {13 public void paintComponent( Graphics g ) {14 Image image = new ImageIcon("Winter.jpg").getImage();15 g.drawImage(image, 3, 4, this);16 }17 }18 19 class MyPanel_2 extends JPanel {20 public void paintComponent( Graphics g ) {21 g.fillRect(0, 0, this.getWidth(), this.getHeight());22 int red = (int) (Math.random() * 255);23 int green = (int) (Math.random() * 255);24 int blue = (int) (Math.random() * 255);25 Color randomColor = new Color(red, green, blue);26 g.setColor(randomColor);27 g.fillOval(70, 40, 100, 100);28 }29 }30 31 class MyPanel_3 extends JPanel {32 public void paintComponent( Graphics g ) {33 Graphics2D g2d = (Graphics2D) g;34 GradientPaint gradient = new GradientPaint(70, 70, Color.blue, 150, 150, Color.orange);35 g2d.setPaint( gradient );36 g2d.fillOval(70, 70, 100, 100);37 }38 }39 40 class Gui implements ActionListener{41 42 JButton button = new JButton("click me");43 JFrame frame = new JFrame();44 45 private void set_frame() {46 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);47 frame.setSize(300, 200);48 frame.setVisible(true);49 }50 51 public void show_button() {52 set_frame();53 frame.getContentPane().add(button);54 button.addActionListener(this);55 }56 57 public void show_my_panel( String index) {58 set_frame();59 if ( index.equals("0") ) {60 frame.getContentPane().add(new MyPanel_0());61 }62 else if ( index.equals("1") ) {63 frame.getContentPane().add(new MyPanel_1());64 }65 else if ( index.equals("2") ) {66 frame.getContentPane().add(new MyPanel_2());67 }68 else if ( index.equals("3") ) {69 frame.getContentPane().add(new MyPanel_3());70 }71 }72 73 public void actionPerformed( ActionEvent event ) {74 button.setText("I‘ve been clicked!");75 }76 }77 78 class GuiTest {79 public static void main( String[] args ) {80 Gui gui = new Gui();81 if ( args.length > 0 ) {82 gui.show_my_panel( args[0] );83 }84 else {85 gui.show_button();86 }87 }88 }
这个程序演示了几种不同的绘图等方法
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(70, 70, Color.blue, 150, 150, 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学习笔记_GUI(4)