package test12_2;import javax.swing.*;
import java.awt.*;public class test12_2 {
public static void main(String[] args)
{
Show s = new Show();
s.setTitle("两个按钮");
s.setVisible(true);
s.setResizable(false);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(300 ,85 );
s.setLocationRelativeTo(null);
}
}class Show extends JFrame
{
Show()
{
setLayout(new BorderLayout());
add(new MyPanel());
}
}class MyPanel extends JPanel
{
DrawButton bt1 = new DrawButton("ok");
DrawButton bt2 = new DrawButton("cancel");

MyPanel()
{
setLayout(new GridLayout(1 , 2 , 10 , 20));
add(bt1);
add(bt2);
}
}class DrawButton extends JButton
{

private String string; DrawButton(String s)
{
string = s;
}

protected void paintComponent(Graphics g)
{
super.paintComponents(g);

g.setColor(Color.BLUE);
g.drawOval(10 , 10 , (int)(0.8 * getWidth()), (int)(0.8* getHeight()));
g.drawString(string, (int)(0.4* getWidth()) ,(int)(0.6 * getHeight()));
}

}   执行代码之后没什么问题但是用鼠标在两个Button之间晃动一下,会出现“杂交”现象,求解!

解决方案 »

  1.   

    没看到你说的现象。setVisible放到最后,不然先显示在左上角,一闪到了中间。
      

  2.   

    http://hi.csdn.net/space-10199676-do-album-picid-990815-goto-down.html
    http://hi.csdn.net/space-10199676-do-album-picid-990816-goto-down.html
      

  3.   

    我这边是ok cancel都放到一起了
      

  4.   

    class DrawButton extends JButton
    {     DrawButton(String s)
        {
            super(s);
        }    protected void paintComponent(Graphics g)
        {
            super.paintComponents(g);        g.setColor(Color.BLUE);
            g.drawOval(10 , 10 , (int)(0.8 * getWidth()), (int)(0.8* getHeight()));
            g.drawString(getText(), (int)(0.4* getWidth()) ,(int)(0.6 * getHeight()));
        }}
      

  5.   

    楼上兄弟,执行结果还是两个button的文字都挤在一起了,和一开始一样似乎
      

  6.   

    你得界面渲染似乎不对吧。重载DrawButton.paint(Graphics)方法试试:
    把DrawButton.paintComponent方法名更名为 paint 就可以了。
      

  7.   

    请9楼把运行结果的截图贴出来看下。我仔细看了你们的代码,难道是我记错了?DrawButton对象还没画出来,就开始在上面搞点名堂出来,界面应该就是LZ贴出来的图片那样的吧:
    http://hi.csdn.net/space-10199676-do-album-picid-990816-goto-down.html
    这不是一个正确的结果吧
      

  8.   


    还要将super.paintComponents(g);改为super.paint(g);
      

  9.   

    我把代码修改了下,跑了一遍。从13楼贴出的截图上看,我还是觉得界面渲染似乎不对,按钮没完整地绘出来。
    我得到的结果是这样的:
    代码是这样的:import javax.swing.*;
    import java.awt.*;public class Test { public static void main(String[] args) {
    Show s = new Show();
    s.setTitle("两个按钮");
    //s.setVisible(true);
    s.setResizable(false);
    s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    s.setSize(300, 85);
    s.setLocationRelativeTo(null);
    s.setVisible(true);
    }}@SuppressWarnings("serial")
    class Show extends JFrame {
    Show() {
    setLayout(new BorderLayout());
    add(new MyPanel());
    }
    }@SuppressWarnings("serial")
    class MyPanel extends JPanel {
    DrawButton bt1 = new DrawButton("ok");
    DrawButton bt2 = new DrawButton("cancel"); MyPanel() {
    setLayout(new GridLayout(1, 2, 10, 20));
    add(bt1);
    add(bt2);
    }
    }@SuppressWarnings("serial")
    class DrawButton extends JButton { private String string; DrawButton(String s) {
    string = s;
    } /*protected void paintComponent(Graphics g) {
    super.paintComponents(g); g.setColor(Color.BLUE);
    g.drawOval(10, 10, (int) (0.8 * getWidth()), (int) (0.8 * getHeight()));
    g.drawString(string, (int) (0.4 * getWidth()), (int) (0.6 * getHeight()));
    }*/ public void paint(Graphics g) {
    super.paint(g); g.setColor(Color.BLUE);
    g.drawOval(10, 10, (int) (0.8 * getWidth()), (int) (0.8 * getHeight()));
    g.drawString(string, (int) (0.4 * getWidth()), (int) (0.6 * getHeight()));
    }}
      

  10.   

    我得截图:
    http://hiphotos.baidu.com/lfp001/pic/item/fb9fecadcbef7609bd5e07d72edda3cc7dd99e8a.jpg
      

  11.   

    椭圆位置问题
    g.drawOval( getWidth()/10 , getHeight()/10 , (int)(0.8 * getWidth()), (int)(0.8* getHeight()));
      

  12.   


    对于逸飞的情况,可能是版本问题。 
    我和19楼一样,我查了一下paint和paintComponent的区别:piant: 绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。 paintComponent:绘制此容器中的每个组件。 19楼怎么想到覆写paint()的
      

  13.   

    override paint是 AWT 的做法。
      

  14.   

     /*protected void paintComponent(Graphics g) {
            super.paintComponents(g);        g.setColor(Color.BLUE);
            g.drawOval(10, 10, (int) (0.8 * getWidth()), (int) (0.8 * getHeight()));
            g.drawString(string, (int) (0.4 * getWidth()), (int) (0.6 * getHeight()));
        }*/    public void paint(Graphics g) {
            super.paint(g);        g.setColor(Color.BLUE);
            g.drawOval(10, 10, (int) (0.8 * getWidth()), (int) (0.8 * getHeight()));
            g.drawString(string, (int) (0.4 * getWidth()), (int) (0.6 * getHeight()));
        }有人说:
      paint是全部东西都要重画的 
      paintComponent只是重画上面的容器 
    是因为我用了drawOval和drawString方法么所以需要用paint而不是paintComponent
      

  15.   

    恩,button上带文字,再在button上画个圈,字儿再圈里面。
      

  16.   

    我上传了一个定制swing的演示