将 2 个 JTextPane 添加到一个 scrollPane
Add 2 JTextPane to one scrollPane
我正在尝试将 2 个 JTextPane 添加到一个 scrollPane。但它不滚动。我究竟做错了什么?
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 211, 178, 193);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane_branding = new JTextPane();
textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);
我不太确定你想在这里实现什么。如果您希望两个 JTextPane
都可以滚动,则需要将每个 JScrollPane
都放在自己的 JScrollPane
中。看起来像这样:
JTextPane textPane_branding = new JTextPane();
JScrollPane scroll_branding = new JScrollPane(textPane_branding);
scroll_branding.setBounds(98, 0, 78, 191);
panel.add(scroll_branding);
JTextPane textPane_trunk = new JTextPane();
JScrollPane scroll_trunk = new JScrollPane(textPane_trunk);
scroll_trunk.setBounds(0, 0, 88, 191);
panel.add(scroll_trunk);
如果你想让你的两个 JTextPane
成为一个可滚动的 JPanel
我想知道为什么你将 fixed Bounds
设置为你的 JScrollPane
和JTextPanes
。这使得 Scrolling 在这里变得荒谬。这就是为什么 JScrollPanes
不能使用没有布局和使用固定边界的窗格。这也是非常糟糕的做法。
因此,我建议您在 JPanel
中使用布局管理器,并使用 setPreferredSize
和 JTextPanes
来定义所需的尺寸。然后您的 JScrollPane
将开始工作。
JScrollPane scrollPane = new JScrollPane();
//scrollPane.setBounds(402, 211, 178, 193); // Don't do this!
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
//panel.setLayout(null); // Use a Layout Manager
JTextPane textPane_branding = new JTextPane();
textPane_branding.setPreferredSize(new Dimension(78,191));
//textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setPreferredSize(new Dimension(88,191));
//textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);
我正在尝试将 2 个 JTextPane 添加到一个 scrollPane。但它不滚动。我究竟做错了什么?
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 211, 178, 193);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane_branding = new JTextPane();
textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);
我不太确定你想在这里实现什么。如果您希望两个 JTextPane
都可以滚动,则需要将每个 JScrollPane
都放在自己的 JScrollPane
中。看起来像这样:
JTextPane textPane_branding = new JTextPane();
JScrollPane scroll_branding = new JScrollPane(textPane_branding);
scroll_branding.setBounds(98, 0, 78, 191);
panel.add(scroll_branding);
JTextPane textPane_trunk = new JTextPane();
JScrollPane scroll_trunk = new JScrollPane(textPane_trunk);
scroll_trunk.setBounds(0, 0, 88, 191);
panel.add(scroll_trunk);
如果你想让你的两个 JTextPane
成为一个可滚动的 JPanel
我想知道为什么你将 fixed Bounds
设置为你的 JScrollPane
和JTextPanes
。这使得 Scrolling 在这里变得荒谬。这就是为什么 JScrollPanes
不能使用没有布局和使用固定边界的窗格。这也是非常糟糕的做法。
因此,我建议您在 JPanel
中使用布局管理器,并使用 setPreferredSize
和 JTextPanes
来定义所需的尺寸。然后您的 JScrollPane
将开始工作。
JScrollPane scrollPane = new JScrollPane();
//scrollPane.setBounds(402, 211, 178, 193); // Don't do this!
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
//panel.setLayout(null); // Use a Layout Manager
JTextPane textPane_branding = new JTextPane();
textPane_branding.setPreferredSize(new Dimension(78,191));
//textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setPreferredSize(new Dimension(88,191));
//textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);