这个就需要使用FlowLayout布局管理器了

解决方案 »

  1.   

    使用 FlowLayout
    eg:
    private FlowLayout flowLayout = new FlowLayout();
    flowLayout.setAlignment(FlowLayout.LEFT);
      

  2.   

    顺便问一下,有没有控制绝对大小的啊?好像setBounds()不好用啊
      

  3.   

    如果你要结对控制到小,首先要设getContentPane().setLayout(null),然后再用setbounds()就可以了。下面是一个我做的例子:import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class AddData
    {
    // Attributes
    private JFrame  enterForm = new JFrame("Data Input Form");
    private JFrame  printForm = new JFrame("Print Form");
    private JLabel  lblName, lblDescription, lblQuantity,
            lblReorderPoint, lblAisle,
            lblShelf, lblBin;
    private JTextField txtName, txtDescription, txtQuantity,
               txtReorderPoint, txtAisle,
               txtShelf, txtBin;
    private JTextArea  printArea, printTitle;
    private JButton  efBttnSave,efBttnClear, efBttnExit,
    pfBttnExit; private Product product = new Product();
    private Vector inventory = new Vector();
    private static boolean notYetPrintTitle = true; // Constructor
    public AddData()
    {
    enterForm.setBounds(140,140,525,260);
    Container content = enterForm.getContentPane();
    content.setLayout(null); // Product Name Field
    lblName = new JLabel("Product Name");
    lblName.setBounds(20,20,120,22);
    content.add(lblName);
    txtName = new JTextField();
    txtName.setBounds(110,20,125,22);
    content.add(txtName);
    TextHandler nameHandler = new TextHandler();
    txtName.addActionListener(nameHandler); // Description Field
    lblDescription = new JLabel("Description");
    lblDescription.setBounds(20,60,120,22);
    content.add(lblDescription);
    txtDescription = new JTextField();
    txtDescription.setBounds(110,60,375,22);
    content.add(txtDescription);
    TextHandler descHandler = new TextHandler();
    txtDescription.addActionListener(descHandler); // Quantity Field
    lblQuantity = new JLabel("Quantity");
    lblQuantity.setBounds(20,100,120,22);
    content.add(lblQuantity);
    txtQuantity = new JTextField();
    txtQuantity.setBounds(110,100,100,22);
    content.add(txtQuantity);
    TextHandler qtyHandler = new TextHandler();
    txtQuantity.addActionListener(qtyHandler); // Reorder Point Field
    lblReorderPoint = new JLabel("Reorder Point");
    lblReorderPoint.setBounds(290,100,120,22);
    content.add(lblReorderPoint);
    txtReorderPoint = new JTextField();
    txtReorderPoint.setBounds(385,100,100,22);
    content.add(txtReorderPoint);
    TextHandler rdpHandler = new TextHandler();
    txtReorderPoint.addActionListener(rdpHandler); // Aisle Field
    lblAisle = new JLabel("Aisle");
    lblAisle.setBounds(20,140,60,22);
    content.add(lblAisle);
    txtAisle = new JTextField();
    txtAisle.setBounds(60,140,20,22);
    content.add(txtAisle);
    TextHandler aisleHandler = new TextHandler();
    txtAisle.addActionListener(aisleHandler); // Shelf Field
    lblShelf = new JLabel("Shelf");
    lblShelf.setBounds(180,140,60,22);
    content.add(lblShelf);
    txtShelf = new JTextField();
    txtShelf.setBounds(220,140,20,22);
    content.add(txtShelf);
    TextHandler shelfHandler = new TextHandler();
    txtShelf.addActionListener(shelfHandler); // Bin Field
    lblBin = new JLabel("Bin");
    lblBin.setBounds(350,140,60,22);
    content.add(lblBin);
    txtBin = new JTextField();
    txtBin.setBounds(380,140,20,22);
    content.add(txtBin);
    TextHandler binHandler = new TextHandler();
    txtBin.addActionListener(binHandler); // Save Button
    efBttnSave = new JButton("Save");
    efBttnSave.setBounds(20,190,90,30);
    efBttnSave.setEnabled(false);
    content.add(efBttnSave);
    ButtonHandler efBttnSaveHandler = new ButtonHandler();
    efBttnSave.addActionListener(efBttnSaveHandler); // Clear Button
    efBttnClear = new JButton("Clear");
    efBttnClear.setBounds(200,190,90,30);
    content.add(efBttnClear);
    ButtonHandler efBttnClearHandler = new ButtonHandler();
    efBttnClear.addActionListener(efBttnClearHandler); // Exit Button
    efBttnExit = new JButton("Exit");
    efBttnExit.setBounds(390,190,90,30);
    content.add(efBttnExit);
    ButtonHandler efBttnExitHandler = new ButtonHandler();
    efBttnExit.addActionListener(efBttnExitHandler); // Print Window
    printForm.setBounds(100,140,850,550);
    printForm.setVisible(false);
    Container pcontent = printForm.getContentPane();
    pcontent.setLayout(null); // Print Title within the print window
    printTitle = new JTextArea();
    printTitle.setBounds(10,10,820,50);
    printTitle.setEditable(false);
    pcontent.add(printTitle); // Print Area within the print window
    printArea = new JTextArea();
    printArea.setBounds(10,60,820,400);
    printArea.setEditable(false);
    pcontent.add(printArea); // Exit button within print window
    pfBttnExit = new JButton("Exit");
    pfBttnExit.setBounds(370,480,90,30);
    pcontent.add(pfBttnExit);
    ButtonHandler pfBttnExitHandler = new ButtonHandler();
    pfBttnExit.addActionListener(pfBttnExitHandler); printForm.addWindowListener(new WindowAdapter()
    {
    public void WindowClosing(WindowEvent e)
    {
    printForm.hide();
    }
    });
    enterForm.setVisible(false);
    }
      

  4.   


    // Allow other class to get access to its visibility
    public JFrame getPrintForm()
    {
    return printForm;
    } // Allow other class to get access to its visibility
    public JFrame getEnterForm()
    {
    return enterForm;
    } // Allow other class to get access to its focus
    public JTextField getTxtName()
    {
    return txtName;
    } // clear all contents in the text fields
    public void clearDataFields()
    {
    txtName.setText("");
    txtDescription.setText("");
    txtQuantity.setText("");
    txtReorderPoint.setText("");
    txtAisle.setText("");
    txtShelf.setText("");
    txtBin.setText("");
    } // Button Action Event Handler
    class ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == efBttnSave)
    {
    printArea.append(product.toString()+"\n");
    inventory.add(new Product(product.getName(),
      product.getDescription(),
      product.getQuantity(),
      product.getReorderPoint(),
      product.getAisle(),
      product.getShelf(),
      product.getBin()));
    clearDataFields();
    txtName.requestFocus();
    efBttnSave.setEnabled(false); // The title will be printed only when the first field has been entered
    if (notYetPrintTitle)
    {
    printTitle.setFont(new Font("System",Font.BOLD,18));
    printTitle.append("\t\t    Inventory Report\n\n");
    notYetPrintTitle = false;
    }
    }
    else if (e.getSource() == efBttnClear)
    {
    clearDataFields();
    txtName.requestFocus();
    }
    else if (e.getSource() == efBttnExit)
    {
    enterForm.setVisible(false);
    }
    else if (e.getSource() == pfBttnExit)
    {
    printForm.setVisible(false);
    }
    }
    } // Text Field Action Event handler
    class TextHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    long numberL;
    int  number;
    char c;
    if (e.getSource() == txtName)
    {
    if (txtName.getText().length() < 13 && txtName.getText().length() > 0)
    {
    product.setName(txtName.getText());
    txtDescription.requestFocus();
    }
    else if (txtName.getText().length() == 0)
    {
    txtName.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The field can not be blank.",
    "No data entry",
    JOptionPane.ERROR_MESSAGE);
    }
    else
    {
    txtName.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be 12 character long or less.",
    "Out of range",
    JOptionPane.ERROR_MESSAGE);
    txtName.setText("");
    }
    }
    else if (e.getSource() == txtDescription)
    {
    if (txtDescription.getText().length() < 41 &&
    txtDescription.getText().length() > 0)
    {
    product.setDescription(txtDescription.getText());
    txtQuantity.requestFocus();
    }
    else if (txtDescription.getText().length() == 0)
    {
    txtDescription.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The field can not be blank.",
    "No data entry",
    JOptionPane.ERROR_MESSAGE);
    }
    else
    {
    txtDescription.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be 40 character long or less.",
    "Out of range",
    JOptionPane.ERROR_MESSAGE);
    txtDescription.setText("");
    }
    }
    else if (e.getSource() == txtQuantity)
    {
    try
    {
    numberL = Long.parseLong(txtQuantity.getText());
    product.setQuantity(numberL);
    txtReorderPoint.requestFocus();
    }
    catch (NumberFormatException nfe)
    {
    txtQuantity.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be an integer",
    "Invalid data taype",
    JOptionPane.ERROR_MESSAGE);
    txtQuantity.setText("");
    }
    }
    else if (e.getSource() == txtReorderPoint)
    {
    try
    {
    numberL = Long.parseLong(txtReorderPoint.getText());
    product.setReorderPoint(numberL);
    txtAisle.requestFocus();
    }
    catch (NumberFormatException nfe)
    {
    txtReorderPoint.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be an integer",
    "Invalid data taype",
    JOptionPane.ERROR_MESSAGE);
    txtReorderPoint.setText("");
    }
    }
    else if (e.getSource() == txtAisle)
    {
    try
    {
    number = Integer.parseInt(txtAisle.getText());
    if (number >= 0 && number <= 9)
    {
    product.setAisle(number);
    txtShelf.requestFocus();
    }
    else
    {
    txtAisle.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The number must be 0-9",
    "Out of range",
    JOptionPane.ERROR_MESSAGE);
    txtAisle.setText("");
    }
    }
    catch (NumberFormatException nfe)
    {
    txtAisle.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be an integer",
    "Invalid data taype",
    JOptionPane.ERROR_MESSAGE);
    txtAisle.setText("");
    }
    }
    else if (e.getSource() == txtShelf)
    {
    if (txtShelf.getText().length() < 2)
    {
    c = Character.toUpperCase(txtShelf.getText().charAt(0));
    if (c >= 'A' && c <= 'F')
    {
    product.setShelf(c);
    txtBin.requestFocus();
    }
    else
    {
    txtShelf.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The character must be A-F",
    "Out of range",
    JOptionPane.ERROR_MESSAGE);
    txtShelf.setText("");
    }
    }
    else
    {
    txtShelf.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The content must be a character",
    "Invalid data taype",
    JOptionPane.ERROR_MESSAGE);
    txtShelf.setText("");
    }
    }
    else if (e.getSource() == txtBin)
    {
    if (txtBin.getText().length() < 2)
    {
    c = Character.toUpperCase(txtBin.getText().charAt(0));
    if (c >= 'A' && c <= 'D')
    {
    product.setBin(c);
    efBttnSave.requestFocus();
    }
    else
    {
    txtBin.requestFocus();
    JOptionPane.showMessageDialog(null,
    "The character must be A-D",
    "Out of Range",
    JOptionPane.ERROR_MESSAGE);
    txtBin.setText("");
    }
    }
    else
    {
    txtBin.requestFocus();
    JOptionPane.showMessageDialog( null,
    "The content must be a character",
    "Invalid data taype",
    JOptionPane.ERROR_MESSAGE);
    txtShelf.setText("");
    }
    } // end of outer else if block
    if (txtName.getText().length() != 0 && txtDescription.getText().length() != 0 &&
    txtQuantity.getText().length() != 0  && txtReorderPoint.getText().length() != 0 &&
    txtAisle.getText().length() != 0 && txtShelf.getText().length() != 0 &&
    txtBin.getText().length() != 0)
    {
    efBttnSave.setEnabled(true);
    } } // end of actionPerformed() method
    } // end of textHandler class
    }// end of public class