简体   繁体   English

如何使文本字段和标签位于7x3网格中?

[英]How do I get the textfields and the labels to sit in a 7x3 grid?

The first column in my grid always comes out right but then the rest begin replacing the other cells. 网格中的第一列总是正确显示,然后其余部分开始替换其他单元格。 Also the border layout does not seem to be functioning. 此外,边框布局似乎没有起作用。 I do not know what the problem is. 我不知道问题是什么。 It should have the title on top, a 7x3 grid in the center and the buttons on the bottom. 它的标题应该在顶部,中间是一个7x3的网格,底部应该是按钮。 Please help! 请帮忙! Thank you! 谢谢!

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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


public class GUI extends JFrame{

    private JPanel mainPanel,titlePanel, fieldPanel, buttonPanel;

    private JLabel title, teams, totalP, wlt;

    private JTextField team1, team2, team3, team4, team5, team6, total1, total2, total3, total4, total5, total6, wlt1, wlt2, wlt3, wlt4, wlt5, wlt6;

    private JButton read, calc, champWin, earthCW, exit;

    final private int WINDOW_HEIGHT = 400;
    final private int WINDOW_WIDTH = 900;

    public GUI(){

        buildtitlePanel();
        buildfieldPanel();
        buildbuttonPanel();
        buildmainPanel();

        setTitle("Desert Soccer League");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private void buildmainPanel() {

        mainPanel = new JPanel();

        mainPanel.setLayout(new BorderLayout());

        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(fieldPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

    }

    private void buildtitlePanel() {

        titlePanel = new JPanel();

        title = new JLabel();

        title.setText("2014 Desert Soccer League Totals");

        titlePanel.add(title);



    }

    private void buildfieldPanel() {

        fieldPanel = new JPanel();

        fieldPanel.setLayout(new GridLayout(7, 3));

        teams = new JLabel();
        teams.setText("Teams");

        totalP = new JLabel();
        totalP.setText("Total Points");

        wlt = new JLabel();
        wlt.setText("Win-Loss-Tie");

        team1 = new JTextField(10);
        team2 = new JTextField(10);
        team3 = new JTextField(10);
        team4 = new JTextField(10);
        team5 = new JTextField(10);
        team6 = new JTextField(10);

        total1 = new JTextField(10);
        total2 = new JTextField(10);
        total3 = new JTextField(10);
        total4 = new JTextField(10);
        total5 = new JTextField(10);
        total6 = new JTextField(10);

        wlt1 = new JTextField(10);
        wlt2 = new JTextField(10);
        wlt3 = new JTextField(10);
        wlt4 = new JTextField(10);
        wlt5 = new JTextField(10);
        wlt6 = new JTextField(10);

        team1.setEditable(false);
        team2.setEditable(false);
        team3.setEditable(false);
        team4.setEditable(false);
        team5.setEditable(false);
        team6.setEditable(false);

        total1.setEditable(false);
        total2.setEditable(false);
        total3.setEditable(false);
        total4.setEditable(false);
        total5.setEditable(false);
        total6.setEditable(false);

        wlt1.setEditable(false);
        wlt2.setEditable(false);
        wlt3.setEditable(false);
        wlt4.setEditable(false);
        wlt5.setEditable(false);
        wlt6.setEditable(false);

        fieldPanel.add(teams);
        fieldPanel.add(team1);
        fieldPanel.add(team2);
        fieldPanel.add(team3);
        fieldPanel.add(team4);
        fieldPanel.add(team5);
        fieldPanel.add(team6);

        fieldPanel.add(totalP);
        fieldPanel.add(total1);
        fieldPanel.add(total2);
        fieldPanel.add(total3);
        fieldPanel.add(total4);
        fieldPanel.add(total5);
        fieldPanel.add(total6);

        fieldPanel.add(wlt);
        fieldPanel.add(wlt1);
        fieldPanel.add(wlt2);
        fieldPanel.add(wlt3);
        fieldPanel.add(wlt4);
        fieldPanel.add(wlt5);
        fieldPanel.add(wlt6);



    }

    private void buildbuttonPanel() {

        buttonPanel = new JPanel();

        buttonPanel.setLayout(new GridLayout(1, 5));

        read = new JButton();
        calc = new JButton();
        champWin = new JButton();
        earthCW = new JButton();
        exit = new JButton();

        read.setText("Read Input File");
        read.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        calc.setText("Calculate Points");
        calc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        champWin.setText("Championship Winner");
        champWin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        earthCW.setText("Earth Cup Winner");
        earthCW.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        exit.setText("Exit");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        buttonPanel.add(read);
        buttonPanel.add(calc);
        buttonPanel.add(champWin);
        buttonPanel.add(earthCW);
        buttonPanel.add(exit);



    }


}
    mainPanel = new JPanel();

    mainPanel.add(titlePanel, BorderLayout.NORTH);
    mainPanel.add(fieldPanel, BorderLayout.CENTER);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

By default a JPanel uses a FlowLayout . 默认情况下, JPanel使用FlowLayout If you want to use a BorderLayout , then you need to set the layout on the panel: 如果要使用BorderLayout ,则需要在面板上设置布局:

    mainPanel = new JPanel( new BorderLayout() );

The GridLayout fills out the rows first so the code should be: GridLayout首先填充行,因此代码应为:

fieldPanel.add(teams);
fieldPanel.add(totalP);
fieldPanel.add(wlt);

fieldPanel.add(team1);
fieldPanel.add(total1);
fieldPanel.add(wlt1);
...

Also note that in your code you are adding the total? 还请注意,在您的代码中您要添加总数? fields twice (which won't do anything), instead of the team? 两次(什么都不做),而不是团队? fields. 领域。

Another way to specify the grid is to just use: 指定网格的另一种方法是只使用:

fieldPanel.setLayout(new GridLayout(0, 3));

This tells the grid to add 3 components to each row then move on to the next row. 这告诉网格向每行添加3个组件,然后移至下一行。 This way you don't have to worry about the exact number of rows. 这样,您不必担心确切的行数。

To add to camickr answer you're also adding the same total fields multiple times, so change this: 要添加到camickr答案中,您还要多次添加相同的总计字段,因此请更改以下内容:

fieldPanel.add(teams);
fieldPanel.add(total1);
fieldPanel.add(total2);
fieldPanel.add(total3);
fieldPanel.add(total4);
fieldPanel.add(total5);
fieldPanel.add(total6);

to

fieldPanel.add(teams);
fieldPanel.add(team1);
fieldPanel.add(team2);
fieldPanel.add(team3);
fieldPanel.add(team4);
fieldPanel.add(team5);
fieldPanel.add(team6);

This is what is causing your display issue. 这就是导致您的显示问题的原因。

Your code should look like: 您的代码应如下所示:

fieldPanel.add(teams);
fieldPanel.add(totalP);
fieldPanel.add(wlt);
fieldPanel.add(team1);
fieldPanel.add(total1);
fieldPanel.add(wlt1);
fieldPanel.add(team2);
fieldPanel.add(total2);
fieldPanel.add(wlt2);
// etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在RelativeLayout中获得不相互重叠的按钮 - How do I get buttons not to sit on top of each other in RelativeLayout 如何更改网格中的按钮标签? - How do I change button labels in a grid? 我想将标签和文本字段彼此对齐,并将所有按钮排成一行; 我该怎么做? - I want to align labels and textfields next to one other and all the buttons in a single row; how can i do it? 如何在Java的borderlayout中显示标签和文本字段? - How can i make my labels and textfields appear in a borderlayout in java? 如何存储textFields中的信息? - How do I store information from the textFields? 如何使用foreach循环从窗格中获取所有TextField? - How do I get all TextFields from a pane with foreach-loop? 如何在JavaFX中将SQLite与文本字段和标签正确连接? - How to correctly connect SQLite with textfields and labels in JavaFX? javafx:如何处理项目(文本字段,标签等)? - javafx: how to handle items(textfields, labels, etc)? 如何使GUI中的对象(文本字段等)消失? - How do I make objects (textfields etc.) in the GUI disappear? 如何通过文本字段将Java变量插入查询中? - How do I insert Java variables into query through textfields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM