How to add JTextField on a JButton just by clicking?

本秂侑毒 提交于 2019-12-11 21:24:43

问题


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?


回答1:


You can create a custom button.

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 enter, 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();
            }
        });
    }
}



回答2:


Use addActionListener to add a suitable listener to the button. You can toggle the visibility of a component using the setVisible(boolean) method. However, you probably have to resize/repaint the window after this action.

Edit: If you want to replace the whole window content, you might also want to take a look at Swing's CardLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html



来源:https://stackoverflow.com/questions/23311314/how-to-add-jtextfield-on-a-jbutton-just-by-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!