gpt4 book ai didi

java - 我想要做到这一点,所以如果我点击 btnNewButton 它会更改 textArea 上的文本

转载 作者:行者123 更新时间:2023-12-01 17:16:12 25 4
gpt4 key购买 nike

我尝试过textArea.setText();但这行不通。我真的不知道还能做什么。我知道可能有一些简单的方法可以做到这一点,但我很困惑。请帮忙。我知道java。但 Swing 让我感到困惑。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


public class GeoUI extends JFrame{


private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
final JTextArea textArea = new JTextArea();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GeoUI frame = new GeoUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GeoUI() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 150);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(0, 3, 0, 0));

JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(new GridLayout(3, 1, 0, 0));

JLabel lblRadiussidelength = new JLabel("Radius/SideLength:");
lblRadiussidelength.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblRadiussidelength);

JLabel lblShape = new JLabel("Shape:");
lblShape.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblShape);

JPanel panel_1 = new JPanel();
contentPane.add(panel_1);
panel_1.setLayout(new GridLayout(3, 1, 0, 0));

textField_1 = new JTextField();
panel_1.add(textField_1);
textField_1.setColumns(10);

textField = new JTextField();
panel_1.add(textField);
textField.setColumns(10);



JButton btnNewButton = new JButton("Go");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText("slflkdsjfskl");

}


});
panel_1.add(btnNewButton);

JPanel panel_2 = new JPanel();
contentPane.add(panel_2);
panel_2.setLayout(new GridLayout(1, 0, 0, 0));

JTextArea textArea = new JTextArea();
textArea.setEditable(false);
panel_2.add(textArea);
}
}

最佳答案

ActionListener 注册到按钮时,

textArea 尚未定义。在注册监听器之前,先声明(作为 final)并初始化组件。

final JTextArea textArea = new JTextArea();

关于java - 我想要做到这一点,所以如果我点击 btnNewButton 它会更改 textArea 上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21994640/

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