完整的代码是这样的
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;public class Main extends JFrame {/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new Main();ovalButton button1 = new ovalButton();
ovalButton button2 = new ovalButton();button1.setText("OK");
button2.setText("CANCEL");frame.add(button1);
frame.add(button2);
frame.setTitle("hello");
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
class ovalButton extends JButton {protected void painComponent(Graphics g){
super.paintComponent(g);g.drawOval(this.getX(), this.getY(), this.getWidth() , this.getHeight());
}
}可是椭圆画不出来啊 - - ,不知道为什么......请指教,哈哈thanks
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;public class Main extends JFrame {/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new Main();ovalButton button1 = new ovalButton();
ovalButton button2 = new ovalButton();button1.setText("OK");
button2.setText("CANCEL");frame.add(button1);
frame.add(button2);
frame.setTitle("hello");
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
class ovalButton extends JButton {protected void painComponent(Graphics g){
super.paintComponent(g);g.drawOval(this.getX(), this.getY(), this.getWidth() , this.getHeight());
}
}可是椭圆画不出来啊 - - ,不知道为什么......请指教,哈哈thanks
改为
protected void paintComponent(Graphics g)不过出来的效果也不是你想要的,因为你只是在按钮上画了个椭圆而已.
楼主可以这样做,用一个椭圆图片做背景的<div>加上onclick,