附上我的窗口代码:
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没有显示出来。任何人都可以帮助我吗?
感谢所有回复的人。
答案 0 :(得分:2)
在这种情况下,您可以使用GridLayout
。请参阅How to Use GridLayout
以下是包含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);
}
});
}
}
快照:
答案 1 :(得分:1)
首先,JFrame
默认使用BorderLayout
,BorderLayout
只允许单个组件占据其五个可用位置中的任何一个。其次,组件只能属于单个父母
看看你的代码......
scroll1.setViewportView(textPane);
panel.add(scroll1);
this.add(scroll1);
//...
scroll2.setViewportView(textPane);
panel.add(scroll2);
this.add(scroll2);
this.add(panel);
textPane
设置为scroll1
scroll1
添加到panel
scroll
添加到this
,有效地将其从panel
移除...... textPane
设置为scroll2
的视口视图,将其从scroll1
scroll2
添加到panel
scroll2
,有效地将其从panel
... panel
,覆盖之前添加到框架中的所有内容...... 这实际上意味着panel
是BorderLayout
尝试在框架上布局的唯一可见组件,但它不包含任何内容
相反,您可以在将每个滚动窗格添加到框架时为每个滚动窗格指定一个位置,例如..
scroll1.setViewportView(textPane);
this.add(scroll1, BorderLayout.NORTH);
//...
scroll2.setViewportView(textPane_1);
this.add(scroll2, BorderLayout.SOUTH);
更新了工作示例
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Window extends JFrame {
private JTextPane textPane;
private JTextPane textPane_1;
public Window() {
super("Window");
this.init();
this.setSize(800, 600);
this.setVisible(true);
}
void init() {
textPane = new JTextPane();
textPane_1 = new JTextPane();
JScrollPane scroll1 = new JScrollPane(textPane);
scroll1.setViewportView(textPane);
JScrollPane scroll2 = new JScrollPane(textPane_1);
add(scroll1, BorderLayout.NORTH);
add(scroll2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Window frame = new Window();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这是JTextPane
的默认行为,其总体大小取决于其内容的大小......
现在,您可以使用Scrollable
界面并指定" initial",PreferredScrollableViewportSize
,向滚动窗格建议您的组件想要使用多少空间。
幸运的是,JTextPane
已经实现了此界面,因此您只需要覆盖getPreferredScrollableViewportSize
方法,例如...
textPane = new JTextPane() {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}
};
textPane_1 = new JTextPane() {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}
};
请查看Laying Out Components Within a Container了解详情
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正