谁能帮我解释一下这段简单的java小程序,尤其是参数的设置,各参数实现什么功能,谢谢!
publicclassDrawTestextendsJFrame{JPanelbuttonPanel=newJPanel();JButtonround=newJButto...
public class DrawTest extends JFrame {
JPanel buttonPanel=new JPanel();
JButton round=new JButton("圆");
JButton rectangle=new JButton("长方形");
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");
setLayout(null);
setSize(600,400);
setResizable(false);
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);
round.setBounds(170, 20, 60, 30);
rectangle.setBounds(350, 20, 80, 30);
buttonPanel.add(round);
buttonPanel.add(rectangle);
add(buttonPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(final Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillOval(roundX,roundY,roundW,roundH);
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);
repaint();
}
}
});
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
} 展开
JPanel buttonPanel=new JPanel();
JButton round=new JButton("圆");
JButton rectangle=new JButton("长方形");
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");
setLayout(null);
setSize(600,400);
setResizable(false);
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);
round.setBounds(170, 20, 60, 30);
rectangle.setBounds(350, 20, 80, 30);
buttonPanel.add(round);
buttonPanel.add(rectangle);
add(buttonPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(final Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillOval(roundX,roundY,roundW,roundH);
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);
repaint();
}
}
});
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
} 展开
3个回答
展开全部
public class DrawTest extends JFrame {
//生成一个面板
JPanel buttonPanel=new JPanel();
//生成一个按钮并在外观上显示园
JButton round=new JButton("圆");
//生成一个按钮并在外观上显示长方形
JButton rectangle=new JButton("长方形");
//设置圆的初始位置的x,y值,并设置高和宽
int roundX=130,roundY=150,roundW=50,roundH=50;
//设置矩形的初始位置的x,y值,并设置高和宽
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
//设置本界面的标题为圆和长方形
setTitle("圆和长方形");
//设置布局为空
setLayout(null);
//设置整个窗体的大小
setSize(600,400);
//设置这个窗体不能改变大小
setResizable(false);
//设置这个窗体的依赖性
setLocationRelativeTo(null);
//设置按钮面板的布局为空
buttonPanel.setLayout(null);
//设置按钮面板的边界值
buttonPanel.setBounds(0, 300, 600, 100);
//设置round这个按钮的边界
round.setBounds(170, 20, 60, 30);
//设置rectangle这个按钮的边界
rectangle.setBounds(350, 20, 80, 30);
//向按钮这个面板中添加round这个按钮
buttonPanel.add(round);
//向按钮这个面板中添加rectangle这个按钮
buttonPanel.add(rectangle);
//向这个窗体中添加buttonPane这个面板
add(buttonPanel);
//设置窗体的可见性为真
setVisible(true);
//设置整个窗体按照默认的关闭操作
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//在面板上画图函数,Graphics是一个java虚拟的屏幕
public void paint(final Graphics g){
//调用父类的paint函数
super.paint(g);
//设置画笔的颜色为黑色
g.setColor(Color.black);
//画一个圆
g.fillOval(roundX,roundY,roundW,roundH);
//画一个矩形
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//为round这个按钮添加事件监听器
round.addMouseListener(new MouseAdapter() {
//如果发生鼠标点击的事件,e为事件
public void mouseClicked(MouseEvent e) {
//如果点击的是鼠标的一号键时,就执行下面的程序
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
//重新设置一个圆的值
g.fillOval(roundX,roundY,roundW,roundH);
//调用repaint进行真正意义上的重画
repaint();
}
}
});
//基本同上,就不赘述了
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
//重新画一个矩形
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//调用repaint函数进行再面板上进行重画
repaint();
}
}
});
}
//主函数,参数在这个程序中没有什么体现。
public static void main(String[] args) {
//生成一个DrawTest的对象,并调用init函数进行初始化
new DrawTest().init();
}
}
//生成一个面板
JPanel buttonPanel=new JPanel();
//生成一个按钮并在外观上显示园
JButton round=new JButton("圆");
//生成一个按钮并在外观上显示长方形
JButton rectangle=new JButton("长方形");
//设置圆的初始位置的x,y值,并设置高和宽
int roundX=130,roundY=150,roundW=50,roundH=50;
//设置矩形的初始位置的x,y值,并设置高和宽
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
//设置本界面的标题为圆和长方形
setTitle("圆和长方形");
//设置布局为空
setLayout(null);
//设置整个窗体的大小
setSize(600,400);
//设置这个窗体不能改变大小
setResizable(false);
//设置这个窗体的依赖性
setLocationRelativeTo(null);
//设置按钮面板的布局为空
buttonPanel.setLayout(null);
//设置按钮面板的边界值
buttonPanel.setBounds(0, 300, 600, 100);
//设置round这个按钮的边界
round.setBounds(170, 20, 60, 30);
//设置rectangle这个按钮的边界
rectangle.setBounds(350, 20, 80, 30);
//向按钮这个面板中添加round这个按钮
buttonPanel.add(round);
//向按钮这个面板中添加rectangle这个按钮
buttonPanel.add(rectangle);
//向这个窗体中添加buttonPane这个面板
add(buttonPanel);
//设置窗体的可见性为真
setVisible(true);
//设置整个窗体按照默认的关闭操作
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//在面板上画图函数,Graphics是一个java虚拟的屏幕
public void paint(final Graphics g){
//调用父类的paint函数
super.paint(g);
//设置画笔的颜色为黑色
g.setColor(Color.black);
//画一个圆
g.fillOval(roundX,roundY,roundW,roundH);
//画一个矩形
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//为round这个按钮添加事件监听器
round.addMouseListener(new MouseAdapter() {
//如果发生鼠标点击的事件,e为事件
public void mouseClicked(MouseEvent e) {
//如果点击的是鼠标的一号键时,就执行下面的程序
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
//重新设置一个圆的值
g.fillOval(roundX,roundY,roundW,roundH);
//调用repaint进行真正意义上的重画
repaint();
}
}
});
//基本同上,就不赘述了
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
//重新画一个矩形
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//调用repaint函数进行再面板上进行重画
repaint();
}
}
});
}
//主函数,参数在这个程序中没有什么体现。
public static void main(String[] args) {
//生成一个DrawTest的对象,并调用init函数进行初始化
new DrawTest().init();
}
}
展开全部
public class DrawTest extends JFrame {//继承窗口类
JPanel buttonPanel=new JPanel();//new一个面板
JButton round=new JButton("圆");//定义一个按钮
JButton rectangle=new JButton("长方形");//定义一个按钮
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");//设置窗口的标题
setLayout(null); //
setSize(600,400);//设置窗口的窗口既大小
setResizable(false);////设置窗口为隐藏
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);//设置buttonPanel的位置
round.setBounds(170, 20, 60, 30);//设置按钮圆的位置
rectangle.setBounds(350, 20, 80, 30);//设置按钮长方形的位置
buttonPanel.add(round);//把按钮添加到buttonPanel面板里
buttonPanel.add(rectangle);
add(buttonPanel);//添加按钮到窗口里面
setVisible(true);//设置窗口为显示
setDefaultCloseOperation(EXIT_ON_CLOSE);// 单击关闭窗口时执行的操作(关闭)。
}
public void paint(final Graphics g){//画笔
super.paint(g);
g.setColor(Color.black);//将此g图形上下文的当前颜色设置为黑色
g.fillOval(roundX,roundY,roundW,roundH);//使用当前颜色填充外接指定矩形框的椭圆。
//roundX-要填充椭圆的左上角的 x 坐标,roundY-要填充椭圆的左上角的 y 坐标,roundW-要填充椭圆的宽度,roundH-要填充椭圆的高度
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//rectangleX-要填充矩形的左上角的 x 坐标,rectangleY-要填充矩形的左上角的 y 坐标,rectangleW-要填充矩形的宽度,rectangleH-要填充矩形的高度
//为圆按钮添加监听单击事件
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);//重新设置椭圆的属性既单击圆按钮变大
repaint();
}
}
});
//为长方形按钮添加监听单击事件
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
}
JPanel buttonPanel=new JPanel();//new一个面板
JButton round=new JButton("圆");//定义一个按钮
JButton rectangle=new JButton("长方形");//定义一个按钮
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");//设置窗口的标题
setLayout(null); //
setSize(600,400);//设置窗口的窗口既大小
setResizable(false);////设置窗口为隐藏
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);//设置buttonPanel的位置
round.setBounds(170, 20, 60, 30);//设置按钮圆的位置
rectangle.setBounds(350, 20, 80, 30);//设置按钮长方形的位置
buttonPanel.add(round);//把按钮添加到buttonPanel面板里
buttonPanel.add(rectangle);
add(buttonPanel);//添加按钮到窗口里面
setVisible(true);//设置窗口为显示
setDefaultCloseOperation(EXIT_ON_CLOSE);// 单击关闭窗口时执行的操作(关闭)。
}
public void paint(final Graphics g){//画笔
super.paint(g);
g.setColor(Color.black);//将此g图形上下文的当前颜色设置为黑色
g.fillOval(roundX,roundY,roundW,roundH);//使用当前颜色填充外接指定矩形框的椭圆。
//roundX-要填充椭圆的左上角的 x 坐标,roundY-要填充椭圆的左上角的 y 坐标,roundW-要填充椭圆的宽度,roundH-要填充椭圆的高度
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
//rectangleX-要填充矩形的左上角的 x 坐标,rectangleY-要填充矩形的左上角的 y 坐标,rectangleW-要填充矩形的宽度,rectangleH-要填充矩形的高度
//为圆按钮添加监听单击事件
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);//重新设置椭圆的属性既单击圆按钮变大
repaint();
}
}
});
//为长方形按钮添加监听单击事件
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询