【问题标题】:How to reset or refresh Jframe with new values如何使用新值重置或刷新 Jframe
【发布时间】:2016-03-23 21:12:31
【问题描述】:

我有一个 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");
    }

}

【问题讨论】:

  • 您是否将数组内容放入 JLabel 中,然后添加到框架的内容窗格中?如果是这样,您是否在添加新标签之前删除了旧标签?
  • 能否添加代码段,以便我们查看您出错的地方?
  • 上传代码即可。我把它们放在一个容器中,然后放到一个内容窗格中。
  • 查看更新的答案

标签: java swing user-interface jframe contentpane


【解决方案1】:

如需更明确的答案,请发帖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 等中调用它们。

结果:

除此之外,您不应依赖 static 的使用,因为它会脱离 OOP 的要点。

【讨论】:

  • 哇,感谢一大堆伟大的见解和建议。我刚开始学习 Java,很抱歉出现明显的困惑,但再次感谢!
  • 有一个问题,如果我按照您描述的那样实现代码,如果用户输入一个新字符串存储在数组中,JFrame 是否会自动刷新?
  • 你到底是什么意思?您是否尝试从 textareas 中获取内容并将其存储在数组中?
  • 主程序将读取一个 .txt 文件并将这些字符串存储在一个数组中。然后按字母顺序排序并创建所有排序单词的单独链接列表。这两个数组和链表现在被传递到我的 GUI 并被附加。回到主程序,通过一个窗口询问用户是否要添加单词。如果他们愿意,则将单词添加到数组和链表中,然后再次将其传递给 GUI 并显示。我的问题是如何摆脱旧数组和链表,以便 GUI 只显示新数组并列出用户输入的单词?
  • 我建议也许使用Collections 并让一个存储您读取文件的一个,然后一个存储排序值,然后存储一个您想要附加到的。然后您可以将这些Collections 传递给LinkedLists 并使用textArea.setText() 显示它们,然后当您获得输入时,您可以使用.append() 方法添加用户的输入。
猜你喜欢
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 2010-12-01
  • 2013-02-22
  • 2013-05-05
  • 1970-01-01
相关资源
最近更新 更多