遇到点问题,就是一个窗口,分为3部分,第一部分是显示PANDA的图,一至十之间任意的个数,要求每行最多有5个,第二部分是HOW MANY PANDAS CAN YOU SEE?和那个TEXTFIELD,第三部分是“CHECK!”这个BUTTON.现在我编的这个程序显示很乱,请帮忙改一下程序。我也不知道说清没有,反正我快把自己说晕了,谢谢了。import java.awt.*;
import javax.swing.*;public class FrameDesign extends JFrame{
   ImageIcon a = new ImageIcon("image/panda.jpg");
   JLabel question = new JLabel("How many pandas can you see?");
   JTextField enterArea = new JTextField(8);
   JButton button = new JButton("Check!");   public FrameDesign(){ 
  
      JPanel top = new JPanel(new GridLayout(0,5));
      setIcon();
      
      JPanel center = new JPanel(new GridLayout());
      center.add(question);
      center.add(enterArea);
      
      JPanel bottom = new JPanel(new FlowLayout(FlowLayout.CENTER));
      bottom.add(button);
      
      setLayout(new GridLayout(0,5));
      add(top);
      add(center);
      add(bottom);
   }   public void setIcon(){
      int number = (int)(Math.random() * 10 + 1);
      for (int i = 0; i < number; i++){
         add(new JLabel(a));
      }
   }   public static void main(String[] args){
      JFrame frame = new FrameDesign();
      frame.setTitle("Welcome to pandaland!");
      frame.setSize(500,400);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;public class Test extends JFrame{
       ImageIcon a = new ImageIcon("image/panda.jpg");
       JLabel question = new JLabel("How many pandas can you see?");
       JTextField enterArea = new JTextField(8);
       JButton button = new JButton("Check!");
    JPanel center = new JPanel();
       public Test(){ 
      
          JPanel top = new JPanel(new GridLayout(0,5));
          setIcon();
          
          //JPanel center = new JPanel(new GridLayout());
          //center.add(question);
          //center.add(enterArea);
          
          JPanel south = new JPanel(new GridLayout());
          south.add(question);
          south.add(enterArea);
          south.add(button);
          
          JPanel tempCenter = new JPanel(new BorderLayout(40, 10));
          tempCenter.add(center);
          tempCenter.add(new JLabel(), BorderLayout.NORTH);
          tempCenter.add(new JLabel(), BorderLayout.WEST);
          tempCenter.add(new JLabel(), BorderLayout.EAST);
          add(tempCenter);
          add(south, BorderLayout.SOUTH);
        setIcon();
       }   public void setIcon(){
          int num = (int)(Math.random() * 10 + 1);
          center.removeAll();
          int len = (int)Math.sqrt(num) + 1;
          center.setLayout(new GridLayout(len, len));
          for (int i = 0; i < num; i++){
             center.add(new JLabel(" panda "));
          }
          //pack();//
       }   public static void main(String[] args){
          JFrame frame = new Test();
          frame.setTitle("Welcome to pandaland!");
          frame.setSize(500,400);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }
      

  2.   

    不好意思,可能是我说的不够清楚,第二部分是HOW MANY PANDAS CAN YOU SEE?和那个TEXTFIELD,在同一行,第三部分是“CHECK!”这个BUTTON在下一行,而且,button的大小不会随窗口的变化而变化。
      

  3.   

    import java.awt.*;
    import javax.swing.*;public class Test extends JFrame{
       ImageIcon a = new ImageIcon("image/panda.jpg");
       JLabel question = new JLabel("How many pandas can you see?");
       JTextField enterArea = new JTextField(8);
       JButton button = new JButton("Check!");
        JPanel center = new JPanel();
       public Test(){ 
      
          JPanel top = new JPanel(new GridLayout(0,5));
          setIcon();
          
          //JPanel center = new JPanel(new GridLayout());
          //center.add(question);
          //center.add(enterArea);
          
          JPanel south = new JPanel(new GridLayout());
          JPanel temp1 = new JPanel();
          JPanel temp2 = new JPanel();
          temp1.add(question);
          temp1.add(enterArea);
          temp2.add(button);
          south.setLayout(new GridLayout(2, 1));
          south.add(temp1);
          south.add(temp2);
          
          JPanel tempCenter = new JPanel(new BorderLayout(40, 10));
          tempCenter.add(center);
          tempCenter.add(new JLabel(), BorderLayout.NORTH);
          tempCenter.add(new JLabel(), BorderLayout.WEST);
          tempCenter.add(new JLabel(), BorderLayout.EAST);
          add(tempCenter);
          add(south, BorderLayout.SOUTH);
               setIcon();
       }   public void setIcon(){
          int num = (int)(Math.random() * 10 + 1);
          center.removeAll();
          int len = (int)Math.sqrt(num) + 1;
          center.setLayout(new GridLayout(len, len));
          for (int i = 0; i < num; i++){
             center.add(new JLabel(" panda "));
          }
          //pack();//
       }   public static void main(String[] args){
          JFrame frame = new Test();
          frame.setTitle("Welcome to pandaland!");
          frame.setSize(500,400);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }
      

  4.   

    啊啊啊啊啊啊啊啊啊啊啊啊啊啊,wd9053,你好棒啊,谢谢啦,那,再问一句,就是PANDA这个东西,如果有5个以上,那,第一行一定要是5个,这要怎么改呢,本来我是实现了这个功能,结果下边全乱了,现在是下边好了,上边有不好了。
      

  5.   

    那干脆设为一行显示5个好了
    import java.awt.*;
    import javax.swing.*;public class Test extends JFrame{
       ImageIcon a = new ImageIcon("image/panda.jpg");
       JLabel question = new JLabel("How many pandas can you see?");
       JTextField enterArea = new JTextField(8);
       JButton button = new JButton("Check!");
        JPanel center = new JPanel();
       public Test(){ 
      
          JPanel top = new JPanel(new GridLayout(0,5));
          //setIcon();
          
          //JPanel center = new JPanel(new GridLayout());
          //center.add(question);
          //center.add(enterArea);
          
          JPanel south = new JPanel(new GridLayout());
          JPanel temp1 = new JPanel();
          JPanel temp2 = new JPanel();
          temp1.add(question);
          temp1.add(enterArea);
          temp2.add(button);
          south.setLayout(new GridLayout(2, 1));
          south.add(temp1);
          south.add(temp2);
          
          JPanel tempCenter = new JPanel(new BorderLayout(40, 10));
          tempCenter.add(center);
          tempCenter.add(new JLabel(), BorderLayout.NORTH);
          tempCenter.add(new JLabel(), BorderLayout.WEST);
          tempCenter.add(new JLabel(), BorderLayout.EAST);
          add(tempCenter);
          add(south, BorderLayout.SOUTH);
               setIcon();
       }   public void setIcon(){
          int num = (int)(Math.random() * 10 + 1);
          System.out.println(num);
          center.removeAll();
          //int len = (int)Math.sqrt(num) + 1;
          int line = (num - 1) / 5 + 1;
          int total = line * 5;
          center.setLayout(new GridLayout(line, 5));
          for (int i = 0; i < num; i++){
             center.add(new JLabel(" panda "));
          }
          for(int i = num; i < total; i++)
          {
           center.add(new JLabel());
          }
          //pack();//
       }   public static void main(String[] args){
          JFrame frame = new Test();
          frame.setTitle("Welcome to pandaland!");
          frame.setSize(500,400);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }