如何使我的Jtextfiels,Jlabels和JtextArea出现在borderlayout中?我想将我的label1放置在北部,将textarea放置在东部,将其余的放置在西部。问题是它们没有出现,并且我的文本框似乎在左侧被挤压。

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ReliefFrame extends JFrame
{
    ReliefCenter s = new ReliefCenter("JavaCup");
    JPanel row;
    JPanel roww;
    JPanel rowww;
    JPanel rowwww;
    JLabel Label1;
    JLabel Label2;
    JLabel Label3;
    JLabel Label4;
    JButton[] button=new JButton[5];
    JTextField aTextField;
    JTextField bTextField;
    JTextField cTextField;
    JTextArea aTextArea;
    public ReliefFrame() {
        super("ReliefCenter");
        setSize(700, 700);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible( true );
        setLayout( new BorderLayout() );
        Label1= new JLabel("Welcome to JavaCup Relief Center");
        add(Label1);

        createNorthPanel();
        createEastPanel();
        createWestPanel();
        createSouthPanel();
        createaTextField();
        Label2= new JLabel("Relief Good",JLabel.LEFT);
        add(Label2);
        createbTextField();
        Label3= new JLabel("Quantity",JLabel.LEFT);
        add(Label3);
        createcTextField();
        Label4= new JLabel("Unit",JLabel.LEFT);
        add(Label4);
        createButton();
        createBButton();
        createCButton();
        createDButton();
        createEButton();
        createTextArea();
        add( row, "East" );
        add( roww, "West" );
        add( rowww, "North");
        add( rowwww, "South");
    }

    private void createNorthPanel()
    {
        rowww = new JPanel();
        rowww.setLayout( new BorderLayout( ) );
    }
    private void createEastPanel()
    {
        row = new JPanel();
        row.setLayout( new BorderLayout() );
    }
    private void createWestPanel()
    {
        roww = new JPanel();
        roww.setLayout( new GridLayout(6,1) );
    }
    private void createSouthPanel()
    {
        rowwww=new JPanel();
        rowwww.setLayout(new GridLayout(1,5));
    }
    private void createaTextField()
    {
        aTextField = new JTextField( "" );
        roww.add( aTextField );
    }

    private void createbTextField()
    {
        bTextField = new JTextField( "" );
        roww.add( bTextField );
    }

    private void createcTextField()
    {
        cTextField = new JTextField( "" );
        cTextField.setHorizontalAlignment(JTextField.LEFT);
        roww.add( cTextField );
    }
    private void createTextArea()
    {
        aTextArea = new JTextArea("" );
        aTextArea.setEditable(false);
        row.add(aTextArea);
    }

    private void createButton()
    {
        button[0] = new JButton( "Add Good" );
        class ButtonListener implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                String a = aTextField.getText();
                String b = bTextField.getText();
                String c = cTextField.getText();
                int d=Integer.parseInt(b);
                s.addNewGood(a,d,c);//
                aTextField.setText( "" );
                bTextField.setText( "" );
                cTextField.setText( "" );
                aTextArea.append("Added new good: "+a+"\n");
            }
        }
        button[0].addActionListener( new ButtonListener() );
        rowwww.add( button[0] );
    }
    private void createBButton()
    {
        button[1] = new JButton( "Receive Goods" );
        class ButtonListener implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                String a = aTextField.getText();
                String b = bTextField.getText();
                int d=Integer.parseInt(b);
                s.receiveGoods(a,d);//
                aTextField.setText( "" );
                bTextField.setText( "" );
                aTextArea.append("Received "+a+":"+d+"\n");
            }
        }
        button[1].addActionListener( new ButtonListener() );
        rowwww.add( button[1] );
    }
    private void createCButton()
    {
        button[2] = new JButton( "Release Packs" );
        class ButtonListener implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                String a = bTextField.getText();
                int b=Integer.parseInt(a);
                s.releasePacks(b);
                bTextField.setText( "" );
            }
        }
        button[2].addActionListener( new ButtonListener() );
        rowwww.add( button[2] );
    }
    private void createDButton()
    {
        button[3] = new JButton( "Packs Released" );
        class ButtonListener implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                int a=s.getPackCount();
                aTextArea.append("JavaCup has released"+a+"packs. \n");
            }
        }
        button[3].addActionListener( new ButtonListener() );
        rowwww.add( button[3] );
    }
    private void createEButton()
    {
        button[4] = new JButton( "Inventory Report" );
        class ButtonListener implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {

                aTextArea.append("===INVENTORY===\n");
                s.printInventory();
                aTextArea.append("===============\n");

            }
        }
        button[4].addActionListener( new ButtonListener() );
        rowwww.add( button[4] );
    }
    public static void main(String[] arguments) {
    ReliefFrame c = new ReliefFrame();
    }
}

最佳答案

BorderLayout基于preferredSize工作。
因此,请尝试以下方法进行测试:

    ...
    add( row, BorderLayout.EAST );
    add( roww, BorderLayout.WEST );
    add( rowww, BorderLayout.NORTH);
    add( rowwww, BorderLayout.SOUTH);

    row.setPreferredSize(new Dimension(100,100));
    roww.setPreferredSize(new Dimension(100,100));
    rowww.setPreferredSize(new Dimension(100,100));
    rowwwww.setPreferredSize(new Dimension(100,100));
}


另外,您在添加标签时未指定约束,因此LayoutManager不知道该怎么做。

因此,请尝试使用以下标签:

labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout());

Label1= new JLabel("Welcome to JavaCup Relief Center");
labelPanel.add(Label1);

createNorthPanel();
createEastPanel();
createWestPanel();
createSouthPanel();
createaTextField();
Label2= new JLabel("Relief Good",JLabel.LEFT);
labelPanel.add(Label2);
createbTextField();
Label3= new JLabel("Quantity",JLabel.LEFT);
labelPanel.add(Label3);
createcTextField();
Label4= new JLabel("Unit",JLabel.LEFT);
labelPanel.add(Label4);

add(labelPanel, BorderLayout.CENTER);


请注意,我在GridLayout中放入了JPanel。您可以使用最适合的LayoutManager。我只是以最简单的例子为例

10-07 13:50