如何让我的JTextField在标题下正确排列?

时间:2014-10-13 18:00:24

标签: user-interface jpanel jtextfield flowlayout

这应该是什么样子

enter image description here

import java.awt.*;  
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   

import javax.swing.*;

public class UI extends JFrame
 {
   private static final long serialVersionUID = 1L;
   private JPanel topPanel;
   private JPanel mainPanel;
   private JPanel botPanel;
   private JLabel message;
   private JTextField total, total1, total2, total3, total4, total5;
   private JTextField score, score1, score2, score3, score4,score5;
   private final int width = 650;       
   private final int height = 400;  

 public UI()
 {
    setTitle("Desert Soccer League");
    setSize(width,height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildTop();
    add(topPanel, BorderLayout.NORTH);
    buildMain();
    add(mainPanel, BorderLayout.CENTER);
    buildBot();
    add(botPanel, BorderLayout.SOUTH);
}

private void buildTop()
{
    topPanel = new JPanel(new FlowLayout());
    message = new JLabel("2014 Desert Soccer League");
    topPanel.add(message);
}

private void buildMain()
{
    mainPanel = new JPanel(new FlowLayout(0,100,20));
    JLabel team = new JLabel("Teams");
    JLabel points = new JLabel("Total Points");
    JLabel winLoss = new JLabel("Win-Loss-Tie");
    JTextField t = new JTextField(10);
    t.setText("Arabian");
    t.setEnabled(false);
    JTextField t1 = new JTextField(10);
    t1.setText("Gobi");
    t1.setEnabled(false);
    JTextField t2 = new JTextField(10);
    t2.setText("Outback");
    t2.setEnabled(false);
    JTextField t3 = new JTextField(10);
    t3.setText("Patagonian");
    t3.setEnabled(false);
    JTextField t4 = new JTextField(10);
    t4.setText("Sahara");
    t4.setEnabled(false);
    JTextField t5 = new JTextField(10);
    t5.setText("Sonoran");
    t5.setEnabled(false);

    mainPanel.add(team);
    mainPanel.add(points);
    mainPanel.add(winLoss);
    mainPanel.add(t);
    mainPanel.add(t1);
    mainPanel.add(t2);
    mainPanel.add(t3);
    mainPanel.add(t4);
    mainPanel.add(t5);
}

private void buildBot()
{
    botPanel = new JPanel(new FlowLayout());
    JButton io = new JButton("Read Input File");
    io.setActionCommand("I");
    io.addActionListener(new ButtonListener());
    JButton calc = new JButton("Calculate Points");
    calc.setActionCommand("C");
    calc.addActionListener(new ButtonListener());
    JButton champ = new JButton("Championship Winner");
    champ.setActionCommand("O");
    champ.addActionListener(new ButtonListener());
    JButton second = new JButton("Earth Cup Wiiner");
    second.setActionCommand("T");
    second.addActionListener(new ButtonListener());
    JButton exit = new JButton("Exit");
    exit.setActionCommand("E");
    exit.addActionListener(new ButtonListener5());

    botPanel.add(io);
    botPanel.add(calc);
    botPanel.add(champ);
    botPanel.add(second);
    botPanel.add(exit);
}

private class ButtonListener implements ActionListener
{
     public void actionPerformed(ActionEvent e)
      {
         if(e.getActionCommand() == "I")
         {
             JOptionPane.showInputDialog("Enter the path and the name of the file");
         }
      }
}

private class ButtonListener5 implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {
          if(e.getActionCommand() == "E")   
            {
                System.exit(0); 
            }

      }
}
public static void main(String[] args)
 {
    UI frame = new UI();
    frame.setVisible(true);
 }
}

我在JLabel团队下排队我的文本文件时遇到了麻烦。有人可以帮我排队我的第一行文本字段,如上例所示。我也尝试过GridLayout,但它似乎没有按照我想要的方式工作,因为它自动调整大小,看起来没什么吸引力。另外,因为这是一个类项目,我只能使用我所教过的内容,所以我们学到的唯一布局是BorderLayout,FlowLayout和GridLayout。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

将标签放在网格中,而不是放在网格上方的单独面板中。