我制作了一个GUI来反转一个输入字符串,我从第一个textfield获取输入,然后使用函数strRev()反转这个字符串,然后尝试在第二个textfield上生成结果。但是在第二个文本框上没有得到任何输出。
package com.awt;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
public class Buttons{
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Buttons window = new Buttons();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Buttons() {
initialize();
}
public String strRev(String s) {
char a;
String ans="";
for(int i=0;i<s.length();i++) {
a=s.charAt(i);
ans=a+ans;
}
return ans;
}
private void initialize() {
frame = new JFrame("Label Demo");
frame.setBounds(150, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
textField = new JTextField();
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton okButton = new JButton("Reverse");
frame.getContentPane().add(okButton);
textField_1 = new JTextField();
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
String str=textField.getText();
final String ans=strRev(str);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField_1.setText(ans);
}
});
}
}GUI如下所示:-

请告诉我我的错误,谢谢
发布于 2021-12-15 06:38:00
只有当用户单击反向按钮时,才需要读取textField的内容。目前,当GUI加载时,当textField中没有任何内容时,您将读取内容。因此,对空字符串进行反向调用将给您提供一个空字符串,这就是您在textField_1中看到的。将读取textField的代码移动到addActionListener中。
private void initialize() {
frame = new JFrame("Label Demo");
frame.setBounds(150, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
textField = new JTextField();
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton okButton = new JButton("Reverse");
frame.getContentPane().add(okButton);
textField_1 = new JTextField();
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// THIS BLOCK IS RUN EVERYTIME THE BUTTON IS CLICKED!
// Read the contents of textField
String str=textField.getText();
// Reverse the string you just read
final String ans=strRev(str);
// Set the answer on the other textField.
textField_1.setText(ans);
}
});
}https://stackoverflow.com/questions/70359269
复制相似问题