2012-09-21 110 views
0

我想用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。有人可以請解釋。而不是

回答

1
  • 使用JTextPane#setText("")創建一個新的JTextPane

  • ,否則你有(re)validate()repaint()

+0

我也試過這個:tp.setText(「」);但它似乎並不奏效。 – OneMoreError

+0

更好地幫助您更快地發佈關於[JTextPane#setText()]的[SSCCE](http://sscce.org/)(http://docs.oracle.com/javase/7/docs/api/javax/ swing/JEditorPane.html#setText%28java.lang.String%29)不起作用 – mKorbel

+0

@CSSS tp.setText(「」)確實有效。您應該從actionPerformed()方法中調用它,以便每次單擊「新建」時清除內容。 – Rempelos

1

文本窗格或許應該被添加到JTabbedPane如果通知Container這個程序。支持多個文件。如果打算用於「單個文檔」,則在啓動時將文本窗格放到框架上。

相關問題