前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java JAVA生成随机椭圆二一个以上widget的frame

java JAVA生成随机椭圆二一个以上widget的frame

作者头像
葫芦
发布2019-05-09 11:45:13
4620
发布2019-05-09 11:45:13
举报
文章被收录于专栏:葫芦

 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);
}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014/02/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档