繁体   English   中英

从另一个类中的JButton的actionPerformed更改JTextArea文本

[英]Change JTextArea texts from JButton's actionPerformed which is in another class

我有一个功能齐全的基于控制台的数据库,需要向其中添加GUI。 我创建了一个带有“显示所有学生”按钮的标签页(当前只有一个标签),该按钮页面在触发时将显示JTextArea中的学生列表,该列表当然在其自己的类中,而不是在按钮的动作侦听器类中。 问题是,按钮的动作侦听器内部无法识别JTextArea。 如果将参数添加到动作侦听器中,则会出现更多错误。 救命?

我已经在Stack Overflow中搜索了类似的问题,但是当我在代码中尝试过该方法时,这真的不可行吗? 或者,也许我只需要轻推一下脑袋。 无论如何。

到目前为止,这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StudDatabase extends JFrame 
{
    private     JTabbedPane tabbedPane;
    private     JPanel      studentPanel;
    private static Scanner input = new Scanner(System.in);    
    static int studentCount = 0;
    static Student studentArray[] = new Student[500];

    public StudDatabase()
    {
        setTitle("Student Database");
        setSize(650, 500);
        setBackground(Color.gray);

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the tab pages
        createStudentPage();
        // more tabs later...

        // Create a tab pane
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab( "Student Admin", studentPanel );
        topPanel.add( tabbedPane, BorderLayout.CENTER );
    }

    public void createStudentPage()
    {
        studentPanel = new JPanel();
        studentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        JButton listButton = new JButton("List All Student(s)");
        listButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent event) 
            {
                if(studentCount > 0) 
                {
                    for(int i=0; i<studentCount; i++)
                    {
                        // print out the details into JTextArea
                        // ERROR! textDisplay not recognised!!!
                        textDisplay.append("Student " + i);
                    }
                    System.out.printf("\n");
                }
                else // no record? display warning to user
                {
                    System.out.printf("No data to display!\n\n");
                }            
          }
        });
        studentPanel.add(listButton);

        JTextArea textDisplay = new JTextArea(10,48);
        textDisplay.setEditable(true); // set textArea non-editable
        JScrollPane scroll = new JScrollPane(textDisplay);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        studentPanel.add(scroll);
    }

    public static void main(String[] args) 
    {    
        StudDatabase mainFrame = new StudDatabase();
        mainFrame.setVisible(true);
    }

您的代码由于以下原因而无法正常工作:

int j = i+5;
int i = 4;

您必须先声明变量, 然后才能在Java中使用它们。

其次,为了从内部内部使用变量(局部变量或实例),这就是您的ActionListener ,您需要将其定为final

因此,以下代码将编译并运行:

final JTextArea textDisplay = new JTextArea(10,48);
 ...
listButton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent event) 
        {
        ...
        textDisplay.append("Student " + i);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM