怎么在一个JButton上画一个三角形 用什么方法都可以 我最想看的是重写paintComponent谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.*;import java.awt.event.*;import java.awt.*;class Mypane extends JPanel implements ActionListener{ JButton rect,ovel,regl; int count=0; JPanel Mypane2; public Mypane() { rect = new JButton("矩形"); ovel = new JButton("椭圆"); regl = new JButton("三角形"); //--------Mypane1---------- JPanel Mypane1=new JPanel(); Mypane1.add(rect); Mypane1.add(ovel); Mypane1.add(regl); //--------Mypane2------------- Mypane2=new JPanel(); Mypane2.setLayout(new GridLayout(3,3)); //------------------------- this.setLayout(new BorderLayout()); add(Mypane1,BorderLayout.NORTH); add(Mypane2,BorderLayout.CENTER); //------------------------------ rect.addActionListener(this); ovel.addActionListener(this); regl.addActionListener(this); } public void actionPerformed(ActionEvent e) { Triangle tt=new Triangle(); Rectangl rr=new Rectangl(); ellipse ee =new ellipse(); if(e.getSource() == regl && count < 9) { Mypane2.add(tt); count++; Mypane2.updateUI(); } if(e.getSource() == ovel && count < 9) { Mypane2.add(rr); count++; Mypane2.updateUI();//更新图形界面 } if(e.getSource() == rect && count < 9) { Mypane2.add(ee); count++; Mypane2.updateUI(); } } public static void main(String args[]) { JFrame Myframe=new JFrame(""); Myframe.getContentPane().add(new Mypane()); Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Myframe.setSize(300,300); Myframe.setVisible(true); } class Triangle extends JPanel { public void paintComponent(Graphics g) { Polygon p=new Polygon(); p.addPoint(0,20); p.addPoint(40,0); p.addPoint(40,40); g.setColor(Color.red); g.fillPolygon(p); }}class Rectangl extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.green); g.fillRect(20,20,40,40); } }class ellipse extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.blue); g.fillOval(20,20,40,40); } } } import java.awt.*;import java.awt.event.*;import javax.swing.*;class Kao extends JFrame implements ActionListener{ JPanel btpl=new JPanel(); JPanel tuply=new JPanel(); JPanel p[]=new JPanel[9]; JButton b1=new JButton("圆"); JButton b2=new JButton("角"); JButton b3=new JButton("方"); int y=0; public static void main(String args[]){ Kao tu=new Kao(); tu.setVisible(true); } Kao(){ setTitle("Draw");setSize(300,350); Container con=getContentPane(); con.add(btpl,BorderLayout.NORTH); btpl.add(b1);btpl.add(b2);btpl.add(b3); con.add(tuply,BorderLayout.CENTER); tuply.setLayout(new GridLayout(3,3)); for(int a=0;a<9;a++){ p[a]=new JPanel(); tuply.add(p[a]); } b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); }class Py extends JPanel{ public Py(){ super.setSize(100,80); } public void paintComponent(Graphics g){ g.setColor(new Color(16,45,150)); g.fillOval(20,10,60,60); }}class Pj extends JPanel{ public Pj(){ super.setSize(100,80); } public void paintComponent(Graphics g){ g.setColor(new Color(150,16,45)); int px[]={50,20,80};int py[]={10,70,70}; g.fillPolygon(px,py,3); }} class Pf extends JPanel{ public Pf(){ super.setSize(100,80); } public void paintComponent(Graphics g){ g.setColor(new Color(45,150,16)); g.fillRect(20,10,60,60); }} public void actionPerformed(ActionEvent e){ y++; if(y<=9){ if(e.getActionCommand()=="圆"){ p[y-1].add(new Py());repaint(); } if(e.getActionCommand()=="角"){ p[y-1].add(new Pj());repaint(); } if(e.getActionCommand()=="方"){ p[y-1].add(new Pf());repaint(); } } } } class PicButton extends JButton{ private BufferedImage image_over; //鼠标在按钮上的图片 private BufferedImage image_off; //鼠标不在按钮上的图片 private BufferedImage image_pressed; //鼠标按下时的图片 private int buttonWidth; //宽 private int buttonHeight; //高 private int[] pixels; //储存图片数据的数组,用于计算contains private boolean mouseOn; private boolean mousePressed; public PicButton() { mouseOn = false; mousePressed = false; //加载图片 image_over = loadImage("button2.png"); image_off = loadImage("button.png"); image_pressed = loadImage("button3.png"); buttonWidth = image_off.getWidth(); buttonHeight = image_off.getHeight(); //读取图片数据 pixels = new int[buttonWidth * buttonHeight]; //抓取像素数据 PixelGrabber pg = new PixelGrabber(image_off, 0, 0, buttonWidth, buttonHeight, pixels, 0, buttonWidth); try{ pg.grabPixels(); } catch(Exception e) { e.printStackTrace(); } //必须设置!否则会有残影! this.setOpaque(false); this.setPreferredSize(new Dimension(buttonWidth, buttonHeight)); this.addMouseMotionListener(new MouseMotionHandler()); this.addMouseListener(new MouseHandler()); } //读取图片文件 public BufferedImage loadImage(String filename) { File file = new File(filename); if(!file.exists()) return null; try{ return ImageIO.read(file); } catch(IOException e) { e.printStackTrace(); return null; } } //覆盖此方法绘制自定义的图片 public void paintComponent(Graphics g) { g.drawImage(image_off, 0, 0, this); if(mouseOn) g.drawImage(image_over, 0, 0, this); else if(mousePressed) g.drawImage(image_pressed, 0, 0, this); } //覆盖此方法绘制自定义的边框 public void paintBorder(Graphics g) { //不填则不要边框 } public boolean contains(int x, int y) { //不判定的话会越界,在组件之外也会激发这个方法 if(!super.contains(x, y)) return false; int alpha = (pixels[(buttonWidth * y + x)] >> 24) & 0xff; repaint(); if(alpha == 0) { return false; } else { return true; } } //暂时没啥用,留着 class MouseMotionHandler extends MouseMotionAdapter { public void mouseMoved(MouseEvent e) { //mouseOn = true; // repaint(); } } //处理进入,离开图片范围的消息 class MouseHandler extends MouseAdapter { public void mouseExited(MouseEvent e) { mouseOn = false; repaint(); } public void mouseEntered(MouseEvent e) { mouseOn = true; repaint(); } public void mousePressed(MouseEvent e) { mouseOn = false; mousePressed = true; repaint(); } public void mouseReleased(MouseEvent e) { //防止在按钮之外的地方松开鼠标 if(contains(e.getX(), e.getY())) mouseOn = true; else mouseOn = false; mousePressed = false; repaint(); } }} 自己画个图片 然后button设置下icon就可以了吧 关于Java泛型的问题,知道的请进!!! java算法题求解 filter的使用问题 诡异的NoClassDefFoundError 关于文件的问题 大家看看这个广播接受的部分怎么老出错呀! 初学JAVA,问一个程序的做法。 看过来! 散分了!!!到底那个开发工具性价比最好,大家进来讨论!! 一道JAVA笔试题 求助:java的一个小问题???? 产生随机不重复的数,请帮帮我啊
import java.awt.event.*;
import java.awt.*;class Mypane extends JPanel implements ActionListener
{
JButton rect,ovel,regl; int count=0;
JPanel Mypane2; public Mypane()
{
rect = new JButton("矩形");
ovel = new JButton("椭圆");
regl = new JButton("三角形"); //--------Mypane1----------
JPanel Mypane1=new JPanel();
Mypane1.add(rect);
Mypane1.add(ovel);
Mypane1.add(regl);
//--------Mypane2-------------
Mypane2=new JPanel();
Mypane2.setLayout(new GridLayout(3,3));
//-------------------------
this.setLayout(new BorderLayout());
add(Mypane1,BorderLayout.NORTH);
add(Mypane2,BorderLayout.CENTER);
//------------------------------
rect.addActionListener(this);
ovel.addActionListener(this);
regl.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Triangle tt=new Triangle();
Rectangl rr=new Rectangl();
ellipse ee =new ellipse();
if(e.getSource() == regl && count < 9)
{
Mypane2.add(tt);
count++;
Mypane2.updateUI();
}
if(e.getSource() == ovel && count < 9)
{
Mypane2.add(rr);
count++; Mypane2.updateUI();//更新图形界面
}
if(e.getSource() == rect && count < 9)
{
Mypane2.add(ee);
count++;
Mypane2.updateUI();
}
}
public static void main(String args[])
{
JFrame Myframe=new JFrame("");
Myframe.getContentPane().add(new Mypane());
Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Myframe.setSize(300,300);
Myframe.setVisible(true);
}
class Triangle extends JPanel
{ public void paintComponent(Graphics g)
{
Polygon p=new Polygon();
p.addPoint(0,20);
p.addPoint(40,0);
p.addPoint(40,40);
g.setColor(Color.red);
g.fillPolygon(p);
}}
class Rectangl extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.green);
g.fillRect(20,20,40,40);
}
}
class ellipse extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(20,20,40,40);
} }
}
import java.awt.event.*;
import javax.swing.*;class Kao extends JFrame implements ActionListener{
JPanel btpl=new JPanel();
JPanel tuply=new JPanel();
JPanel p[]=new JPanel[9];
JButton b1=new JButton("圆");
JButton b2=new JButton("角");
JButton b3=new JButton("方");
int y=0;
public static void main(String args[]){
Kao tu=new Kao();
tu.setVisible(true);
}
Kao(){
setTitle("Draw");setSize(300,350);
Container con=getContentPane();
con.add(btpl,BorderLayout.NORTH);
btpl.add(b1);btpl.add(b2);btpl.add(b3);
con.add(tuply,BorderLayout.CENTER);
tuply.setLayout(new GridLayout(3,3));
for(int a=0;a<9;a++){
p[a]=new JPanel();
tuply.add(p[a]);
}
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
class Py extends JPanel{
public Py(){
super.setSize(100,80);
}
public void paintComponent(Graphics g){
g.setColor(new Color(16,45,150));
g.fillOval(20,10,60,60);
}
}
class Pj extends JPanel{
public Pj(){
super.setSize(100,80);
}
public void paintComponent(Graphics g){
g.setColor(new Color(150,16,45));
int px[]={50,20,80};int py[]={10,70,70};
g.fillPolygon(px,py,3);
}
}
class Pf extends JPanel{
public Pf(){
super.setSize(100,80);
}
public void paintComponent(Graphics g){
g.setColor(new Color(45,150,16));
g.fillRect(20,10,60,60);
}
}
public void actionPerformed(ActionEvent e){
y++;
if(y<=9){
if(e.getActionCommand()=="圆"){
p[y-1].add(new Py());repaint();
}
if(e.getActionCommand()=="角"){
p[y-1].add(new Pj());repaint();
}
if(e.getActionCommand()=="方"){
p[y-1].add(new Pf());repaint();
}
}
}
}
class PicButton extends JButton
{
private BufferedImage image_over; //鼠标在按钮上的图片
private BufferedImage image_off; //鼠标不在按钮上的图片
private BufferedImage image_pressed; //鼠标按下时的图片
private int buttonWidth; //宽
private int buttonHeight; //高
private int[] pixels; //储存图片数据的数组,用于计算contains
private boolean mouseOn;
private boolean mousePressed;
public PicButton()
{
mouseOn = false;
mousePressed = false;
//加载图片
image_over = loadImage("button2.png");
image_off = loadImage("button.png");
image_pressed = loadImage("button3.png");
buttonWidth = image_off.getWidth();
buttonHeight = image_off.getHeight();
//读取图片数据
pixels = new int[buttonWidth * buttonHeight];
//抓取像素数据
PixelGrabber pg = new PixelGrabber(image_off, 0, 0, buttonWidth,
buttonHeight, pixels,
0, buttonWidth);
try{
pg.grabPixels();
}
catch(Exception e)
{
e.printStackTrace();
}
//必须设置!否则会有残影!
this.setOpaque(false);
this.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
this.addMouseMotionListener(new MouseMotionHandler());
this.addMouseListener(new MouseHandler());
}
//读取图片文件
public BufferedImage loadImage(String filename)
{
File file = new File(filename);
if(!file.exists())
return null;
try{
return ImageIO.read(file);
}
catch(IOException e)
{
e.printStackTrace();
return null;
}
}
//覆盖此方法绘制自定义的图片
public void paintComponent(Graphics g)
{
g.drawImage(image_off, 0, 0, this);
if(mouseOn)
g.drawImage(image_over, 0, 0, this);
else if(mousePressed)
g.drawImage(image_pressed, 0, 0, this);
}
//覆盖此方法绘制自定义的边框
public void paintBorder(Graphics g)
{
//不填则不要边框
}
public boolean contains(int x, int y)
{
//不判定的话会越界,在组件之外也会激发这个方法
if(!super.contains(x, y))
return false;
int alpha = (pixels[(buttonWidth * y + x)] >> 24) & 0xff; repaint();
if(alpha == 0)
{
return false;
}
else
{
return true;
}
}
//暂时没啥用,留着
class MouseMotionHandler extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent e)
{
//mouseOn = true;
// repaint();
} }
//处理进入,离开图片范围的消息
class MouseHandler extends MouseAdapter
{
public void mouseExited(MouseEvent e)
{
mouseOn = false;
repaint();
}
public void mouseEntered(MouseEvent e)
{
mouseOn = true;
repaint();
}
public void mousePressed(MouseEvent e)
{
mouseOn = false;
mousePressed = true;
repaint();
}
public void mouseReleased(MouseEvent e)
{
//防止在按钮之外的地方松开鼠标
if(contains(e.getX(), e.getY()))
mouseOn = true;
else
mouseOn = false;
mousePressed = false;
repaint();
}
}
}