如果按下按钮,我只是尝试将文本追加到文本区域。我将其分离,因此textArea在另一个名为TextPanel的类中进行。有趣的是,如果我按下该按钮,并且还在控制台上运行,我也会将文本打印到控制台,只是无法将其追加到我的textArea ...
这是我的代码:

工具栏

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Toolbar extends JPanel implements ActionListener{

    private JButton helloButton;
    private JButton goodbyeButton;
    private TextPanel textPanel;

    public Toolbar() {

        helloButton = new JButton("Hello!");
        goodbyeButton = new JButton("Goodbye!");
        textPanel = new TextPanel();

        helloButton.addActionListener(this);
        goodbyeButton.addActionListener(this);

        setLayout(new FlowLayout(FlowLayout.LEFT));

        add(helloButton);
        add(goodbyeButton);
    }

    public void actionPerformed(ActionEvent e) {

        JButton clicked = (JButton)e.getSource();

        if (clicked == helloButton) {
            textPanel.appendText("Hello!\n");
            System.out.println("Hello!\n");
        }
        else if (clicked == goodbyeButton) {
            textPanel.appendText("Goodbye!\n");
            System.out.println("Goodbye!\n");
        }
    }
    /*
    public void setTextPanel(TextPanel textPanel) {
        this.textPanel = textPanel;
    }
    */
}




TextPanel.java

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TextPanel extends JPanel{

    private JTextArea textArea;

    public TextPanel() {

        textArea = new JTextArea();
        setLayout(new BorderLayout());
        add(new JScrollPane(textArea), BorderLayout.CENTER);
    }

    public void appendText(String text) {
        textArea.append(text);
    }
}



MainFrame.java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class MainFrame extends JFrame{

    private JButton btn;
    private TextPanel textPanel;
    private Toolbar toolbar;

    public MainFrame() {

        super("My First JAVA Swing Window");
        setLayout(new BorderLayout());

        textPanel = new TextPanel();
        toolbar = new Toolbar();

        add(textPanel, BorderLayout.CENTER);
        add(toolbar, BorderLayout.NORTH);

        setSize(600,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

}


App.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class App {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame();
            }
        });

    }

}

最佳答案

您需要使用add(textPanel);添加textPanel对象



由于增加了Main class代码:扩展了答案。
使用textPanel = new TextPanel()创建工具栏后,从工具栏类中删除setTextPanel,取消注释toolbar.setTextPanel(textPanel)方法并调用toolbar = new Toolbar()
问题是您要创建两个textPanel实例,并且只修改尚未添加到框架的实例。

08-06 00:13