问在JPanel上写字符的问题 是不是在paintComponent()中调用了repaint()? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的程序import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.border.*;import java.util.*;public class Draw extends JFrame{ final JLabel lblshape = new JLabel("Shape Color"); final JLabel lblback = new JLabel("Background"); final JLabel lbltext = new JLabel("Text"); final JLabel lblsize = new JLabel("Size"); final JLabel lblaction = new JLabel("no action"); final JTextField txt = new JTextField("Type Text Here"); final JSpinner spsize = new JSpinner(); final JCheckBox chfill = new JCheckBox("Fill the region"); int drawindex = 0; int shapeindex = 0; int backindex = 0; boolean fill = false; Color[] clist = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; //combobox final JComboBox cbshape = new JComboBox(clist){ public void paintComponent(Graphics g){ super.paintComponent(g); int index = getSelectedIndex(); g.setColor(clist[index]); Dimension t = getSize(); g.fillRect(2, 2, t.width - 4, t.height - 4); } }; final JComboBox cbback = new JComboBox(clist){ public void paintComponent(Graphics g){ super.paintComponent(g); int index = getSelectedIndex(); g.setColor(clist[index]); Dimension t = getSize(); g.fillRect(2, 2, t.width - 4, t.height - 4); } }; class DataRenderer extends JLabel implements ListCellRenderer { public DataRenderer() { setOpaque(true); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean hasFocus) { if ( isSelected ) { setBorder( BorderFactory.createLineBorder(Color.red) ); } else { setBorder( BorderFactory.createLineBorder(Color.DARK_GRAY) ); } Color t = (Color)value; setBackground(t); return this; } } //togglebutton final JToggleButton jbround = new JToggleButton(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); Dimension t = getSize(); g.drawOval(2, 2, t.width - 4, t.height - 4); } }; final JToggleButton jboval = new JToggleButton(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); Dimension t = getSize(); g.drawOval(2, 10, t.width - 4, t.height - 18); } }; final JToggleButton jbrect = new JToggleButton(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); Dimension t = getSize(); g.drawRect(10, 10, t.width - 20, t.height - 20); } }; final JToggleButton jbline = new JToggleButton(){ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); Dimension t = getSize(); g.drawLine(8, 8, t.width - 16, t.height - 16); } }; final JToggleButton jbtext = new JToggleButton("A"); //设置 public Draw(){ super("Draw"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try{ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }catch(Exception ex){ ex.printStackTrace(System.err); } setSize(735, 570); //font Font tf = new Font("宋体", Font.BOLD, 19); Font af = new Font("宋体", Font.BOLD | Font.ITALIC, 30); lblshape.setFont(tf); lblback.setFont(tf); lbltext.setFont(tf); lblsize.setFont(tf); txt.setFont(tf); chfill.setFont(tf); jbtext.setFont(af); //jcombobox DataRenderer renderer = new DataRenderer(); renderer.setPreferredSize(new Dimension(70, 30)); cbshape.setRenderer(renderer); cbback.setRenderer(renderer); //size cbshape.setPreferredSize(new Dimension(70,30)); cbback.setPreferredSize(new Dimension(70,30)); spsize.setPreferredSize(new Dimension(70,30)); //menu JMenuBar menubar = new JMenuBar(); JMenu mfile = new JMenu("File"); mfile.setMnemonic(KeyEvent.VK_F); JMenu medit = new JMenu("Edit"); medit.setMnemonic(KeyEvent.VK_E); JMenu mhelp = new JMenu("Help"); mhelp.setMnemonic(KeyEvent.VK_H); menubar.add(mfile); menubar.add(medit); menubar.add(mhelp); setJMenuBar(menubar); //Layout Container cp = getContentPane(); JPanel pinfo = new JPanel(); pinfo.setLayout(new GridLayout(2, 1)); JPanel pshap = new JPanel(); pshap.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5)); pshap.add(lblshape); pshap.add(cbshape); pshap.add(Box.createHorizontalStrut(150)); pshap.add(lbltext); pshap.add(txt); JPanel pback = new JPanel(); pback.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5)); pback.add(lblback); pback.add(Box.createHorizontalStrut(2)); pback.add(cbback); pback.add(Box.createHorizontalStrut(150)); pback.add(lblsize); pback.add(spsize); pback.add(chfill); pinfo.add(pshap); pinfo.add(pback); pinfo.setBorder(new EtchedBorder()); cp.add(BorderLayout.NORTH, pinfo); //加入绘画按钮 JPanel pgraphics = new JPanel(); ButtonGroup bg = new ButtonGroup(); bg.add(jbround); bg.add(jboval); bg.add(jbline); bg.add(jbrect); bg.add(jbtext); pgraphics.setPreferredSize(new Dimension(55, 10)); pgraphics.setLayout(new GridLayout(8, 1, 5, 3)); pgraphics.add(jbround); pgraphics.add(jboval); pgraphics.add(jbline); pgraphics.add(jbrect); pgraphics.add(jbtext); pgraphics.setBorder(new EtchedBorder()); cp.add(BorderLayout.WEST, pgraphics); //绘画区 Mypane paint = new Mypane(); paint.setBorder(new EtchedBorder()); cp.add(BorderLayout.CENTER, paint); cp.add(BorderLayout.SOUTH, lblaction); } void addlistener(){ //绘图按钮的listener jbround.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ drawindex = 0; } }); jboval.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ drawindex = 1; } }); jbline.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ drawindex = 2; } }); jbrect.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ drawindex = 3; } }); jbtext.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ drawindex = 4; } }); } class Mypane extends JPanel{ private int xs = 0; private int ys = 0; private int xt = 0; private int yt = 0; private int xd = 0; private int yd = 0; private int xf = 0; private int yf = 0; boolean hadfirstdraw = false; boolean hadfinish = false; //用来记录画过的图形 private LinkedList linelists = new LinkedList(); //起点 private LinkedList linelistd = new LinkedList(); //终点 private LinkedList linelistc = new LinkedList(); //前景色 //private LinkedList linelistb = new LinkedList(); //是否填充 //private LinkedList linelistg = new LinkedList(); //背景色 private LinkedList roundlists = new LinkedList(); private LinkedList roundlistd = new LinkedList(); private LinkedList roundlistc = new LinkedList(); private LinkedList roundlistb = new LinkedList(); private LinkedList roundlistg = new LinkedList(); private LinkedList ovallists = new LinkedList(); private LinkedList ovallistd = new LinkedList(); private LinkedList ovallistc = new LinkedList(); private LinkedList ovallistg = new LinkedList(); private LinkedList ovallistb = new LinkedList(); private LinkedList rectlists = new LinkedList(); private LinkedList rectlistd = new LinkedList(); private LinkedList rectlistc = new LinkedList(); private LinkedList rectlistb = new LinkedList(); private LinkedList rectlistg = new LinkedList(); private LinkedList strlists = new LinkedList(); //保存字符串 //private LinkedList strlistd = new LinkedList(); private LinkedList strlist = new LinkedList(); private LinkedList strlistsize = new LinkedList(); private LinkedList strlistc = new LinkedList(); MouseListener ml = new MouseListener(){ public void mousePressed(MouseEvent e){ shapeindex = cbshape.getSelectedIndex(); backindex = cbback.getSelectedIndex(); fill = chfill.isSelected(); xs = e.getX(); ys = e.getY(); hadfirstdraw = false; hadfinish = false; } public void mouseReleased(MouseEvent e){ xd = e.getX(); yd = e.getY(); hadfinish = true; switch(drawindex){ case 0: roundlists.add(new Point(xs, ys)); roundlistd.add(new Point(xd, yd)); roundlistc.add(new Integer(shapeindex)); roundlistg.add(new Integer(backindex)); roundlistb.add(new Boolean(fill)); break; case 1: ovallists.add(new Point(xs, ys)); ovallistd.add(new Point(xd, yd)); ovallistc.add(new Integer(shapeindex)); ovallistg.add(new Integer(backindex)); ovallistb.add(new Boolean(fill)); break; case 2: linelists.add(new Point(xs, ys)); linelistd.add(new Point(xd, yd)); linelistc.add(new Integer(shapeindex)); break; case 3: rectlists.add(new Point(xs, ys)); rectlistd.add(new Point(xd, yd)); rectlistc.add(new Integer(shapeindex)); rectlistg.add(new Integer(backindex)); rectlistb.add(new Boolean(fill)); break; case 4: strlist.add(txt.getText()); strlists.add(new Point(xd, yd)); strlistc.add(new Integer(shapeindex)); strlistsize.add(spsize.getValue()); break; } repaint(); } public void mouseClicked(MouseEvent e){return;} public void mouseEntered(MouseEvent e){return;} public void mouseExited(MouseEvent e) {return;} }; MouseMotionListener mml = new MouseMotionListener() { public void mouseDragged(MouseEvent e) { xt = e.getX(); yt = e.getY(); repaint(); } public void mouseMoved(MouseEvent e) {} }; public Mypane(){ setOpaque(true); addMouseListener(ml); addMouseMotionListener(mml); } public void paintComponent(Graphics g ){ super.paintComponent(g); g.setXORMode(getBackground()); g.setColor(clist[shapeindex]); System.out.println("ddddddddddddddddddd"); switch(drawindex){ case 0: g.drawOval(xs, ys, Math.min(xt - xs, yt - ys), Math.min(xt - xs, yt - ys)); if(fill){ g.setColor(clist[backindex]); g.fillOval(xs, ys, Math.min(xt - xs, yt - ys), Math.min(xt - xs, yt - ys)); } break; case 1: g.drawOval(xs, ys, xt - xs, yt - ys); if(fill){ g.setColor(clist[backindex]); g.fillOval(xs, ys, xt - xs, yt - ys); } break; case 2: g.drawLine(xs, ys, xt, yt); break; case 3: g.drawRect(xs, ys, xt - xs, yt - ys); if(fill){ g.setColor(clist[backindex]); g.fillRect(xs, ys, xt - xs, yt - ys); } break; case 4: Font newf = new Font("宋体", Font.BOLD, ((Integer)spsize.getValue()).intValue()); setFont(newf); g.drawString(txt.getText(), xs, ys); } hadfirstdraw = true; Point s2; Point d; int id; int gr; boolean f; super.paintComponent(g); g.setPaintMode(); for(int i = 0; i < roundlists.size(); i++){ s2 = (Point)roundlists.get(i); d = (Point)roundlistd.get(i); id = ((Integer)roundlistc.get(i)).intValue(); f = ((Boolean)roundlistb.get(i)).booleanValue(); gr = ((Integer)roundlistg.get(i)).intValue(); g.setColor(clist[id]); g.drawOval(s2.x, s2.y, Math.min(d.x - s2.x, d.y - s2.y), Math.min(d.x - s2.x, d.y - s2.y)); if(f){ g.setColor(clist[gr]); g.fillOval(s2.x, s2.y, Math.min(d.x - s2.x, d.y - s2.y), Math.min(d.x - s2.x, d.y - s2.y)); } } for(int i = 0; i < ovallists.size(); i++){ s2 = (Point)ovallists.get(i); d = (Point)ovallistd.get(i); id = ((Integer)ovallistc.get(i)).intValue(); f = ((Boolean)ovallistb.get(i)).booleanValue(); gr = ((Integer)ovallistg.get(i)).intValue(); g.setColor(clist[id]); g.drawOval(s2.x, s2.y, d.x - s2.x, d.y - s2.y); if(f){ g.setColor(clist[gr]); g.fillOval(s2.x, s2.y, d.x - s2.x, d.y - s2.y); } } for(int i = 0; i < linelists.size(); i++){ s2 = (Point)linelists.get(i); d = (Point)linelistd.get(i); id = ((Integer)linelistc.get(i)).intValue(); g.setColor(clist[id]); g.drawLine(s2.x, s2.y, d.x, d.y); } for(int i = 0; i < rectlists.size(); i++){ s2 = (Point)rectlists.get(i); d = (Point)rectlistd.get(i); id = ((Integer)rectlistc.get(i)).intValue(); f = ((Boolean)rectlistb.get(i)).booleanValue(); gr = ((Integer)rectlistg.get(i)).intValue(); g.setColor(clist[id]); g.drawRect(s2.x, s2.y, d.x - s2.x, d.y - s2.y); if(f){ g.setColor(clist[gr]); g.fillRect(s2.x, s2.y, d.x - s2.x, d.y - s2.y); } } for(int i = 0; i < strlist.size(); i++){ s2 = (Point)strlists.get(i); id = ((Integer)strlistc.get(i)).intValue(); g.setColor(clist[id]); Font nf = new Font("宋体", Font.BOLD, ((Integer)strlistsize.get(i)).intValue()); setFont(nf); g.drawString((String)strlist.get(i), s2.x, s2.y); } } } public static void main(String[] args) { Draw instant = new Draw(); instant.addlistener(); instant.setVisible(true); }} 上面两个帖子是一个程序,一个帖子装不下,问题就出在点击左边的“A”按钮后,再绘图板上点一下鼠标,就会看到paintComponent不断被调用,(命令行一直显示“dddddddddddddddd”),我通过在我写的repaint函数前加入println();来判断是哪个repaint引起的paintComponent不断被调用,结果是:哪个都不是,我实在想不明白了,所有来请教各位高手了。 System.out.println("ddddddddddddddddddd");是在命令行下输出啦建议楼主找本关于界面设计的书看看《JFC核心编程》就不错 我知道System.out.println("ddddddddddddddddddd");是在命令行下输出啦我是为了在命令行输入,来看看paintComponent是否被调用。 把你的paintCompoent()方法里的两句setFont(newf); 和 setFont(nf);改成 g.setFont(newf); 和 g.setFont(nf);setFont()方法是用来改变组件的字体的,它要调repaint()方法,这样就导致你的程序不断的重画了。 java与QQ的那点事 java连接远程access问题 。。。。。 谈爱情故事,谈观察者设计模式[转] 问一个正则表达式 请问:xpath,xquery这些技术现在用的多吗? 怎么能输入一个000x格式的字符串 菜鸟问:JDK1.5.0\bin里面的那些可执行小东西怎么用呀? 除了JCREATOR PRO外还有什么好的JAVA编辑器 RMI服务端和客户端在同一太机器上成功,在不同的机器上不成功 为什么运算出来的和我想的不一样? 一个servlet运行问题,大家帮我看看 有懂jsf的人吗,帮兄弟一个忙,谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;public class Draw extends JFrame{ final JLabel lblshape = new JLabel("Shape Color");
final JLabel lblback = new JLabel("Background");
final JLabel lbltext = new JLabel("Text");
final JLabel lblsize = new JLabel("Size");
final JLabel lblaction = new JLabel("no action");
final JTextField txt = new JTextField("Type Text Here");
final JSpinner spsize = new JSpinner();
final JCheckBox chfill = new JCheckBox("Fill the region");
int drawindex = 0;
int shapeindex = 0;
int backindex = 0;
boolean fill = false; Color[] clist = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; //combobox
final JComboBox cbshape = new JComboBox(clist){
public void paintComponent(Graphics g){
super.paintComponent(g);
int index = getSelectedIndex();
g.setColor(clist[index]);
Dimension t = getSize();
g.fillRect(2, 2, t.width - 4, t.height - 4);
}
}; final JComboBox cbback = new JComboBox(clist){
public void paintComponent(Graphics g){
super.paintComponent(g);
int index = getSelectedIndex();
g.setColor(clist[index]);
Dimension t = getSize();
g.fillRect(2, 2, t.width - 4, t.height - 4);
}
}; class DataRenderer extends JLabel implements ListCellRenderer { public DataRenderer() {
setOpaque(true);
} public Component getListCellRendererComponent( JList list,
Object value,
int index,
boolean isSelected,
boolean hasFocus) {
if ( isSelected ) {
setBorder( BorderFactory.createLineBorder(Color.red) );
}
else {
setBorder( BorderFactory.createLineBorder(Color.DARK_GRAY) );
} Color t = (Color)value;
setBackground(t); return this;
}
} //togglebutton
final JToggleButton jbround = new JToggleButton(){
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
Dimension t = getSize();
g.drawOval(2, 2, t.width - 4, t.height - 4);
}
}; final JToggleButton jboval = new JToggleButton(){
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
Dimension t = getSize();
g.drawOval(2, 10, t.width - 4, t.height - 18);
}
}; final JToggleButton jbrect = new JToggleButton(){
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
Dimension t = getSize();
g.drawRect(10, 10, t.width - 20, t.height - 20);
}
}; final JToggleButton jbline = new JToggleButton(){
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
Dimension t = getSize();
g.drawLine(8, 8, t.width - 16, t.height - 16);
}
}; final JToggleButton jbtext = new JToggleButton("A");
//设置
public Draw(){
super("Draw");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception ex){
ex.printStackTrace(System.err);
} setSize(735, 570); //font
Font tf = new Font("宋体", Font.BOLD, 19);
Font af = new Font("宋体", Font.BOLD | Font.ITALIC, 30);
lblshape.setFont(tf);
lblback.setFont(tf);
lbltext.setFont(tf);
lblsize.setFont(tf);
txt.setFont(tf);
chfill.setFont(tf);
jbtext.setFont(af); //jcombobox
DataRenderer renderer = new DataRenderer();
renderer.setPreferredSize(new Dimension(70, 30));
cbshape.setRenderer(renderer);
cbback.setRenderer(renderer); //size
cbshape.setPreferredSize(new Dimension(70,30));
cbback.setPreferredSize(new Dimension(70,30));
spsize.setPreferredSize(new Dimension(70,30));
//menu
JMenuBar menubar = new JMenuBar();
JMenu mfile = new JMenu("File");
mfile.setMnemonic(KeyEvent.VK_F);
JMenu medit = new JMenu("Edit");
medit.setMnemonic(KeyEvent.VK_E);
JMenu mhelp = new JMenu("Help");
mhelp.setMnemonic(KeyEvent.VK_H);
menubar.add(mfile);
menubar.add(medit);
menubar.add(mhelp);
setJMenuBar(menubar); //Layout
Container cp = getContentPane();
JPanel pinfo = new JPanel();
pinfo.setLayout(new GridLayout(2, 1)); JPanel pshap = new JPanel();
pshap.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
pshap.add(lblshape);
pshap.add(cbshape);
pshap.add(Box.createHorizontalStrut(150));
pshap.add(lbltext);
pshap.add(txt); JPanel pback = new JPanel();
pback.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
pback.add(lblback);
pback.add(Box.createHorizontalStrut(2));
pback.add(cbback);
pback.add(Box.createHorizontalStrut(150));
pback.add(lblsize);
pback.add(spsize);
pback.add(chfill); pinfo.add(pshap);
pinfo.add(pback);
pinfo.setBorder(new EtchedBorder());
cp.add(BorderLayout.NORTH, pinfo); //加入绘画按钮
JPanel pgraphics = new JPanel();
ButtonGroup bg = new ButtonGroup();
bg.add(jbround);
bg.add(jboval);
bg.add(jbline);
bg.add(jbrect);
bg.add(jbtext);
pgraphics.setPreferredSize(new Dimension(55, 10));
pgraphics.setLayout(new GridLayout(8, 1, 5, 3));
pgraphics.add(jbround);
pgraphics.add(jboval);
pgraphics.add(jbline);
pgraphics.add(jbrect);
pgraphics.add(jbtext);
pgraphics.setBorder(new EtchedBorder()); cp.add(BorderLayout.WEST, pgraphics); //绘画区
Mypane paint = new Mypane();
paint.setBorder(new EtchedBorder()); cp.add(BorderLayout.CENTER, paint);
cp.add(BorderLayout.SOUTH, lblaction);
} void addlistener(){ //绘图按钮的listener
jbround.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawindex = 0;
}
}); jboval.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawindex = 1;
}
}); jbline.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawindex = 2;
}
}); jbrect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawindex = 3;
}
}); jbtext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawindex = 4;
}
});
}
private int ys = 0;
private int xt = 0;
private int yt = 0;
private int xd = 0;
private int yd = 0;
private int xf = 0;
private int yf = 0;
boolean hadfirstdraw = false;
boolean hadfinish = false;
//用来记录画过的图形
private LinkedList linelists = new LinkedList(); //起点
private LinkedList linelistd = new LinkedList(); //终点
private LinkedList linelistc = new LinkedList(); //前景色
//private LinkedList linelistb = new LinkedList(); //是否填充
//private LinkedList linelistg = new LinkedList(); //背景色 private LinkedList roundlists = new LinkedList();
private LinkedList roundlistd = new LinkedList();
private LinkedList roundlistc = new LinkedList();
private LinkedList roundlistb = new LinkedList();
private LinkedList roundlistg = new LinkedList(); private LinkedList ovallists = new LinkedList();
private LinkedList ovallistd = new LinkedList();
private LinkedList ovallistc = new LinkedList();
private LinkedList ovallistg = new LinkedList();
private LinkedList ovallistb = new LinkedList(); private LinkedList rectlists = new LinkedList();
private LinkedList rectlistd = new LinkedList();
private LinkedList rectlistc = new LinkedList();
private LinkedList rectlistb = new LinkedList();
private LinkedList rectlistg = new LinkedList(); private LinkedList strlists = new LinkedList(); //保存字符串
//private LinkedList strlistd = new LinkedList();
private LinkedList strlist = new LinkedList();
private LinkedList strlistsize = new LinkedList();
private LinkedList strlistc = new LinkedList(); MouseListener ml = new MouseListener(){
public void mousePressed(MouseEvent e){
shapeindex = cbshape.getSelectedIndex();
backindex = cbback.getSelectedIndex();
fill = chfill.isSelected(); xs = e.getX();
ys = e.getY();
hadfirstdraw = false;
hadfinish = false;
} public void mouseReleased(MouseEvent e){
xd = e.getX();
yd = e.getY();
hadfinish = true; switch(drawindex){
case 0: roundlists.add(new Point(xs, ys));
roundlistd.add(new Point(xd, yd));
roundlistc.add(new Integer(shapeindex));
roundlistg.add(new Integer(backindex));
roundlistb.add(new Boolean(fill));
break;
case 1: ovallists.add(new Point(xs, ys));
ovallistd.add(new Point(xd, yd));
ovallistc.add(new Integer(shapeindex));
ovallistg.add(new Integer(backindex));
ovallistb.add(new Boolean(fill));
break;
case 2: linelists.add(new Point(xs, ys));
linelistd.add(new Point(xd, yd));
linelistc.add(new Integer(shapeindex));
break;
case 3: rectlists.add(new Point(xs, ys));
rectlistd.add(new Point(xd, yd));
rectlistc.add(new Integer(shapeindex));
rectlistg.add(new Integer(backindex));
rectlistb.add(new Boolean(fill));
break;
case 4: strlist.add(txt.getText());
strlists.add(new Point(xd, yd));
strlistc.add(new Integer(shapeindex));
strlistsize.add(spsize.getValue());
break;
}
repaint();
} public void mouseClicked(MouseEvent e){return;}
public void mouseEntered(MouseEvent e){return;}
public void mouseExited(MouseEvent e) {return;}
}; MouseMotionListener mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
xt = e.getX();
yt = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
}; public Mypane(){
setOpaque(true);
addMouseListener(ml);
addMouseMotionListener(mml); } public void paintComponent(Graphics g ){
super.paintComponent(g);
g.setXORMode(getBackground());
g.setColor(clist[shapeindex]); System.out.println("ddddddddddddddddddd");
switch(drawindex){
case 0: g.drawOval(xs, ys, Math.min(xt - xs, yt - ys), Math.min(xt - xs, yt - ys));
if(fill){
g.setColor(clist[backindex]);
g.fillOval(xs, ys, Math.min(xt - xs, yt - ys), Math.min(xt - xs, yt - ys));
}
break;
case 1: g.drawOval(xs, ys, xt - xs, yt - ys);
if(fill){
g.setColor(clist[backindex]);
g.fillOval(xs, ys, xt - xs, yt - ys);
}
break;
case 2: g.drawLine(xs, ys, xt, yt);
break;
case 3: g.drawRect(xs, ys, xt - xs, yt - ys);
if(fill){
g.setColor(clist[backindex]);
g.fillRect(xs, ys, xt - xs, yt - ys);
}
break;
case 4: Font newf = new Font("宋体", Font.BOLD, ((Integer)spsize.getValue()).intValue());
setFont(newf);
g.drawString(txt.getText(), xs, ys);
} hadfirstdraw = true; Point s2;
Point d;
int id;
int gr;
boolean f; super.paintComponent(g);
g.setPaintMode();
for(int i = 0; i < roundlists.size(); i++){
s2 = (Point)roundlists.get(i);
d = (Point)roundlistd.get(i);
id = ((Integer)roundlistc.get(i)).intValue();
f = ((Boolean)roundlistb.get(i)).booleanValue();
gr = ((Integer)roundlistg.get(i)).intValue(); g.setColor(clist[id]);
g.drawOval(s2.x, s2.y, Math.min(d.x - s2.x, d.y - s2.y), Math.min(d.x - s2.x, d.y - s2.y));
if(f){
g.setColor(clist[gr]);
g.fillOval(s2.x, s2.y, Math.min(d.x - s2.x, d.y - s2.y), Math.min(d.x - s2.x, d.y - s2.y));
}
}
for(int i = 0; i < ovallists.size(); i++){
s2 = (Point)ovallists.get(i);
d = (Point)ovallistd.get(i);
id = ((Integer)ovallistc.get(i)).intValue();
f = ((Boolean)ovallistb.get(i)).booleanValue();
gr = ((Integer)ovallistg.get(i)).intValue(); g.setColor(clist[id]);
g.drawOval(s2.x, s2.y, d.x - s2.x, d.y - s2.y);
if(f){
g.setColor(clist[gr]);
g.fillOval(s2.x, s2.y, d.x - s2.x, d.y - s2.y);
}
}
for(int i = 0; i < linelists.size(); i++){
s2 = (Point)linelists.get(i);
d = (Point)linelistd.get(i);
id = ((Integer)linelistc.get(i)).intValue();
g.setColor(clist[id]);
g.drawLine(s2.x, s2.y, d.x, d.y);
}
for(int i = 0; i < rectlists.size(); i++){
s2 = (Point)rectlists.get(i);
d = (Point)rectlistd.get(i);
id = ((Integer)rectlistc.get(i)).intValue();
f = ((Boolean)rectlistb.get(i)).booleanValue();
gr = ((Integer)rectlistg.get(i)).intValue(); g.setColor(clist[id]);
g.drawRect(s2.x, s2.y, d.x - s2.x, d.y - s2.y);
if(f){
g.setColor(clist[gr]);
g.fillRect(s2.x, s2.y, d.x - s2.x, d.y - s2.y);
}
}
for(int i = 0; i < strlist.size(); i++){
s2 = (Point)strlists.get(i);
id = ((Integer)strlistc.get(i)).intValue();
g.setColor(clist[id]);
Font nf = new Font("宋体", Font.BOLD, ((Integer)strlistsize.get(i)).intValue());
setFont(nf);
g.drawString((String)strlist.get(i), s2.x, s2.y);
} } } public static void main(String[] args)
{
Draw instant = new Draw();
instant.addlistener();
instant.setVisible(true);
}
}
问题就出在点击左边的“A”按钮后,再绘图板上点一下鼠标,就会看到paintComponent不断被调用,(命令行一直显示“dddddddddddddddd”),
我通过在我写的repaint函数前加入println();来判断是哪个repaint引起的paintComponent不断被调用,结果是:哪个都不是,我实在想不明白了,所有来请教各位高手了。
是在命令行下输出啦建议楼主找本关于界面设计的书看看
《JFC核心编程》就不错
System.out.println("ddddddddddddddddddd");
是在命令行下输出啦我是为了在命令行输入,来看看paintComponent是否被调用。
setFont(newf); 和 setFont(nf);
改成 g.setFont(newf); 和 g.setFont(nf);setFont()方法是用来改变组件的字体的,它要调repaint()方法,这样就导致你的程序不断的重画了。