如何用JTextField 和JButton 制作一个具有填名功能的菜单?

How to make with JTextField and JButton a menu with name filling functions?

所以我正在尝试制作一个面板,玩家可以在其中配置本地棋盘游戏中玩家的名字。 我希望他们能够添加 2 到 6 之间的玩家,所以默认情况下我让面板显示 2 JTextField 框供他们添加名称,在第二个框下方有一个“+”按钮来添加额外的球员。但是我不知道该怎么做以及如何执行这样的操作,当用户按下“+”按钮时,会弹出一个新的 JTextField 供玩家添加新的玩家名称。

我正在提供我用于这个特定面板的代码,但我有必要提供我的 window 的完整代码。感谢任何帮助,并提前致谢。

private void setLocalPanel() {

    GridBagConstraints c = new GridBagConstraints();
    
    // localButtonsPanel config
    localButtonsPanel = new JPanel(new GridLayout(0, 1, 0, 10));
    localButtonsPanel.setOpaque(false);
    
    // nameBox1 config
    nameBox1 = new JTextField("Player name");
    nameBox1.setBackground(new Color(255, 255, 255));
    nameBox1.setEditable(true);
    c.ipady = 10;
    localButtonsPanel.add(nameBox1, c);

    // nameBox2 config
    nameBox2 = new JTextField("Player name");
    nameBox2.setBackground(new Color(255, 255, 255));
    nameBox2.setEditable(true);
    localButtonsPanel.add(nameBox2, c);
    
    // + config
    extraBtn = new JButton("+");
    extraBtn.setForeground(new Color(255, 255, 255));
    extraBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
    extraBtn.setOpaque(false);
    extraBtn.setContentAreaFilled(false);
    extraBtn.setBorderPainted(false);
    extraBtn.setFocusPainted(false);
    localButtonsPanel.add(extraBtn, new GridBagConstraints());
    
    c.gridy = 0;
    localPanel.add(localButtonsPanel, c);
    
    // startBtn config
    startBtn = new JButton("Start");
    startBtn.setForeground(new Color(255, 255, 255));
    startBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
    startBtn.setOpaque(false);
    startBtn.setContentAreaFilled(false);
    startBtn.setBorderPainted(false);
    startBtn.setFocusPainted(false);
    c.gridy = 1;
    localPanel.add(startBtn, c);
    
    // localBackBtn config
    localBackBtn = new JButton("Back");
    localBackBtn.setForeground(new Color(255, 255, 255));
    localBackBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
    localBackBtn.setOpaque(false);
    localBackBtn.setContentAreaFilled(false);
    localBackBtn.setBorderPainted(false);
    localBackBtn.setFocusPainted(false);
    c.gridy = 2;
    localPanel.add(localBackBtn, c);
    
    setLocalActions();
}
private void setLocalActions() {
    
    // startBtn config
    startBtn.addMouseListener(new MouseAdapter() {

        public void mouseEntered(MouseEvent e) {
            startBtn.setForeground(new Color(200, 210, 10));
            startBtn.setText("> Start <");
        }
        
        public void mouseExited(MouseEvent e) {
            startBtn.setForeground(new Color(255, 255, 255));
            startBtn.setText("Start");
        }
        
        public void mouseClicked(MouseEvent e) {
            // nameBox1.getText()
            // nameBox2.getText()
            localPanel.setVisible(false);
            gamePanel.setVisible(true);
        }
    });
    
    // localBackBtn config
    localBackBtn.addMouseListener(new MouseAdapter() {

        public void mouseEntered(MouseEvent e) {
            localBackBtn.setForeground(new Color(200, 210, 10));
            localBackBtn.setText("> Back <");
        }
        
        public void mouseExited(MouseEvent e) {
            localBackBtn.setForeground(new Color(255, 255, 255));
            localBackBtn.setText("Back");
        }
        
        public void mouseClicked(MouseEvent e) {
            localPanel.setVisible(false);
            playPanel.setVisible(true);
        }
    });
}

PS。 localPanel 有一个 GridBagLayout.

您需要将 ActionListener 添加到 JButton 而不是 MouseListener。参考How to Use Buttons.

单击 + 按钮后,您想要向 JPanel 添加一个新的 JTextField,其中包含允许用户输入玩家姓名的字段。

这是根据您问题中的代码改编的代码,当用户单击 + 按钮时会添加一个 JTextField。关于代码的注释出现在它后面。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

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

public class PlayGame implements ActionListener, Runnable {
    private static final int  MAX_PLAYERS = 6;
    private static final String  BACK = "Back";
    private static final String  PLUS = "+";
    private static final String  START = "Start";

    private ArrayList<JTextField>  playerNameTextFields;
    private GridBagConstraints  gbc;
    private JFrame  frame;
    private JPanel  localButtonsPanel;

    public PlayGame() {
        playerNameTextFields = new ArrayList<>();
    }

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    @Override // java.awt.event.ActionListener
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case BACK:
                // Add code to execute when user clicks 'Back' button.
                break;
            case PLUS:
                addPlayerNameTextField();
                localButtonsPanel.revalidate();
                localButtonsPanel.repaint();
                if (playerNameTextFields.size() >= MAX_PLAYERS) {
                    ((JButton) event.getSource()).setEnabled(false);
                }
                break;
            case START:
                // Add code to execute when user clicks 'Start' button.
                break;
            default:
                System.out.println("Not implemented: " + actionCommand);
        }
    }

    private void addPlayerNameTextField() {
        JTextField playerNameTextField = new JTextField(10);
        playerNameTextField.setName("player" + playerNameTextFields.size());
        playerNameTextFields.add(playerNameTextField);
        gbc.gridy++;
        localButtonsPanel.add(playerNameTextField, gbc);
    }

    private JPanel createLocalPanel() {
        JPanel localPanel = new JPanel(new GridBagLayout());
        localPanel.setOpaque(false);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.insets.top = 10;
        localButtonsPanel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = -1;
        gbc.insets.top = 10;
        localButtonsPanel.setPreferredSize(new Dimension(120, 200));
        localButtonsPanel.setOpaque(false);
        addPlayerNameTextField();
        addPlayerNameTextField();
        localPanel.add(localButtonsPanel, c);
        c.gridy = 1;
        JButton extraBtn = new JButton(PLUS);
        extraBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
        extraBtn.setForeground(Color.WHITE);
        extraBtn.setContentAreaFilled(false);
        extraBtn.setBorderPainted(false);
        extraBtn.setFocusPainted(false);
        extraBtn.addActionListener(this);
        extraBtn.setToolTipText("Add another player");
        localPanel.add(extraBtn, c);
        c.gridy = 2;
        JButton startBtn = new JButton(START);
        startBtn.setForeground(Color.WHITE);
        startBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
        startBtn.setContentAreaFilled(false);
        startBtn.setBorderPainted(false);
        startBtn.setFocusPainted(false);
        startBtn.addActionListener(this);
        localPanel.add(startBtn, c);
        JButton localBackBtn = new JButton(BACK);
        localBackBtn.setForeground(Color.WHITE);
        localBackBtn.setFont(new Font("Segoe Script", Font.BOLD,40));
        localBackBtn.setContentAreaFilled(false);
        localBackBtn.setBorderPainted(false);
        localBackBtn.setFocusPainted(false);
        localBackBtn.addActionListener(this);
        c.gridy = 3;
        localPanel.add(localBackBtn, c);
        return localPanel;
    }

    private void showGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        frame.add(createLocalPanel(), BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new PlayGame());
    }
}

请注意,当您在 JButton 上调用 setContentAreaFilled(false) 时,无需同时调用 setOpaque(false)

您可以在 class java.awt.Color.

中使用常量 WHITE 而不是 new java.awt.Color(255, 255, 255)

GridLayoutGridBagLayout 不一样。参考Laying Out Components Within a Container

我没有像您那样使用背景图片,而是将背景颜色设置为深灰色。