标题:[求助]请问paintComponent要怎么用?
只看楼主
apodemas
Rank: 1
等 级:新手上路
帖 子:153
专家分:0
注 册:2005-4-22
 问题点数:0 回复次数:3 
[求助]请问paintComponent要怎么用?
JComponet中的paintComponent要怎么用呢?感觉好象和paint差不多,但------: import java.awt.*; import javax.swing.*; class Mycanvas extends JPanel{ public void paintComponet(Graphics g){ super.paintComponent(g); g.setColor(Color.red); g.drawString("a JPanle used as canvas",50,50); } } public class Apt extends JApplet{ Mycanvas canvas; JPanel panel; JButton button; public void init(){ canvas=new Mycanvas();panel=new JPanel(); button=new JButton("ok"); canvas.setVisible(true); panel.add(button);Container con=getContentPane(); con.add(panel,BorderLayout.NORTH); con.add(canvas,BorderLayout.CENTER); } } 书上的例子,结果看起来和这个方法并没什么关系... 文档上写的: Calls the UI delegate's paint method, if the UI delegate is non-null. We pass the delegate a copy of the Graphics object to protect..... 似乎也没写清楚用法,反正是没看明白...
搜索更多相关主题的帖子: void public button import 
2005-10-06 23:25
lcm
Rank: 4
等 级:贵宾
威 望:13
帖 子:140
专家分:0
注 册:2005-2-4
得分:0 

//一段小程序 供参考。。。 import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class ShowColors2 extends JFrame {

private JButton changeColorButton; private Color color = Color.LIGHT_GRAY; private Container container;

public ShowColors2(){ super(" Using JColorChooser "); container = getContentPane(); container.setLayout( new FlowLayout()); changeColorButton = new JButton ("Change Color "); changeColorButton.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent event ){ color = JColorChooser.showDialog(ShowColors2.this, "Choose a color ", color);

if (color == null) color = Color.LIGHT_GRAY; container.setBackground(color);

} }) ; container.add( changeColorButton ); setSize(400,130); setVisible(true);

}

public static void main (String args[] ){ ShowColors2 application = new ShowColors2(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); } }


从 小 事 做 起,脚 踏 实 地 ~
2005-10-07 00:26
lcm
Rank: 4
等 级:贵宾
威 望:13
帖 子:140
专家分:0
注 册:2005-2-4
得分:0 

import java.awt.*; import javax.swing.*;

public class LinesRectsOvals extends JFrame { public LinesRectsOvals(){ super("Drawing lines , rectangles ans ovals "); setSize(400,165); setVisible(true);

} public void paint (Graphics g){ super.paint(g); g.setColor(Color.RED); g.drawLine(5,30,350,30); g.setColor(Color.BLUE); g.drawRect(5,40,90,55); g.fillRect(100,40,90,50);

g.setColor(Color.CYAN); g.fillRoundRect(195,40,90,55,50,50); g.drawRoundRect(290,40,90,55,20,20);

g.setColor(Color.YELLOW); g.draw3DRect(5,100,90,55,true); g.fill3DRect(100,100,90,55,false); g.setColor( Color.MAGENTA); g.drawOval( 195,100,90,55); g.fillOval(290,100,90,55); }

public static void main(String args[]){ LinesRectsOvals application = new LinesRectsOvals(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

} }


从 小 事 做 起,脚 踏 实 地 ~
2005-10-07 00:32
lcm
Rank: 4
等 级:贵宾
威 望:13
帖 子:140
专家分:0
注 册:2005-2-4
得分:0 
呵呵。。眼花了。。看错了。。。

从 小 事 做 起,脚 踏 实 地 ~
2005-10-07 00:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-29461-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054099 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved