首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

在swing上画图的有关问题

2012-01-12 
在swing上画图的问题package test12_2import javax.swing.*import java.awt.*public class test12_2 {p

在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

百度不支持这样

热点排行