import javax.swing.JLabel;
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class ChessPiece extends JLabel{
String name;
public ChessPiece(String name){
super(name);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
this.name=name;
}
}
public class ChessTest extends JFrame {
static int width;
public ChessTest(){
super("Test");
JPanel JP=new JPanel();
JP.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
this.getContentPane().add(JP);
ChessPiece CP=new ChessPiece("帅");
width=CP.getWidth();
JP.add(CP);
pack();
setVisible(true);
}
public static void main(String[] args){
ChessTest CT=new ChessTest();
System.out.println(width);
}
}
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class ChessPiece extends JLabel{
String name;
public ChessPiece(String name){
super(name);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
this.name=name;
}
}
public class ChessTest extends JFrame {
static int width;
public ChessTest(){
super("Test");
JPanel JP=new JPanel();
JP.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
this.getContentPane().add(JP);
ChessPiece CP=new ChessPiece("帅");
width=CP.getWidth();
JP.add(CP);
pack();
setVisible(true);
}
public static void main(String[] args){
ChessTest CT=new ChessTest();
System.out.println(width);
}
}
解决方案 »
- 关于匿名内部类的一个疑惑!感觉很有用的提问!
- java 里面 当我们去链接一个远程的地址的时候,ip地址存在,但是端口不存在,就会出现超时,这个程序怎么写?
- 购物系统如何实现?
- string indexOf startWith contains 的效率,谁高谁低?
- 请高手解释代码?
- 请问50KB左右的图片作为背景加载一般要多长时间?
- 我一直是作信息管理系统开发的,请问什么是嵌入式开发,举个例子?如何开发?用于哪类项目?
- 请问专科生可不可以考研
- 我想自己写些小程序来提高自己,但是不知道从何下手,请高手指教。
- a problem about anonymous inner!!
- 看看这是为什么?关于String与StringBuffer
- jdk 1.4.2中如何获取系统环境变量?
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class ChessPiece extends JLabel{
String name;
public ChessPiece(String name){
//super(name);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
this.name=name;
public void paint(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("隶书",Font.BOLD,28));
g.drawString(name,20,20);
g.fillOval(50,50,20,20);
g.drawOval(50,50,20,20); }
}
}
public class ChessTest extends JFrame {
static int width;
public ChessTest(){
super("Test");
JPanel JP=new JPanel();
JP.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
this.getContentPane().add(JP);
ChessPiece CP=new ChessPiece("帅");
width=CP.getWidth();
JP.add(CP);
pack();
setVisible(true);
}
public static void main(String[] args){
ChessTest CT=new ChessTest();
System.out.println(width);
}
}这个程序我原意是想显示一个中国象棋棋子“帅”,但是实际上不显示,我觉得可能是Drawstring的参数中的坐标出了问题,有可能我写的坐标不是在JLabel中,所以我想看下JLabel的坐标范围,于是有了输出width,但是width显示为0,所以我也不知道到底哪里出了差错,大家帮下忙吧。
你把width=CP.getWidth();放到setVisible(true);方法后面就可以了。
import javax.swing.*;import java.awt.*;
import java.awt.event.*; class ChessP extends JLabel{
String name;
public ChessP(String name){
//super(name);
this.name=name;
}
public void paint(Graphics g){
g.drawString(name,70,35);
}
}public class ChessTest extends JFrame {
int width;
public ChessTest(){
super("Test");
JPanel JP=new JPanel();
JP.setBorder(BorderFactory.createEmptyBorder(20,60,20,60));
this.getContentPane().add(JP);
ChessP CP=new ChessP("hello");
JP.add(CP);
setSize(300,300);
pack();
setVisible(true);
System.out.println(CP.getBounds());
}
public static void main(String[] args){
ChessTest CT=new ChessTest();
}
}