我想问问为什么我的程序运行之后空白一片..在弄容器背景之前还好好的.是容器问题么??希望高手们能为我解答..谢谢!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BackGroundPane extends JPanel {
 private ImageIcon imageIcon;
 
 public ImageIcon getImageIcon() {
  return imageIcon;
 } public void setImageIcon(ImageIcon imageIcon) {//设置图片
  this.imageIcon = imageIcon;
 } public BackGroundPane() {
  super();
  this.setOpaque(false);
 } protected void paintChildren(Graphics g) {//重绘JPanel下的Components
  super.paintChildren(g);
  for(int i =0;i<this.getComponents().length;i++){
   this.getComponent(i).repaint();
  }
  g.dispose();
 } protected void paintComponent(Graphics g) {
  Graphics2D g2D = (Graphics2D)g;
  super.paintComponent(g2D);if(imageIcon!=null){//绘制背景
   g2D.drawImage(imageIcon.getImage(), 0, 0,this.getWidth(),this.getHeight(),this);}
  g2D.dispose();
 }
}
public class ComponentTest extends JFrame {
 private BackGroundPane container;
 private JLabel jtextfield1;
 private JTextField jtextfield2;
 private JButton jbutton1;
 private JButton jbutton2;
 int randomNumber;
 int typedNumber;
 private ImageIcon i1;
 private JLabel l1;
 public ComponentTest(){
  super("Testing JFrame");
  container = new BackGroundPane();
  i1=new ImageIcon("A3NM0A898E_102_300_260.jpg");
  //l1=new JLabel(i1);
  container.setImageIcon(i1);  
  jtextfield1 = new JLabel("猜数字游戏");
  jtextfield1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
  jtextfield1.setText("猜数字游戏");
  jtextfield1.setBounds(new Rectangle(123, 6, 124, 25));
  jtextfield2 = new JTextField("请输入数字,范围1-100",10);
  jtextfield2.setText("0");
  jtextfield2.setBounds(new Rectangle(62, 41, 267, 25));
  jtextfield2.setEditable(false);
  jbutton1 = new JButton("生成随机数");
  jbutton1.setBounds(new Rectangle(38, 179, 108, 31));
  jbutton2 = new JButton("猜数");
  jbutton2.setBounds(new Rectangle(247, 179, 108, 31));
  container.add(jtextfield1);
  container.add(jtextfield2);
  container.add(jbutton1);
  container.add(jbutton2);
  TextFieldHandler handler = new TextFieldHandler();
  jtextfield2.addActionListener(handler);
  jbutton1.addActionListener(handler);
  jbutton2.addActionListener(handler);
  setSize(400,300);
  setVisible(true);
 }
 public static void main(String args[]){
  ComponentTest component = new ComponentTest();
  component.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent e) {
  try{
   
   if(e.getSource() == jbutton1)
   
      randomNumber = (int)(Math.random()*100+1);
      if(randomNumber==0)
      randomNumber++;
      
   jtextfield1.setText("数字已生成");
   jbutton1.setEnabled(false);
   jtextfield2.setEditable(true);      if(e.getSource() == jtextfield2)
      jbutton2.setEnabled(true);
   typedNumber = Integer.parseInt(jtextfield2.getText());
   if (typedNumber > 100)
   throw new NumberFormatException();
   if(e.getSource() == jbutton2||e.getSource() == jtextfield2)
   {
      if(typedNumber > randomNumber)
      JOptionPane.showMessageDialog(null, "大了");
      else if (typedNumber < randomNumber)
   JOptionPane.showMessageDialog(null,"小了");
      else{
      JOptionPane.showMessageDialog(null,"恭喜你,答对了");
   jbutton1.setEnabled(true);
   jtextfield2.setEditable(false);
   jbutton2.setEnabled(false);
   jtextfield1.setText("猜大小游戏!");
   jtextfield2.setText("请输入数字,范围1-100!");
      }
   }
  }catch (NumberFormatException exception){
   JOptionPane.showMessageDialog(null, "请在文本框输入正确的信息");
  }    
 }
}
 }
拜托了!...解决了的高手可以把程序发到我邮箱么?!.谢谢[email protected]
加我QQ也行..236241416

解决方案 »

  1.   

    帮你改好了,可以运行了.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ComponentTest extends JFrame { private JLabel jtextfield1; private JTextField jtextfield2; private JButton jbutton1; private JButton jbutton2; int randomNumber; int typedNumber; private ImageIcon i1; public ComponentTest() {
    this.getContentPane().setLayout(null);
    this.setTitle("Testing   JFrame");
    this.setSize(new   Dimension(400,   335));
    i1 = new ImageIcon("D:/My Documents/My Pictures/晶莹冰雪世界/024.jpg");//这里我使用的是绝对路径
    JLabel   hy=new   JLabel(i1);
    this.getLayeredPane().add(hy,new   Integer(Integer.MIN_VALUE)); jtextfield1 = new JLabel("猜数字游戏");
    jtextfield1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
    jtextfield1.setText("猜数字游戏");
    jtextfield1.setBounds(new Rectangle(123, 6, 124, 25));
    jtextfield2 = new JTextField("请输入数字,范围1-100", 10);
    jtextfield2.setText("0");
    jtextfield2.setBounds(new Rectangle(62, 41, 267, 25));
    jtextfield2.setEditable(false);
    jbutton1 = new JButton("生成随机数");
    jbutton1.setBounds(new Rectangle(38, 179, 108, 31));
    jbutton2 = new JButton("猜数");
    jbutton2.setBounds(new Rectangle(247, 179, 108, 31));
    this.getContentPane().add(jtextfield1);
    this.getContentPane().add(jtextfield2);
    this.getContentPane().add(jbutton1);
    this.getContentPane().add(jbutton2);
    TextFieldHandler handler = new TextFieldHandler();
    jtextfield2.addActionListener(handler);
    jbutton1.addActionListener(handler);
    jbutton2.addActionListener(handler);
    hy.setBounds(0,0,i1.getIconWidth(),i1.getIconHeight()); 
            ((JPanel)getContentPane()).setOpaque(false);
    setVisible(true);
    } public static void main(String args[]) {
    ComponentTest component = new ComponentTest();
    component.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } private class TextFieldHandler implements ActionListener { public void actionPerformed(ActionEvent e) {
    try { if (e.getSource() == jbutton1) randomNumber = (int) (Math.random() * 100 + 1);
    if (randomNumber == 0)
    randomNumber++; jtextfield1.setText("数字已生成");
    jbutton1.setEnabled(false);
    jtextfield2.setEditable(true); if (e.getSource() == jtextfield2)
    jbutton2.setEnabled(true);
    typedNumber = Integer.parseInt(jtextfield2.getText());
    if (typedNumber > 100)
    throw new NumberFormatException();
    if (e.getSource() == jbutton2 || e.getSource() == jtextfield2) {
    if (typedNumber > randomNumber)
    JOptionPane.showMessageDialog(null, "大了");
    else if (typedNumber < randomNumber)
    JOptionPane.showMessageDialog(null, "小了");
    else {
    JOptionPane.showMessageDialog(null, "恭喜你,答对了");
    jbutton1.setEnabled(true);
    jtextfield2.setEditable(false);
    jbutton2.setEnabled(false);
    jtextfield1.setText("猜大小游戏!");
    jtextfield2.setText("请输入数字,范围1-100!");
    }
    }
    } catch (NumberFormatException exception) {
    JOptionPane.showMessageDialog(null, "请在文本框输入正确的信息");
    }
    }
    }
    }
      

  2.   

    LZ,你都没有把container   =   new   BackGroundPane(); 这个对象加载到ComponentTest对象中。肯定不能看到拉在构造对象中加一句
    JPanel panel = JPanel)this.getContentPane();
    然后在显示之前加panel.add(container);这样就OK拉
      

  3.   

    好多代码啊....眼睛疼...
    不过,好象确实是因为Container 没有添加到JFrame中去吧...
      

  4.   

    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    class BackGroundPane extends JPanel { 
     private ImageIcon imageIcon; 
      
     public ImageIcon getImageIcon() { 
      return imageIcon; 
     }  public void setImageIcon(ImageIcon imageIcon) {//设置图片 
      this.imageIcon = imageIcon; 
     }  public BackGroundPane() { 
      super(); 
      this.setOpaque(false); 
     }  protected void paintChildren(Graphics g) {//重绘JPanel下的Components 
      super.paintChildren(g); 
      for(int i =0;i <this.getComponents().length;i++){ 
       this.getComponent(i).repaint(); 
      } 
      g.dispose(); 
     }  protected void paintComponent(Graphics g) { 
      Graphics2D g2D = (Graphics2D)g; 
      super.paintComponent(g2D); if(imageIcon!=null){//绘制背景 
       g2D.drawImage(imageIcon.getImage(), 0, 0,this.getWidth(),this.getHeight(),this); } 
      g2D.dispose(); 
     } 

    public class ComponentTest extends JFrame { 
     private BackGroundPane container; 
     private JLabel jtextfield1; 
     private JTextField jtextfield2; 
     private JButton jbutton1; 
     private JButton jbutton2; 
     int randomNumber; 
     int typedNumber; 
     private ImageIcon i1; 
     private JLabel l1; 
     public ComponentTest(){ 
      super("Testing JFrame"); 
      container = new BackGroundPane(); 
      i1=new ImageIcon("A3NM0A898E_102_300_260.jpg"); 
      //l1=new JLabel(i1); 
      container.setImageIcon(i1);   
      jtextfield1 = new JLabel("猜数字游戏"); 
      jtextfield1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20)); 
      jtextfield1.setText("猜数字游戏"); 
      jtextfield1.setBounds(new Rectangle(123, 6, 124, 25)); 
      jtextfield2 = new JTextField("请输入数字,范围1-100",10); 
      jtextfield2.setText("0"); 
      jtextfield2.setBounds(new Rectangle(62, 41, 267, 25)); 
      jtextfield2.setEditable(false); 
      jbutton1 = new JButton("生成随机数"); 
      jbutton1.setBounds(new Rectangle(38, 179, 108, 31)); 
      jbutton2 = new JButton("猜数"); 
      jbutton2.setBounds(new Rectangle(247, 179, 108, 31)); 
      container.add(jtextfield1); 
      container.add(jtextfield2); 
      container.add(jbutton1); 
      container.add(jbutton2); 
      TextFieldHandler handler = new TextFieldHandler(); 
      jtextfield2.addActionListener(handler); 
      jbutton1.addActionListener(handler); 
      jbutton2.addActionListener(handler); 
      setSize(400,300); 
      /*加了以下两行*/
      getContentPane().add(container);
      pack();
      /***************/
      setVisible(true); 
     } 
     public static void main(String args[]){ 
      ComponentTest component = new ComponentTest(); 
      component.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
     private class TextFieldHandler implements ActionListener{  public void actionPerformed(ActionEvent e) { 
      try{ 
        
       if(e.getSource() == jbutton1) 
        
          randomNumber = (int)(Math.random()*100+1); 
          if(randomNumber==0) 
          randomNumber++; 
           
       jtextfield1.setText("数字已生成"); 
       jbutton1.setEnabled(false); 
       jtextfield2.setEditable(true);       if(e.getSource() == jtextfield2) 
          jbutton2.setEnabled(true); 
       typedNumber = Integer.parseInt(jtextfield2.getText()); 
       if (typedNumber >  100) 
       throw new NumberFormatException(); 
       if(e.getSource() == jbutton2 || e.getSource() == jtextfield2) 
       { 
          if(typedNumber >  randomNumber) 
          JOptionPane.showMessageDialog(null, "大了"); 
          else if (typedNumber  < randomNumber) 
       JOptionPane.showMessageDialog(null,"小了"); 
          else{ 
          JOptionPane.showMessageDialog(null,"恭喜你,答对了"); 
       jbutton1.setEnabled(true); 
       jtextfield2.setEditable(false); 
       jbutton2.setEnabled(false); 
       jtextfield1.setText("猜大小游戏!"); 
       jtextfield2.setText("请输入数字,范围1-100!"); 
          } 
       } 
      }catch (NumberFormatException exception){ 
       JOptionPane.showMessageDialog(null, "请在文本框输入正确的信息"); 
      }     
     } 

     }