import java.awt.Event.*;
import java.awt.*;import javax.swing.*;public class Chess extends JLabel {
public static void main(String[] args) {
new Chess("txz", "我", Color.green,50);

} private String name;
private Color chessColor;
private String player;
private int round; public Chess(String name, String player, Color chessColor, int round) {
this.name = name;
this.player = player;
this.chessColor = chessColor;
this.round = round;
setBackground(Color.white);
} public void paint(Graphics g) {
g.setColor(Color.green);
g.fillOval(1, 1, round - 1, round - 1);
g.setColor(chessColor);
g.setFont(new Font("隶书", Font.BOLD, 40));
g.drawString(name, 10, round - 18);
g.setColor(Color.yellow);
g.drawOval(1, 1, round - 1, round - 1);
} public int getWidth() {
return round;
} public int getHeight() {
return round;
} public int getRound() {
return round;
} public String getName() {
return name;
} public Color getChessColor() {
return chessColor;
} public void setPlayer(String player) {
this.player = player;
} public String getPlayer() {
return player;
}
}

解决方案 »

  1.   

    把 main 改成這樣,要畫出東西,先要有"畫布"才行。public static void main(String[] args) {
        JFrame f = new JFrame("A JFrame");
        f.setSize(250, 250);
        f.setLocation(300,200);
        f.getContentPane().add(BorderLayout.CENTER, new Chess("txz", "我", Color.RED,50));
        f.setVisible(true);  }