附上我的窗口代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
public class Window extends JFrame{
    private JPanel panel;
    private JTextPane textPane;
    private JTextPane textPane_1;
    public Window() {
        super("Window");
        this.init();
        this.setSize(800, 600);
        this.setVisible(true);
    }
    void init(){
        panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        textPane = new JTextPane();
        textPane.setBounds(6, 48, 788, 185);
        panel.add(textPane);
        textPane.setFocusable(true);
        textPane_1 = new JTextPane();
        textPane_1.setBounds(6, 346, 788, 185);
        panel.add(textPane_1);
        JScrollPane scroll1 = new JScrollPane(textPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll1.setViewportView(textPane);
        panel.add(scroll1);
        this.add(scroll1);
        JScrollPane scroll2 = new JScrollPane(textPane_1, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll2.setViewportView(textPane);
        panel.add(scroll2);
        this.add(scroll2);
        this.add(panel);
    }
}


我的目标是使两个JTextPanes都有自己的滚动条。屏幕上出现的所有内容只是一个JTextPane(不确定哪个),它只有一个垂直滚动条(我认为这是因为JTextPanes具有自动换行功能)。第二个JTextPane没有出现。谁能帮我?

在此先感谢所有答复。

最佳答案

在这种情况下,您可以使用GridLayout。请参见How to Use GridLayout上的Swing教程

这是带有GridLayout的代码以及内联注释。

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class MyWindow extends JFrame {
    private JPanel panel;
    private JTextPane textPane;
    private JTextPane textPane_1;

    public MyWindow() {
        super("Window");
        this.init();
    }

    void init() {
        // panel with GridLayout having 2 rows and 1 column
        panel = new JPanel(new GridLayout(2,1));

        textPane = new JTextPane();
        // enclose the text pane inside the scroll pane
        // scroll pane shows scrollbars when needed
        JScrollPane scroll1 = new JScrollPane(textPane);
        // add scroll pane at first column of the first row
        // never add text pane again in the panel because
        //  it's already added in scroll pane
        panel.add(scroll1);

        textPane_1 = new JTextPane();
        JScrollPane scroll2 = new JScrollPane(textPane_1);
        // add scroll pane  at first column of the second row
        panel.add(scroll2);

        // finally add the panel in the JFrame's content pane in the center
        getContentPane().add(panel, BorderLayout.CENTER);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                MyWindow window=new MyWindow();
                window.setVisible(true);
            }
        });
    }
}


快照:





请在How to Use Various Layout Managers上查看Swing教程

10-06 05:19