从JButton的actionPerformed中更改JTextArea文本,该文本位于另一个类中

时间:2014-05-26 01:38:50

标签: java swing jbutton jtextarea

我有一个功能齐全的基于控制台的数据库,我需要添加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);
    }

1 个答案:

答案 0 :(得分:4)

您的代码无法正常工作,原因与此无关:

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

你必须在之前声明变量在Java中使用它们。

其次,为了使用inner class内部的变量(本地或实例) - 这就是您的ActionListener - 您需要将其设为final

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

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