如何通过单击在JButton上添加JTextField

如何通过单击在JButton上添加JTextField

本文介绍了如何通过单击在JButton上添加JTextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在用秋千做类似井字游戏的事情.我做了一个GridLayout和9个按钮,现在我想让我的一个jbutton在单击它时显示jtextfield吗?我尝试使用MouseListener,但没有设法以这种方式找到解决方案.有什么建议?

So I'm making something similar to tic-tac-toe using swing. I made a GridLayout and 9 buttons, and now i want one of my jbuttons to show the jtextfield when i click on it? I tried using MouseListener, but didn't manage to find a solution that way. Any suggestions?

推荐答案

您可以创建自定义按钮.

You can create a custom button.

一种方法是对按钮使用CardLayout并向其中添加JTextFieldJLabel.在按钮的ActionListener中,如果按下按钮,则显示文本字段并禁用按钮.在字段中输入文本并点击后,带有show的标签和按钮将被启用.

One way is to use a CardLayout for the button and add a JTextField and a JLabel to it. In the ActionListener of the button, if the button is pressed, you show the text field and disable the button. After you enter your text in the field and hit , the label with show, and the button is enabled.

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class TestButtonTextField {
    public TestButtonTextField() {
        JFrame frame = new JFrame();
        frame.add(new TextFieldButton("Hello"));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    protected class TextFieldButton extends JButton {

        private static final String FIELD = "field";
        private static final String LABEL = "label";

        private final CardLayout layout = new CardLayout();
        private final JLabel label;

        public TextFieldButton(String text) {
            super();
            setLayout(layout);

            label = new JLabel(text);
            label.setHorizontalAlignment(SwingConstants.CENTER);
            this.add(label, LABEL);

            final JTextField field = createField();
            this.add(field, FIELD);

            this.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    layout.show(TextFieldButton.this, FIELD);
                    TextFieldButton.this.setEnabled(false);
                }
            });

        }

        private JTextField createField() {
            final JTextField field = new JTextField(5);
            field.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    label.setText(field.getText());
                    field.setText("");
                    layout.show(TextFieldButton.this, LABEL);
                    TextFieldButton.this.setEnabled(true);
                }
            });
            return field;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestButtonTextField();
            }
        });
    }
}

这篇关于如何通过单击在JButton上添加JTextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:29