您的位置:首页 > 其它

在一个frame设置四个组件

2015-10-27 12:33 218 查看
import javax.swing.*;
import java.awt.*;
public class Panel 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 myColor = new Color(red,green,blue);
g.setColor(myColor);
g.fillOval(70,70, 100, 100);
}
}


View class Panel
在Panel(继承自JPanel)类中方法paintComponent()

fillRect(0,0,this.width,this.height)表示在该panel的(x,y)坐标为起始,this.width和this.height也表示该面板的宽高

需要作出改变的组件,放做全局

在go中有代码:

button1.addActionListener(new ColorActionListener());

button2.addActionListener(new LabelActionListener());

在外部类对象中新建的内部类对象会与该外部类对象产生联结,内部类对象可直接调用外部类对象的实例变量,也因此,需要改变的组件做实例变量也是为了给内部对象使用

BoarderLayout.CENTER 这个中间是相对于其他组件而言的,当其左边的组件变小时,他可能会变大
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: