简体   繁体   English

textPanel.setText(“”); 无法运作或显示

[英]textPanel.setText(“”); doesn't work or display

Whenever I press "clear" in my Java GUI, it never works, please help me finish this. 每当我在Java GUI中按“清除”时,它就永远无法工作,请帮助我完成此操作。 If i replace "textPanel" with another button, it works, otherwise with "textpanel" it doesn't. 如果我将“ textPanel”替换为另一个按钮,它将起作用,否则,将其替换为“ textpanel”。

Here is a lightweight version of my code demonstrating the problem: 这是演示该问题的代码的轻量级版本:

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

public class MainFrame extends JFrame {
    private TextPanel textPanel;
    private FormPanel formpanel;

    public MainFrame(){
        super("My Frame");
        createLayout();
        createFrame();
    }

    public void createFrame(){
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);       
    }

    public void createLayout(){
        BorderLayout myLayout = new BorderLayout();     
        setLayout(myLayout);

        textPanel = new TextPanel();
        formpanel = new FormPanel();

        // adding components
        add(textPanel, BorderLayout.CENTER);
        add(formpanel, BorderLayout.WEST);  
    }

    public static void main(String[] args){
        new MainFrame();
    }

    public static class FormPanel extends JPanel {

        private JButton clear;
        private TextPanel textPanel;

        public FormPanel(){
            clear = new JButton("Clear Cart!");
            textPanel=new TextPanel();
            add(clear);

            clear.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent aev){
                    System.out.println("Test");
                    textPanel.setText("");
                }       
            });
            createGrid();               
        }

        /* this methods simply creates the layout  */ 
        void createGrid(){
            //creating layout
            setLayout(new GridBagLayout());     
            GridBagConstraints gc = new GridBagConstraints();

            gc.gridy++;
            gc.weightx = .1;
            gc.weighty = .1;
            gc.gridx = 2;
            gc.gridy=5;
            gc.anchor = GridBagConstraints.LINE_START; 
            gc.insets = new Insets(0,0,0,0);

            add(clear, gc);

        }
    }

    public static class TextPanel extends JPanel {
        private JTextArea textArea;

        TextPanel (){
            textArea = new JTextArea();
            setLayout(new BorderLayout());

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

        public void appendSomeText(String t){
            textArea.append(t);     
        }

        public void setText(String s){
            textArea.setText(s);
        }
    }
}

You have two instances of TextPanel , one in MainFrame and the other one in FormPanel . 您有两个TextPanel实例,一个在MainFrame ,另一个在FormPanel TextPanel that is defined in FormPanel is actually not added to the panel, so textPanel.setText(""); 实际上不会将TextPanel中定义的FormPanel添加到面板中,因此textPanel.setText(""); has no effect on it as it is not visible. 对它没有影响,因为它不可见。

When the text appended with Add To Cart! 当文本后附有“ Add To Cart! button it actually goes through a method in MainFrame - formEventOccurred() that executes textPanel.appendSomeText() . 按钮实际上通过MainFrame - formEventOccurred()中的方法执行,该方法执行textPanel.appendSomeText() This is the other instance of TextPanel that is part of MainFrame and that is actually visible. 这是TextPanel的另一个实例,它是MainFrame一部分,并且实际上是可见的。

Looks like you need to move the duplicated logic from the main frame to the panels. 看起来您需要将重复的逻辑从主机移到面板上。 Usually you should not extend JFrame as you are not adding any new functionality. 通常,您不应该扩展JFrame因为您没有添加任何新功能。

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

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