我有一个Jframe,用户通过Joptionpane输入新信息,将其添加到一个数组中,然后将其附加并显示到内容窗格中。.然后重复该循环,直到用户输入“ STOP”。当前,该程序在旧数组下输出新数组。如何清除内容窗格中的旧数组,仅显示新值?

import java.awt.*;
import java.util.LinkedList;
import java.util.List;
public class Project1GUI {
    static JFrame Frame;
    static TextArea unsorted_words, sorted_words, linked_words;
    public Project1GUI(String title){
            //All this does is make an empty GUI FRAME.
                    Frame=new JFrame();//i made a new variable from the JFrame class
                    Frame.setSize(400,400);//Used the Variable from JFrame and used some of it functions. This function sets the hieght and width of the Frame
                    Frame.setLocation(200,200);//This sets where the Empty Frame should be
                    Frame.setTitle(title);//This puts a title up top of the Frame
                    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//places an x box that closes when clicked on
                    Frame.setVisible(true);//This activates the JFram when is set true.
                    Frame.setLayout(new GridLayout(1,2));//This sets the layout of the Frame and since i want a Grid i used a GirdLayout
                    //Functions and placed it inside the setlayout functions. to  get 2 grids i places 1 meaning 1 row and 2 for 2 cols
                    unsorted_words=new TextArea(); //From the TextArea class i made three variables
                    sorted_words= new TextArea();
                    linked_words= new TextArea();
                    Container panel=new Container();
                    panel=Frame.getContentPane();
                    panel.add(unsorted_words);
                    panel.add(sorted_words);
                    panel.add(linked_words);

    }


    public void add_unsorted(String words){
        unsorted_words.append(words+"\n");//add words to GUI

    }

    public void add_sorted(String words){
        sorted_words.append(words+"\n");
    }

    public void add_linked(List<String> linked_words2){
        linked_words.append(linked_words2+"\n");
    }

}

最佳答案

要获得更明确的答案,请发布MCVE

看到您尚未发布任何代码,我猜您正在使用JLabelJList或类似的某种形式来显示数组。不管您正在执行哪一项,都需要告诉组件更新其内容,而不仅仅是它本身。为此,您需要调用组件.setText()或类似方法。

如果您有JLabelJTextArea,它可能看起来像这样。

labelOrTextArea.setText("New Text");


如果您使用的是JList,则应像这样更新列表Default List Model

dlm.addElement("New Text");


更新

我发现您的代码有几处错误。按照惯例,JFrame Frame = new JFrame首先,变量应以小写字母开头,并且不应包含下划线“ _”。您还将使用AWT组件而不是Swing组件。您应该使用JTextAreaJPanel(没有JContainer),JLabel等。

您也永远不会将面板添加到框架中。

frame.add(panel);


将其可见性设置为true后,也不应在框架或面板中添加内容。所以你应该像这样设置框架

import javax.swing.*;
import java.awt.*;
import java.util.List;
public class Project1GUI
{
    JTextArea unsorted_words, sorted_words, linked_words;

    public Project1GUI()
    {
        JFrame frame = new JFrame("Title");
        JPanel panel = new JPanel(new GridLayout(2, 1));

        unsorted_words = new JTextArea();
        sorted_words = new JTextArea();
        linked_words = new JTextArea();

        panel.add(unsorted_words);
        panel.add(sorted_words);
        panel.add(linked_words);

        frame.add(panel);
        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


然后,您可以实现当前拥有的方法,并在ActionListener等中调用它们。

结果:

java - 如何使用新值重置或刷新Jframe-LMLPHP

最重要的是,您不应依赖static的使用,因为它会脱离OOP的要点。

10-07 19:08
查看更多