paintComponent的显示调用??

wawa_ai_chengxu 2010-01-04 05:06:48
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.util.Vector;
import java.awt.Point;
import java.awt.color.*;
class J_Panel extends JPanel{
Vector<Point> p= new Vector<Point>();
protected J_Panel(){
JOptionPane.showMessageDialog(null, "请点击面板上你画图形的顶点。");
JButton b=new JButton("确定");
Graphics d=this.getGraphics();
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
super.paintComponent(d); //怎么显示调用啊 ,应该是父组件的paint
}

});
add(b);
addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){
Point temp=new Point(e.getX(),e.getY());
p.add(temp);
}
}
);
}
protected void paintComponent(Graphics g){
int i=0, n=p.size();
Point temp1,temp2;
for(i=0;i<n-1;i++){
temp1=p.get(i);
temp2=p.get(i+1);
g.drawLine(temp1.x, temp1.y, temp2.x, temp2.y);
}
}
}
public class PLine extends JFrame {
public PLine(){
super("图形填充程序");
Container c=getContentPane();
c.setLayout(new BorderLayout());
c.add(new J_Panel(), BorderLayout.CENTER);
//c.setBackground(new Color(Color.green));
}
public static void main(String args[]){
PLine app=new PLine();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(600,400);
app.setVisible(true);
}
}
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2010-01-07
  • 打赏
  • 举报
回复
怎么还是不行啊 ??
「已注销」 2010-01-04
  • 打赏
  • 举报
回复
1.final Graphics d = this.getGraphics();
2.J_Panel.super.paintComponent(d);//就这么调用

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧