日期:2014-05-20  浏览次数:20700 次

一个简单的按钮布局界面,想简化代码,但为什么都不显示啊
Java code

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]);
            
        }
        
    }
    
        

}




------解决方案--------------------
Java code


    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);
放在最后试下
Java code

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);

        
    }
    
        

}

------解决方案--------------------
Java code

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的常量用的不对,应该写成这样
Java code


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);


    }



}

------解决方案--------------------
正解。
探讨
Java code


public static void main(String[] args)
{
JFrame frame = new JFrame();