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之间晃动一下,会出现“杂交”现象,求解!
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之间晃动一下,会出现“杂交”现象,求解!
http://hi.csdn.net/space-10199676-do-album-picid-990816-goto-down.html
{ 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()));
}}
把DrawButton.paintComponent方法名更名为 paint 就可以了。
http://hi.csdn.net/space-10199676-do-album-picid-990816-goto-down.html
这不是一个正确的结果吧
还要将super.paintComponents(g);改为super.paint(g);
我得到的结果是这样的:
代码是这样的: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()));
}}
http://hiphotos.baidu.com/lfp001/pic/item/fb9fecadcbef7609bd5e07d72edda3cc7dd99e8a.jpg
g.drawOval( getWidth()/10 , getHeight()/10 , (int)(0.8 * getWidth()), (int)(0.8* getHeight()));
对于逸飞的情况,可能是版本问题。
我和19楼一样,我查了一下paint和paintComponent的区别:piant: 绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。 paintComponent:绘制此容器中的每个组件。 19楼怎么想到覆写paint()的
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