我想这样做,如果我点击btnNewButton它会更改textArea上的文本

时间:2014-02-24 17:26:50

标签: java swing jframe

我尝试过textArea.setText();但这不起作用。我真的不知道还能做什么。我知道可能有一些简单的方法可以做到这一点,但我很难过。请帮忙。我认识java。但是摇摆只会让我感到困惑。

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);
    }
}

2 个答案:

答案 0 :(得分:5)

使用按钮注册textArea时,尚未定义

ActionListener。声明(作为final)并在注册侦听器之前首先初始化组件。

final JTextArea textArea = new JTextArea();

答案 1 :(得分:0)

试试这个

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;
private JTextArea textArea;

/**
 * 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);

    String shape = "";
    double value = 0;

    JButton btnNewButton = new JButton("Go");
    btnNewButton.addActionListener(new ActionListener() {
        String shape = textField.getText();
        String value = textField_1.getText();
        double value1 = Double.parseDouble(value);
        textArea.setText("SET TEXT");


    });
    panel_1.add(btnNewButton);

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

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


}