0

I am trying to design a basic editor type of GUI in Java using Swing. I have a menu item named New clicking on which I want a blank text area to fill up the GUI. My code is as 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
    }
}

However, even on clicking on the New, I do not get the textPane on the same frame. Can someone please explain.

4

2 回答 2

1
  • 使用JTextPane#setText("")而不是创建一个新的JTextPane

  • 否则你必须通知Container(re)validate()repaint()

于 2012-09-21T17:32:30.483 回答
1

JTabbedPane如果这个应用程序可能应该添加文本窗格。支持多个文档。如果它用于“单个文档”,请在启动时将文本窗格放在框架上。

于 2012-09-21T23:18:25.020 回答