Java 如何在JScrollPane中居中放置组件?
这是我的代码,我想在JScrollPane中居中放置JTextPane:Java 如何在JScrollPane中居中放置组件?,java,swing,netbeans,center,Java,Swing,Netbeans,Center,这是我的代码,我想在JScrollPane中居中放置JTextPane: JFrame frame = new JFrame("Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextPane editor = new JTextPane(); editor.setEditorKit(this); JScrollPane scroll = new JScroll
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextPane editor = new JTextPane();
editor.setEditorKit(this);
JScrollPane scroll = new JScrollPane(editor);
//scroll.setAlignmentX(JScrollPane.CENTER_ALIGNMENT); <<<didnt change anything, so //'ed it
frame.getContentPane().add(scroll);
frame.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
JFrame=newjframe(“示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
最终JTextPane编辑器=新的JTextPane();
editor.setEditorKit(this);
JScrollPane scroll=新的JScrollPane(编辑器);
//scroll.setAlignmentX(JScrollPane.CENTER_ALIGNMENT) 问题可能是框架的布局。首先,您必须设置框架的大小并设置对齐方式。通过@Mr.Cool+1添加到答案中
- JFrame默认实现了BorderLayout,然后代码行
frame.getContentPane().add(滚动)代码>将所有可用空间填充到JFrame中
- 您有两个选择:
a) 将另一个
JComponents
(可以是空的JPanels
)添加到北部
、南部
、西部
和东部
,然后JScrollPane
可以使用JFrame
调整大小
b) 对JFrame
使用GridBagLayout
(没有任何GridBagConstant
),则JScrollPane
无法使用JFrame
调整大小
您所说的“将组件放在JScrollPane中居中”是什么意思?在您的示例中,JScrollPane
拉伸以填充JFrame
的内容窗格,JTextPane
拉伸以填充JScrollPane
。我不明白在那种情况下“中心”是什么意思。