Java将数据放入边框布局中

发布于 2025-01-04 18:51:37 字数 2416 浏览 3 评论 0原文

如何在Java中实现以下输出,如Windows计算器,

示例,

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

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

How to achieve the following output in Java like windows Calculator,

Example,

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

and the remaining Data in Center of border layout,

How to put first , second, third row in North of Border Layout together ? here is my code ,

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

The problem with the code is , the Text field is appearing in the same line where the top buttons are appearing.
please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2025-01-11 18:51:37

最好使用 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);

It is best to use gridbaglayout for the requirements you have as it is very flexible.
You can take a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

I have modified your program to use GridBagLayout for the top panel. I think it does what you expect for the top panel.

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);
厌倦 2025-01-11 18:51:37

编辑:别介意我之前的 JPanel 建议。这应该可以做到:

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

Edit: nevermind my earlier JPanel suggestion. This should do it:

add(InputPanel, BorderLayout.NORTH);
add(Top,BorderLayout.CENTER);
add(Bottom,BorderLayout.SOUTH);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文