JTextField不会按用户读取输入

时间:2014-12-14 08:54:31

标签: java swing textfield

这里我有一个问题,如果我预先设置JTextField值为“test” 当我点击可以返回“test”的按钮时。

但是当我更改了(按用户输入)“BUG”等值时,再次单击该按钮,它也会返回“test”。

代码集PortTextField.setText("9999"); 然后我尝试在这个字段上输入8888, 然后点击按钮,按9999getText打印PortTextField JTextField值似乎无法更新

Server.java :(代码的一部分)

public boolean PortisInteger(String value) {
    try {
        this.portNumber = Integer.parseInt(value);
        return true;
    } catch(NumberFormatException ex) {
        System.out.println(ex);
        return false;
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    Object scr = e.getActionCommand();
    if (scr.equals("Start") && (serverSocket == null || serverSocket.isClosed())) {
        // new JFrame().StartServerToggleButton.setEnabled(false);
        if(PortisInteger(**new JFrame().getPortTextField()**)) {
            if(ServerStart(portNumber)) {
                new JFrame().setIPLabel("IP: " + serverSocket.getInetAddress().getHostName());
            }
        }
    }

JFrame.java:

package Server;

public class JFrame extends javax.swing.JFrame {
    public JFrame() {
        initComponents();
        Server svr = new Server();
        StartServerToggleButton.addActionListener(svr);
        CloseServerToggleButton.addActionListener(svr);
    }

    public void setIPLabel(String IPString) {
        IPLabel.setText(IPString);
    }

    **public String getPortTextField() {
        return PortTextField.getText().trim();
    }**

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        PortTextField = new javax.swing.JTextField();
        PortLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        MessageTextArea = new javax.swing.JTextArea();
        StartServerToggleButton = new javax.swing.JToggleButton();
        CloseServerToggleButton = new javax.swing.JToggleButton();
        IPLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chatroom-Server");

        PortTextField.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        PortTextField.setText("9999");

        PortLabel.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        PortLabel.setText("Port:");

        MessageTextArea.setEditable(false);
        MessageTextArea.setColumns(20);
        MessageTextArea.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        MessageTextArea.setRows(5);
        jScrollPane1.setViewportView(MessageTextArea);

        StartServerToggleButton.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        StartServerToggleButton.setText("Start");

        CloseServerToggleButton.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        CloseServerToggleButton.setText("Close");

        IPLabel.setFont(new java.awt.Font("Harlow Solid Italic", 0, 18)); // NOI18N
        IPLabel.setText("IP: 0.0.0.0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(IPLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(PortLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(PortTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(16, 16, 16)
                                                .addComponent(StartServerToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(CloseServerToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(PortTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(StartServerToggleButton)
                                        .addComponent(CloseServerToggleButton)
                                        .addComponent(PortLabel)
                                        .addComponent(IPLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JToggleButton CloseServerToggleButton;
    public javax.swing.JLabel IPLabel;
    public javax.swing.JTextArea MessageTextArea;
    public javax.swing.JLabel PortLabel;
    public javax.swing.JTextField PortTextField;
    public javax.swing.JToggleButton StartServerToggleButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

2 个答案:

答案 0 :(得分:1)

每次获得portTextField时都会创建一个新的JFrame实例,这是错误的,所以接收它的初始值是正常的。您必须获得JFrame正在运行的实例的引用。

答案 1 :(得分:0)

您没有获得用户输入的原因是因为此代码:

if(PortisInteger(new JFrame().getPortTextField())) {

每次执行上述代码时,您都在创建一个新的JFrame实例。这指的是JFrame的不同实例,而不是具有包含用户输入的JTextField的实例。


我建议你做以下事情:


在Server.java

中添加之前您的Server构造函数
private JFrame frame;


在Server.java

中添加内部您的Server构造函数
frame = new JFrame();


将Server.java中的actionPerformed方法修改为:

public void actionPerformed(ActionEvent e) {
Object scr = e.getActionCommand();
if (scr.equals("Start") && (serverSocket == null || serverSocket.isClosed())) {
    // new JFrame().StartServerToggleButton.setEnabled(false);

    if(PortisInteger(frame.getPortTextField())) {
        if(ServerStart(portNumber)) {
            frame.setIPLabel("IP: " + serverSocket.getInetAddress().getHostName());
        }
    }
}



希望这可以帮助。祝你好运,玩得开心!

干杯,

高远