如何使用jtextfield和jbutton创建一个具有名称填充功能的菜单?

k3bvogb1  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(254)

所以我试着制作一个面板,玩家在其中配置本地棋盘游戏中玩家的名字。我希望他们能够添加2到6之间的球员,所以默认情况下,我已经使面板显示2 JTextField 在第二个框下有一个“+”按钮,可以添加一个额外的玩家。但我不知道如何做,以及如何执行这样的行动,当用户按下“+”按钮一个新的 JTextField 弹出窗口供玩家添加新的玩家名称。
我提供的代码,我用这个特定的面板,但它是必要的,我可以提供我的窗口的整个代码。如有任何帮助,我们将不胜感激。

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);
        }
    });
}

附言 localPanel 有一个 GridBagLayout .

afdcj2ne

afdcj2ne1#

你需要添加一个 ActionListenerJButton 而不是一个 MouseListener . 请参阅如何使用按钮。
+ 按钮,您想添加一个新的 JTextFieldJPanel 包含允许用户输入玩家姓名的字段。
下面是根据您的问题中的代码改编的代码,它添加了 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());
    }
}

注意当你打电话的时候 setContentAreaFilled(false)JButton ,不需要也打电话 setOpaque(false) .
而不是 new java.awt.Color(255, 255, 255) 你可以用常数 WHITE 在课堂上 java.awt.Color . GridLayout 以及 GridBagLayout 都不一样。参见在容器内布置部件
我没有像您那样使用背景图像,而是将背景颜色设置为深灰色。

相关问题