下面是一个Application程序,其功能是创建一个基本框架,设置为FlowLayout布局管理器,根据命令行参数输入值创建相应数量的按钮。
importjava.awt.*;
  importjava.awt.event.*;
  importjavax.swing.*; 
  publicclassFlowLayoutTest
  {
  publicstaticvoidmain(String[]args)
  {
  if(args.length!=1)
  {
  System.out.println(″FlowLayout管理器″);
  System.exit(0);
  }
  StringbuttonString=args[0];
  intbuttonNumber=Byte.parseInt(buttonString);
  ButtonFrameframe=newButtonFrame(buttonNumber);
  frame.setDefaultCloseOperation(JFrame.EXIT-ON-CLOSE);
  frame.show();
  }
  }
  classButtonFrameextendsJFrame
  {
  publicButtonFrame(intbuttonNumber)
  {
  buttons=buttonNumber;
  setTitle(″FlowLayout管理器″);
  setSize(WIDTH,HEIGHT);
  JPanelbuttonPanel=newJPanel();
  for(inti=0;i<buttons;i+ 
  {
  JButtonaddButton=newJButton(″add″+i);
  buttonPanel.add(addButton);
  }
  ContainercontentPane=setContentPane();
  contentPane.add(buttonPanel);
  }
  publicstaticfinalintWIDTH=350;
  publicstaticfinalintHEIGHT=200;
  privateintbuttons;
  }

解决方案 »

  1.   


    import java.awt.*;
    import javax.swing.*;public class FlowLayoutTest {
    public static void main(String[] args) {
    if (args.length != 1) {
    System.out.println("FlowLayout管理器");
    System.exit(0);
    }
    String buttonString = args[0];
    int buttonNumber = Byte.parseByte(buttonString);
    ButtonFrame frame = new ButtonFrame(buttonNumber);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }class ButtonFrame extends JFrame {
    public ButtonFrame(int buttonNumber) {
    buttons = buttonNumber;
    setTitle("FlowLayout管理器");
    setSize(WIDTH, HEIGHT);
    JPanel buttonPanel = new JPanel();
    for (int i = 0; i < buttons; i++) {
    JButton addButton = new JButton("add" + i);
    buttonPanel.add(addButton);
    }
    Container contentPane = getContentPane();
    contentPane.add(buttonPanel);
    } public static final int WIDTH = 350;
    public static final int HEIGHT = 200;
    private int buttons;
    }
      

  2.   

    public class FlowLayoutTest extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 350;
    public static final int HEIGHT = 200;
    private int buttons; public FlowLayoutTest(int buttonNumber) {
    buttons = buttonNumber;
    setTitle("FlowLayout管理器");
    setSize(WIDTH, HEIGHT);
    JPanel buttonPanel = new JPanel();
    for (int i = 0; i < buttons; i++) {
    JButton addButton = new JButton("add" + i);
    buttonPanel.add(addButton);
    }
    Container contentPane = getContentPane();
    contentPane.add(buttonPanel);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length != 1) {
    System.out.println("FlowLayout管理器");
    System.exit(0);
    }
    String buttonString = args[0];
    int buttonNumber = Byte.parseByte(buttonString);
    FlowLayoutTest frame = new FlowLayoutTest(buttonNumber);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show(); }
    }