日期:2014-05-20 浏览次数:20700 次
import javax.swing.*; import java.awt.*; public class Dog { public static void main(String [] args){ JFrame frame = new JFrame(); frame.setSize(new Dimension(100,100)); frame.setLocation(10,10); frame.setVisible(true); frame.setLayout(new BorderLayout()); String[] s ={"BorderLayout.NORTH","BorderLayout.WEST","BorderLayout.CENTER","BorderLayout.EAST","BorderLayout.SOUTH"}; for(int i = 1; i <= 5; i++){ frame.add(new JButton("button"+ i),s[i-1]); } } }
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(new Dimension(100,100)); frame.setLocation(10,10); frame.setVisible(true); frame.setLayout(new BorderLayout()); String[] s ={BorderLayout.NORTH,BorderLayout.WEST,BorderLayout.CENTER,BorderLayout.EAST,BorderLayout.SOUTH}; for(int i = 1; i <= 5; i++){ frame.add(new JButton("button"+ i),s[i-1]); } }
------解决方案--------------------
frame.setVisible(true);
放在最后试下
import javax.swing.*; import java.awt.*; public class Dog { public static void main(String [] args){ JFrame frame = new JFrame(); frame.setSize(new Dimension(100,100)); frame.setLocation(10,10); frame.setLayout(new BorderLayout()); String[] s ={"BorderLayout.NORTH","BorderLayout.WEST","BorderLayout.CENTER","BorderLayout.EAST","BorderLayout.SOUTH"}; for(int i = 1; i <= 5; i++){ frame.add(new JButton("button"+ i),s[i-1]); } frame.setVisible(true); } }
------解决方案--------------------
import javax.swing.*; import java.awt.*; public class Dog { public static void main(String [] args){ JFrame frame = new JFrame(); frame.setSize(new Dimension(100,100)); frame.setLocation(10,10); frame.setLayout(new BorderLayout()); String[] s ={BorderLayout.NORTH,BorderLayout.WEST,BorderLayout.CENTER,BorderLayout.EAST,BorderLayout.SOUTH}; for(int i = 1; i <= 5; i++){ frame.add(new JButton("button"+ i),s[i-1]); } frame.setVisible(true); }}
------解决方案--------------------
刚刚发现上面的Border的常量用的不对,应该写成这样
import javax.swing.*; import java.awt.*; public class Dog{ public static void main(String [] args){ JFrame frame = new JFrame(); frame.setSize(new Dimension(100,100)); frame.setLocation(10,10); frame.setLayout(new BorderLayout()); String[] s ={BorderLayout.NORTH,BorderLayout.WEST,BorderLayout.CENTER,BorderLayout.EAST,BorderLayout.SOUTH}; for(int i = 1; i <= 5; i++){ frame.add(new JButton("button"+ i),s[i-1]); } frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
------解决方案--------------------
正解。