我想用Swing設計基於Java的GUI的編輯器類型。我有一個名爲New
的菜單項,點擊我想要一個空白文本區域來填充GUI。我的代碼folows:在按鈕上打開同一窗體中的文本窗口點擊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UI extends JFrame {
private JMenuBar bar;
private JMenu menu;
private JMenuItem item;
private JTextPane tp;
public UI() {
setLayout(new FlowLayout());
bar = new JMenuBar();
setJMenuBar(bar);
menu = new JMenu("File");
bar.add(menu);
item = new JMenuItem("New");
menu.add(item);
item.addActionListener(new xyz());
}
public class xyz implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
JTextPane tp = new JTextPane();
add(tp);
}
}
public static void main(String args[]) {
// do the rest of the stuffs
}
}
然而,即使在點擊New
,我沒有得到相同的幀上的textPane。有人可以請解釋。而不是
我也試過這個:tp.setText(「」);但它似乎並不奏效。 – OneMoreError
更好地幫助您更快地發佈關於[JTextPane#setText()]的[SSCCE](http://sscce.org/)(http://docs.oracle.com/javase/7/docs/api/javax/ swing/JEditorPane.html#setText%28java.lang.String%29)不起作用 – mKorbel
@CSSS tp.setText(「」)確實有效。您應該從actionPerformed()方法中調用它,以便每次單擊「新建」時清除內容。 – Rempelos