Java将数据放入边框布局中

时间:2012-02-10 17:24:07

标签: java swing layout jframe panel

如何在Java中实现以下输出,如Windows Calculator,

实施例,

First Row = Text Field
Second Row = 5 buttons
Third  Row = 5 Buttons

和边框布局中心的剩余数据,

如何将第一,第二,第三行放在边框布局的北边?这是我的代码,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

    public class GridLayoutFrame extends JFrame 
    {

        private JButton[] buttons; // Array of Buttons 1 to 10
        private JButton[] topButton; // 10 buttons that has to be placed under Text field 5 button in each row
        private JPanel Bottom; // A panel to show everything under first three Row
        private JPanel Top; // Top Panel that contains Text Field and 2nd and third Row
        private JTextField Input = new JTextField();

        private static final String[] names = 
          {"7", "8", "9", "4", "5", "6","1","2","3"};

        private static final String[] topButtons = 
              {"MC","MR","MS","M+","M-","<-","CE","C","+-","Root"};

       // no-argument constructor
       public GridLayoutFrame()
       {
          super ("Calculator");
          Bottom = new JPanel();  
          Top = new JPanel();
          Top.setLayout(new GridLayout(3,1,5,5));   // Three Rows (first row must only show a text field
          Bottom.setLayout(new GridLayout(5,5,10,10));
          topButton = new JButton[topButtons.length];
          buttons = new JButton[ names.length ];


          Input.setSize(500,500);
          Input.setEditable(false);
          Input.setBackground(Color.WHITE);
          Input.setPreferredSize(new Dimension(300, 30));

          for ( int count = 0; count < topButtons.length; count++ ) 
          {
             topButton[ count ] = new JButton( topButtons[count]);
             Top.add( topButton[ count ] ); // add button to panel
          }


          for ( int count = 0; count < names.length; count++ )

          {
             buttons[ count ] = new JButton( names[count]);
             Bottom.add( buttons[ count ] ); // add button to panel
          }

         add(Top,BorderLayout.NORTH);
         //add(Bottom,BorderLayout.CENTER);

       }
    } // end class GridLayoutFrame

代码的问题是,文本字段出现在顶部按钮出现的同一行中。 请帮忙

2 个答案:

答案 0 :(得分:2)

最好使用gridbaglayout满足您的要求,因为它非常灵活。 您可以查看http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

我修改了你的程序,使用GridBagLayout作为顶部面板。我认为它符合您对顶级面板的期望。

public GridLayoutFrame() {
        super("Calculator");
        Bottom = new JPanel();
        Top = new JPanel();
        Top.setLayout(new GridBagLayout()); // Three Rows (first row must
                                                    // only show a text field
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;


        topButton = new JButton[topButtons.length];

        Input.setSize(500, 500);
        Input.setEditable(false);
        Input.setBackground(Color.WHITE);
        Input.setPreferredSize(new Dimension(300, 30));

        c.gridwidth = 5;
        Top.add(Input, c);
        c.gridwidth = 1;


        for (int count = 0; count < topButtons.length; count++) {
            topButton[count] = new JButton(topButtons[count]);
            c.gridx = count % 5;
            Top.add(topButton[count], c ); // add button to panel
        }

        Bottom.setLayout(new GridLayout(5, 5, 10, 10));
        buttons = new JButton[names.length];
        for (int count = 0; count < names.length; count++)

        {
            buttons[count] = new JButton(names[count]);
            Bottom.add(buttons[count]); // add button to panel
        }

        add(Top, BorderLayout.NORTH);
        add(Bottom,BorderLayout.CENTER);

答案 1 :(得分:1)

编辑:不要理解我之前的JPanel建议。这应该这样做:

add(InputPanel, BorderLayout.NORTH);
add(Top,BorderLayout.CENTER);
add(Bottom,BorderLayout.SOUTH);