遇到点问题,就是一个窗口,分为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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}