Domanda

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?

È stato utile?

Soluzione

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.

enter image description hereenter image description hereenter image description here

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top