JAVA生成随机椭圆二一个以上widget的frame,内部类
代码语言:javascript复制package liezi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoButtons {
JFrame frame;
JLabel label1;
JLabel label2;
public static void main(String[] args){
TwoButtons TwoButtons=new TwoButtons();
TwoButtons.go();
}
public void go(){
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton=new JButton("Change Label");
labelButton.addActionListener(new LabelListener());
JButton colorButton=new JButton("Change Circle");
colorButton.addActionListener(new ColorListener());
label1=new JLabel("primitive label");
label2=new JLabel("www.liezi.net");
MyDrawPanel drawPanel=new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH,colorButton);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.getContentPane().add(BorderLayout.EAST,labelButton);
frame.getContentPane().add(BorderLayout.WEST,label1);
frame.getContentPane().add(BorderLayout.NORTH,label2);
frame.setSize(300,300);
frame.setVisible(true);
}
class ColorListener implements ActionListener{
public void actionPerformed(ActionEvent event){
frame.repaint();
}
}
class LabelListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{ frame.repaint();
label1.setText("Ouch!");
label2.setText("*.liezi.net");
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package liezi;
import java.awt.*;
import javax.swing.*;
public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g){
g.fillRect(0, 0, this.getWidth(), this.getHeight());
int x=(int)(Math.random()*255);
int y=(int)(Math.random()*255);
Color randomColor=new Color(x, y, 34);
g.setColor(randomColor);
g.fillOval(50,50,40,40);
}
}