无法在JTextfield中键入文本

时间:2015-05-12 04:57:15

标签: java swing jtextfield

所以,我刚写了一个非常简单的程序,它有1个JLabel,1个JTextField和2个JButton。问题是JTextField不允许我输入任何文本。我一直在努力思考这个问题,所以任何帮助都会受到赞赏!

Animated picture

/* Method: init() */
/**
 * This method has the responsibility for reading in the data base
 * and initializing the interactors at the bottom of the window.
 */
public void init() {
    setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
    setFont("Courier-24");

    nameLabel = new JLabel("Name");
    add(nameLabel, SOUTH);

    nameField = new JTextField(10);
    add(nameField, SOUTH);
    nameField.addActionListener(this);

    graphButton = new JButton("Graph");
    add(graphButton, SOUTH);

    clearButton = new JButton("Clear");
    add(clearButton, SOUTH);

    addActionListeners();
}

/* Method: actionPerformed(e) */
/**
 * This class is responsible for detecting when the buttons are
 * clicked, so you will have to define a method to respond to
 * button actions.
 */
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == nameField) 
        println("Graph: " + nameField.getText());
    if (e.getSource() == graphButton)
        println("Graph");
    if (e.getSource() == clearButton)
        println("Clear");
}

/* Private instance variables */
private JLabel nameLabel;
private JTextField nameField;
private JButton graphButton;
private JButton clearButton;

1 个答案:

答案 0 :(得分:1)

适合我...

Field

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author swhitehead
 */
public class JavaApplication1251 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new JavaApplication1251();
    }

    public JavaApplication1251() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements ActionListener {

        public TestPane() {
            init();
        }

        public void init() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            nameLabel = new JLabel("Name");
            add(nameLabel, gbc);

            nameField = new JTextField(10);
            add(nameField, gbc);
            nameField.addActionListener(this);

            graphButton = new JButton("Graph");
            add(graphButton, gbc);

            clearButton = new JButton("Clear");
            add(clearButton, gbc);

//          addActionListeners();
        }

        /* Method: actionPerformed(e) */
        /**
         * This class is responsible for detecting when the buttons are clicked, so you will have to define a method to respond to button actions.
         */
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == nameField) {
                System.out.println("Graph: " + nameField.getText());
            }
            if (e.getSource() == graphButton) {
                System.out.println("Graph");
            }
            if (e.getSource() == clearButton) {
                System.out.println("Clear");
            }
        }

        /* Private instance variables */
        private JLabel nameLabel;
        private JTextField nameField;
        private JButton graphButton;
        private JButton clearButton;
    }

}

考虑提供展示您问题的runnable example。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将减少混淆和更好的响应