我在一个applet中加入了自己定制的DrawPanel类,并且设置了背景色,DrawPanel类是用于画图的,但是很奇怪,无论怎么设置JPanel总是显示不出背景色,除非把paintComponent注释掉,这是为什么呢?希望大家能帮我讲述一下JPanel的绘图原理,无比感激!
public class MainApplet extends Applet implements ActionListener
{
JPanel jpnFunction = new JPanel();
DrawPanel jpnDraw;
/**
* 在applet上加swing组件。
* @param jc 组件
* @param x x坐标
* @param y y坐标
* @param width 组件长度
* @param height 组件高度
*/
public void add(JComponent jc,int x,int y,int width,int height)
{
jc.setBounds(x, y, width, height);
add(jc);
}
public void init()
{
setBackground(Color.yellow);
setLayout(null);
jpnDraw = new DrawPanel();
jpnDraw.setBackground(Color.green);
jpnFunction.setBackground(Color.blue);
add(jpnDraw,10,10,600,600);
add(jpnFunction,350,350,300,300);
} public void actionPerformed(ActionEvent e)
{ }
}public class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
g.drawString("fuck", getX(),getY());
g.drawRect(getX(),getY(),getWidth()/10,getHeight()/10);
}
}
public class MainApplet extends Applet implements ActionListener
{
JPanel jpnFunction = new JPanel();
DrawPanel jpnDraw;
/**
* 在applet上加swing组件。
* @param jc 组件
* @param x x坐标
* @param y y坐标
* @param width 组件长度
* @param height 组件高度
*/
public void add(JComponent jc,int x,int y,int width,int height)
{
jc.setBounds(x, y, width, height);
add(jc);
}
public void init()
{
setBackground(Color.yellow);
setLayout(null);
jpnDraw = new DrawPanel();
jpnDraw.setBackground(Color.green);
jpnFunction.setBackground(Color.blue);
add(jpnDraw,10,10,600,600);
add(jpnFunction,350,350,300,300);
} public void actionPerformed(ActionEvent e)
{ }
}public class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
g.drawString("fuck", getX(),getY());
g.drawRect(getX(),getY(),getWidth()/10,getHeight()/10);
}
}
解决方案 »
- HttpExchange 如何处理cookies
- 菜鸟请教,类中,数据是怎么传递的
- 请帮我看这3行代码,给分感谢
- 如何设置 AWT 的 java.awt.Menu 的快捷键?
- 请帮助我
- 我现在有个问题是在数据库里时间格式为2006-5-29 下午 03:46:32我现在要通过JSP查询数据库的某个时间段的情况,有没有什么好的方法让数据
- 请问java里面有什么类可以直接实现windows资源管理器的功能
- 老师留的作业题,我对java一窍不通,哪位大大帮忙回答一下啊,十分感谢
- 请问学习完sun的sl-275后学什么?
- 关于javaweb分页
- 想在Swing中使用应用程序级的全局快捷键, 来吧
- 正则表达式,找出每个超链接
如果要给jpnDraw添加背景色,可以通过绘制实体矩形来实现
public void paintComponent(Graphics g)
{
super.paintComponents(g);
//设置颜色
g.setColor(Color.green);
g.fillRect(getX(),getY(),getWidth(),getHeight());
//设置其他颜色
g.setColor(Color.blue);
g.drawString("fuck", getX(),getY());
g.drawRect(getX(),getY(),getWidth()/10,getHeight()/10);
}
public final static int CIRCLE = 1, SQUARE = 2;
private int shape; // use shape to draw an oval or rectangle
public void paintComponent( Graphics g )
{
super.paintComponent( g ); if ( shape == CIRCLE )
g.fillOval( 50, 10, 60, 60 );
else if ( shape == SQUARE )
g.fillRect( 50, 10, 60, 60 );
} // set shape value and repaint CustomPanel
public void draw( int shapeToDraw )
{
shape = shapeToDraw;
repaint();
}} // end class CustomPanelpublic class CustomPanelTest extends JFrame {
private JPanel buttonPanel;
private CustomPanel myPanel;
private JButton circleButton, squareButton;// set up GUI
public CustomPanelTest()
{
super( "CustomPanel Test" ); // create custom drawing area
myPanel = new CustomPanel();
myPanel.setBackground( Color.green ); // set up squareButton
squareButton = new JButton( "Square" );
squareButton.addActionListener( // anonymous inner class to handle squareButton events
new ActionListener() { // draw a square
public void actionPerformed( ActionEvent event )
{
myPanel.draw( CustomPanel.SQUARE );
} } // end anonymous inner class ); // end call to addActionListener circleButton = new JButton( "Circle" );
circleButton.addActionListener( // anonymous inner class to handle circleButton events
new ActionListener() { // draw a circle
public void actionPerformed( ActionEvent event )
{
myPanel.draw( CustomPanel.CIRCLE );
} } // end anonymous inner class ); // end call to addActionListener // set up panel containing buttons
buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout( 1, 2 ) );
buttonPanel.add( circleButton );
buttonPanel.add( squareButton ); // attach button panel & custom drawing area to content pane
Container container = getContentPane();
container.add( myPanel, BorderLayout.CENTER );
container.add( buttonPanel, BorderLayout.SOUTH ); setSize( 300, 150 );
setVisible( true );
}// execute application
public static void main( String args[] )
{
CustomPanelTest application = new CustomPanelTest(); application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}} // end class CustomPanelTest
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{ getUI().update(g, this);
g.setColor(Color.blue);
g.drawString("fuck", getX(), getY());
g.drawRect(getX(), getY(), getWidth() / 10, getHeight() / 10);
g.dispose();
}
}