当前位置: 代码迷 >> J2SE >> 在swing上画图的有关问题
  详细解决方案

在swing上画图的有关问题

热度:59   发布时间:2016-04-24 02:16:18.0
在swing上画图的问题
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之间晃动一下,会出现“杂交”现象,求解!



------解决方案--------------------
Java code
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()));    }}
------解决方案--------------------
探讨

引用:

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

百度不支持这样