怎样才能在一个panel上设置背景图片
还有怎样判断一个字符串是否是数字包括小数点
谢谢!
import javax.swing.*;
import java.awt.Insets;
import java.awt.Graphics;public class PanelDemo extends JPanel {
private ImageIcon icon;
public PanelDemo() {
icon = new ImageIcon(\"1.jpg\");
try{
validateNumber();
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(this,\"这不是一个数字\");
}
}
public static void main(String[] args) {
JFrame f = new JFrame(\"Demo\");
f.getContentPane().add(new PanelDemo());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setResizable(false);
f.setVisible(true);
}
private void validateNumber() throws NumberFormatException {
String str = \"2.0\";
float f = Float.valueOf(str);
String massage = null;
if(str.indexOf(\".\") >= 0) {
massage = \"这是一个有小数点的数字\";
} else {
massage = \"这是一个没有小数点的数字\";
}
JOptionPane.showMessageDialog(this,massage);
}
public void paintComponent(Graphics g) {
Insets insets = getInsets();
super.paintComponent(g);
icon.paintIcon(this,g,insets.top,insets.left);
g.drawString(\"This is a PanelDemo\",200,200);
}
}