0

我正在尝试向我的 JTextarea 添加滚动条,但滚动条没有显示,我的 Jtoolbar 也没有任何人可以告诉我这段代码有什么问题。这样我就可以修复它。我一直在到处寻找,但滚动窗格仍然没有出现

     public PlayerGui() {
    // create main windows
    super("Liste");

    JTextArea editors = new JTextArea();
    editors.setLineWrap(true);
    editors.setWrapStyleWord(true);
    // scroll bar
    JScrollPane scroll = new JScrollPane(editors);
    setEditor(editors);


    // create center panel
    JPanel cent = new JPanel();

    //create Panel for the to
    JPanel north = new JPanel();
    setNorthpanel(north);
    // create tool bar
   JToolBar toolbar = new JToolBar();
   toolbar.add(scroll);

    // set center panel and add preferred layout and backgrounds and size
    setCenter(cent);
    getCenter().setLayout(new  BorderLayout());
    // add scroll bar and toolbar
    add(scroll, BorderLayout.EAST);
    add(toolbar, BorderLayout.SOUTH);
    //getCenter().setBackground(Color.black);
    Dimension size = new  Dimension(getCenter().getPreferredSize());
    getEditor().setPreferredSize(size);
    getCenter().getPreferredSize();
    getCenter().setBorder(new CompoundBorder(new EmptyBorder(10,10 ,10,10),new     EtchedBorder(Color.BLACK, Color.black)));
    //add text editor to the center panel
    getCenter().add(getEditor(),BorderLayout.CENTER);
    //set layout of the frame
    setLayout(new BorderLayout());
    menubar1 = new JMenuBar();
    //create menu list from a string arrays
    for(int i=0; i<list.length; i++){
        JMenu menus = new JMenu(list[i]);
        menubar1.add(menus);
    }
4

1 回答 1

0

您错误地使用了布局管理器。请找到您修改后的有效代码。

public class TestFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame frame = new TestFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestFrame() {
        // create main windows
        super("Liste");
        // scroll bar
        JScrollPane scroll = new JScrollPane();
        //setEditor(editors);

        // create center panel
        JPanel cent = new JPanel();

        //create Panel for the to
        JPanel north = new JPanel();
        getContentPane().setLayout(new BorderLayout(0, 0));
        //toolbar.add(scroll);

        // set center panel and add preferred layout and backgrounds and size
        //setCenter(cent);
        //getCenter().setLayout(new  BorderLayout());
        // add scroll bar and toolbar
        getContentPane().add(scroll);

        JTextArea textArea = new JTextArea();
        scroll.setViewportView(textArea);

        JToolBar toolBar = new JToolBar();
        getContentPane().add(toolBar, BorderLayout.NORTH);
        JMenuBar menubar1 = new JMenuBar();
        //create menu list from a string arrays
//      for(int i=0; i<list.length; i++){
//          JMenu menus = new JMenu(list[i]);
//          menubar1.add(menus);
//      }
    }
}

希望这会有所帮助。:-)

于 2017-08-03T07:18:18.943 回答