【紧急】【需java完整代码】关于JButton

如何创建一个JButton,这个JButton的外观需要用paintComponent中的fill或draw函数来画,具体样子是从左下角到右上角画一条线分成两个三角形,右边三角形的颜色是蓝色。然后将该JButton在JFrame中显示出来。
求完整的可在eclipse中执行的代码

我自己写了个,不过这种东西是有点难度的,必须对Swing的MVC模式有所了解,并且要对Swing类层次结构有所了解,当然如果要效果非常好得深入钻研,我只是略懂,所以大概实现了下,代码如下:(可直接运行)

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.plaf.basic.BasicButtonUI;

public class ButtonDemo extends JFrame {

private JButton bt = null;

public ButtonDemo() {

super("Button Demo");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation(50, 50);

bt = new JButton("按钮");

this.add(bt);

Graphics g = bt.getGraphics();

MyButtonUI buttonUI = new MyButtonUI(g, bt);

bt.setUI(buttonUI);

this.pack();

}

public static void main(String[] args) {

new ButtonDemo().setVisible(true);

}

}

class MyButtonUI extends BasicButtonUI {

private Graphics g;

private JComponent c;

public MyButtonUI() {

}

public MyButtonUI(Graphics g, JComponent c) {

this.g = g;

this.c = c;

}

public void update(Graphics g, JComponent c) {

if (c.isOpaque()) {

g.setColor(c.getBackground());

g.fillRect(0, 0, c.getWidth(), c.getHeight());

}

paint(g, c);

}

public void paint(Graphics g, JComponent c) {

int w = c.getWidth(); // 获取这个组件的宽

int h = c.getHeight(); // 获取这个组件的高

Graphics2D g2d = (Graphics2D) g;

// 设置抗锯齿

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int[] x1 = { 0, w, 0 };

int[] y1 = { 0, 0, h };

g2d.setPaint(Color.GREEN);

g2d.fillPolygon(x1, y1, 3);

int[] x2 = { w, 0, w };

int[] y2 = { 0, h, h };

g2d.setPaint(Color.RED);

g2d.fillPolygon(x2, y2, 3);

String btText = ((JButton) c).getText();

g2d.setPaint(Color.BLACK);

g2d.drawString(btText, w/3, 2*h/3);

}

}

追问

给力!
可以帮我把这个button重新定义成一个类吗?它继承了jbutton,外观就像你做的那样

追答

改过了,如图:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.plaf.basic.BasicButtonUI;

public class ButtonDemo extends JFrame {

private JButton bt = null;

public ButtonDemo() {
super("Button Demo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(50, 50);
this.setSize(300, 300);
this.setLayout(null);

bt = new MyButton("按钮");
bt.setBounds(50, 50, 80, 30);
this.add(bt);

}

public static void main(String[] args) {
new ButtonDemo().setVisible(true);
}

}

class MyButton extends JButton {

public MyButton() {

}

public MyButton(String label) {
super(label);
Graphics g = this.getGraphics();
MyButtonUI buttonUI = new MyButtonUI(g, this);
this.setUI(buttonUI);
}

}

class MyButtonUI extends BasicButtonUI {

private Graphics g;
private JComponent c;

public MyButtonUI() {

}

public MyButtonUI(Graphics g, JComponent c) {
this.g = g;
this.c = c;
}

public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(), c.getHeight());
}
paint(g, c);
}

public void paint(Graphics g, JComponent c) {
int w = c.getWidth(); // 获取这个组件的宽
int h = c.getHeight(); // 获取这个组件的高

Graphics2D g2d = (Graphics2D) g;
// 设置抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int[] x1 = { 0, w, 0 };
int[] y1 = { 0, 0, h };
g2d.setPaint(Color.GREEN);
g2d.fillPolygon(x1, y1, 3);

int[] x2 = { w, 0, w };
int[] y2 = { 0, h, h };
g2d.setPaint(Color.RED);
g2d.fillPolygon(x2, y2, 3);

String btText = ((JButton) c).getText();
g2d.setPaint(Color.BLACK);
g2d.drawString(btText, w/3, 2*h/3);
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答