gpt4 book ai didi

java - JTextField 未读取用户的输入

转载 作者:行者123 更新时间:2023-12-01 12:07:48 24 4
gpt4 key购买 nike

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

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

代码集PortTextField.setText("9999");然后我尝试在此字段中输入8888,然后单击按钮,它会通过 PortTextField 中的 getText 打印 9999JTextField 值似乎没有更新

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
}

最佳答案

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

关于java - JTextField 未读取用户的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com