简体   繁体   中英

Java gui layout

How can I make a gui layout in java which is column based? by that i mean:

JLabel
JTextField
JLabel
JTextField
JLabel
JTextField

I want them all stacked on to of each other, rather than side by side. In the past I've used FlowLayout which isn't suitable, and gridLayout which makes each component much larger than I require. Any ideas?

Look at BoxLayout , but more important, go through the layout manager tutorial to get an overview of all the user-friendly layout managers .

Edit 1:
Also, GridLayout sometimes works well in this situation, but you may want to place your JTextFields inside of JPanels and add the JPanels to the grid, so that the JTextFields aren't huge looking.

Edit 2:
If you have a bunch of JTextFields with associated JLabels, I've had success and fun working from an array of String that represents the JLabel texts, and then placing the JTextFields into a Map<String, JTextField> so that I can easily get a reference to the JTextField based on it's related String. Example to follow...

Edit 3
as promised, the example:

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
public class LabelsAndFields {
   public static final String[] LABEL_TEXTS = {
      "Sunday", "Monday", "Tuesday",
      "Wednesday", "Thursday", "Friday", "Saturday"};

   private JPanel mainPanel = new JPanel();
   private static final int FIELD_COLS = 10;
   private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();

   public LabelsAndFields() {
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
      for (String labelText : LABEL_TEXTS) {
         JTextField textField = new JTextField(FIELD_COLS);
         textFieldMap.put(labelText, textField);
         JPanel panel = new JPanel(new BorderLayout());
         panel.add(new JLabel(labelText), BorderLayout.NORTH);
         panel.add(textField);
         int gap = 8;
         panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
         mainPanel.add(panel);
      }
   }

   public String getText(String labelTextKey) {
      JTextField field = textFieldMap.get(labelTextKey);
      if (field == null) {
         throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
      }

      return field.getText();
   }

   public void setText(String labelTextKey, String text) {
      JTextField field = textFieldMap.get(labelTextKey);
      if (field == null) {
         throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
      }

      field.setText(text);
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private static void createAndShowUI() {
      final LabelsAndFields labelsAndFields = new LabelsAndFields();

      JButton showAllTextBtn = new JButton(new AbstractAction("Show All Text") {
         @Override
         public void actionPerformed(ActionEvent arg0) {
            for (String labelText : LabelsAndFields.LABEL_TEXTS) {
               System.out.printf("%10s: %s%n", labelText, labelsAndFields.getText(labelText));
            }
         }
      });

      JFrame frame = new JFrame("LabelsAndFields");
      frame.getContentPane().add(labelsAndFields.getMainPanel(), BorderLayout.CENTER);
      frame.getContentPane().add(showAllTextBtn, BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

you could use a BoxLayout and in the constructor set the layout type to Box.Y_AXIS, and with this all of the widgets will fall under the previous one added, OR you could still use the GridLayout but add each individual widget to its own JPanel before adding that JPanel to the GridLayout, that will prevent the oversizing problem that you would normally have, and btw you dont have to store any reference to the JPanel's just instantiate, add a widgets, and add the panel until all widgets are taken care of

BoxLayout is what you need:

getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

I'd suggest MigLayout, it's very simple and very powerful.

In this case all you need to do is

JPanel p = new JPanel(new MigLayout("wrap")); 
p.add(new JLabel());
p.add(new JTextField());
p.add(new JLabel());

etc..

I would suggest gridlayout. You can choose he rows and columnsime a table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM