JPanel 布局 - 添加文本框和重新排列组件

JPanel layout - adding text box and rearranging components

我的 Swing GUI 有一些问题。

目前看起来是这样的:

但我想移动一些东西。

  1. 首先我想要键盘下面的按钮
  2. 我想在键盘顶部添加一个文本字段,提交按钮在右侧。

我怎样才能做到这一点?我尝试创建一个 GridLayout 并按行、列坐标放置东西,但它似乎不起作用。

  private class Display extends JPanel {
    Display() {
      setPreferredSize(new Dimension(620, 420));
      setBackground(new Color(250, 230, 180));
      setFont(new Font("Serif", Font.BOLD, 20));
    }

    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      ((Graphics2D) g).setStroke(new BasicStroke(3));
      if (message != null) {
        g.setColor(Color.RED);
        g.drawString(message, 30, 40);
        g.drawString("00:00", 30, 410);
      }
    }
  }

  private void createWindow() {
    setJMenuBar(menuBarCreator());

    // The ActionListener that will respond to button clicks.
    ButtonHandler buttonHandler = new ButtonHandler();

    // Create the subpanels and add them to the main panel.
    display = new Display();
    setLayout(new BorderLayout(3, 3));
    add(display, BorderLayout.CENTER);
    JPanel bottom = new JPanel();
    bottom.setLayout(new GridLayout(1,1));
    add(bottom, BorderLayout.NORTH);

    // Add keyboard
    JPanel keyboard = new JPanel();
    JPanel keyboardHolder = new JPanel();
    keyboard.setLayout(new GridLayout(2, 13));
    keyboardHolder.setLayout(new GridLayout(1, 2));
    for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
      JButton button = new JButton(String.valueOf(alphabet));
      button.addActionListener(buttonHandler);
      keyboard.add(button);
      alphabetButtons.add(button);
    }
    keyboardHolder.add(keyboard, 0,0);
    add(keyboardHolder, BorderLayout.SOUTH);

    // Create three buttons, register the ActionListener to respond to clicks on the
    // buttons, and add them to the bottom panel.

    JButton submitButton = new JButton("Submit");
    submitButton.addActionListener(buttonHandler);
    keyboard.add(submitButton);

    JButton startButton = new JButton(GuiText.START.toString());
    startButton.addActionListener(buttonHandler);
    bottom.add(startButton);

    JButton nextButton = new JButton(GuiText.NEXT.toString());
    nextButton.addActionListener(buttonHandler);
    bottom.add(nextButton);

    JButton skipButton = new JButton(GuiText.SKIP.toString());
    skipButton.addActionListener(buttonHandler);
    bottom.add(skipButton);

    JButton quit = new JButton(GuiText.QUIT.toString());
    quit.addActionListener(buttonHandler);
    bottom.add(quit);

    setBackground(new Color(100, 0, 0));

    nextButton.setEnabled(false);
    skipButton.setEnabled(false);
  }

下面是具体的例子camickr wrote in his comment to the original question. Note that this is not the only possibility. There are many layout managers. I recommend visiting Laying Out Components Within a Container

代码的目的只是向您展示如何实现您想要的布局。

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

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

public class GuesGame implements Runnable {
    private JFrame frame;

    public void run() {
        showGui();
    }

    private JPanel createBottomPanel() {
        JPanel bottomPanel = new JPanel(new GridLayout(3, 1));
        bottomPanel.add(createSubmitPanel());
        bottomPanel.add(createKeyboardPanel());
        bottomPanel.add(createButtonsPanel());
        return bottomPanel;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        JButton startButton = new JButton("Start");
        buttonsPanel.add(startButton);
        JButton nextButton = new JButton("Next");
        buttonsPanel.add(nextButton);
        JButton skipButton = new JButton("Skip");
        buttonsPanel.add(skipButton);
        JButton quitButton = new JButton("Quit");
        buttonsPanel.add(quitButton);
        return buttonsPanel;
    }

    private JPanel createKeyboardPanel() {
        JPanel keyboardPanel = new JPanel(new GridLayout(2, 13));
        for (char c = 'a'; c <= 'z'; c++) {
            JButton button = new JButton(String.valueOf(c));
            keyboardPanel.add(button);
        }
        return keyboardPanel;
    }

    private JPanel createSubmitPanel() {
        JPanel submitPanel = new JPanel();
        JTextField txtFld = new JTextField(20);
        submitPanel.add(txtFld);
        JButton submitButton = new JButton("Submit");
        submitPanel.add(submitButton);
        return submitPanel;
    }

    private void showGui() {
        frame = new JFrame("Guess Game");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(new Display(), BorderLayout.CENTER);
        frame.add(createBottomPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new GuesGame());
    }
}

class Display extends JPanel {
    private String message;

    Display() {
        message = "Starting game";
        setPreferredSize(new Dimension(620, 420));
        setBackground(new Color(250, 230, 180));
        setFont(new Font("Serif", Font.BOLD, 20));
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ((Graphics2D) g).setStroke(new BasicStroke(3));
        if (message != null) {
            g.setColor(Color.RED);
            g.drawString(message, 30, 40);
            g.drawString("00:00", 30, 410);
        }
    }
}

您想要 Display 面板下方的三个 "rows",如下所示

  1. 文本字段和 "submit" 按钮。
  2. 键盘
  3. 其他按钮。

因此 "bottom" 面板包含三个面板,一个放在另一个上面。
第一个面板是文本字段和 "submit" 面板。
下面是 "keyboard".
键盘下方是其他按钮。

请注意,JPanel 的默认布局管理器是 java.awt.FlowLayout,此布局管理器适用于包含 "submit" 按钮的面板,也适用于包含其他按钮的面板.

这是 运行 应用程序的屏幕截图。