【发布时间】: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