当前位置: 技术问答>java相关
怎样画矩形呀
来源: 互联网 发布时间:2015-08-29
本文导语: 想在panel中画一矩形,为啥画不出来呀? import javax.swing.*; import java.awt.*; import javax.swing.border.*; public class myclspaneltest extends JApplet { public void init() { Container mycontainer01=getContentPane(); ...
想在panel中画一矩形,为啥画不出来呀?
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class myclspaneltest extends JApplet
{
public void init()
{
Container mycontainer01=getContentPane();
myPanel mypanel02=new myPanel();
mycontainer01.setLayout(new FlowLayout());
mycontainer01.add(mypanel02);
}
}
class myPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
update(g);
}
public void update(Graphics g)
{
g.drawOval(100,100,50,50);
}
}
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class myclspaneltest extends JApplet
{
public void init()
{
Container mycontainer01=getContentPane();
myPanel mypanel02=new myPanel();
mycontainer01.setLayout(new FlowLayout());
mycontainer01.add(mypanel02);
}
}
class myPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
update(g);
}
public void update(Graphics g)
{
g.drawOval(100,100,50,50);
}
}
|
mycontainer01.setLayout(new FlowLayout());
mypanel02的父容器你设为FlowLayout,其大小是自动设定的。运行后,你会发现mypanel02是很小的,你要画(100,100,50,50),已经超出了它的范围,当然看不到了。另外,重载paintComponent()方法也是可以的。
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
/*
*/
public class myclspaneltest extends JApplet
{
public void init()
{
Container mycontainer01=getContentPane();
TitledBorder mytitleborder01=new TitledBorder("mypanel01 border");
JButton mybutton01=new JButton("Center");
JButton mybutton02=new JButton("This North");
JButton mybutton03=new JButton("This is a test");
JPanel mypanel01=new JPanel();
myPanel mypanel02=new myPanel();
mycontainer01.setLayout(new GridLayout());
mycontainer01.setSize(new Dimension(250, 200));
mypanel01.setLayout(new BorderLayout());
mypanel01.setBorder(mytitleborder01);
mypanel01.setBorder(BorderFactory.createLineBorder(Color.blue,2));
mypanel02.setBorder(BorderFactory.createLineBorder(Color.red,2));
mypanel01.add(mybutton01,BorderLayout.CENTER);
mypanel01.add(mybutton02,BorderLayout.NORTH);
mypanel02.add(mybutton03);
mycontainer01.add(mypanel01);
mycontainer01.add(mypanel02);
}
}
class myPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
update(g);
}
public void update(Graphics g)
{
g.drawRect(100,100,50,60);
}
}
mypanel02的父容器你设为FlowLayout,其大小是自动设定的。运行后,你会发现mypanel02是很小的,你要画(100,100,50,50),已经超出了它的范围,当然看不到了。另外,重载paintComponent()方法也是可以的。
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
/*
*/
public class myclspaneltest extends JApplet
{
public void init()
{
Container mycontainer01=getContentPane();
TitledBorder mytitleborder01=new TitledBorder("mypanel01 border");
JButton mybutton01=new JButton("Center");
JButton mybutton02=new JButton("This North");
JButton mybutton03=new JButton("This is a test");
JPanel mypanel01=new JPanel();
myPanel mypanel02=new myPanel();
mycontainer01.setLayout(new GridLayout());
mycontainer01.setSize(new Dimension(250, 200));
mypanel01.setLayout(new BorderLayout());
mypanel01.setBorder(mytitleborder01);
mypanel01.setBorder(BorderFactory.createLineBorder(Color.blue,2));
mypanel02.setBorder(BorderFactory.createLineBorder(Color.red,2));
mypanel01.add(mybutton01,BorderLayout.CENTER);
mypanel01.add(mybutton02,BorderLayout.NORTH);
mypanel02.add(mybutton03);
mycontainer01.add(mypanel01);
mycontainer01.add(mypanel02);
}
}
class myPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
update(g);
}
public void update(Graphics g)
{
g.drawRect(100,100,50,60);
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。