我试着在JEditorPane中将中间垂直连接应用到文本中。但是文本仍然与顶部对齐。我哪里弄错了?
JEditorPane editor = new JEditorPane();
editor.setText("..large text block..");
editor.setAlignmentY(JEditorPane.CENTER_ALIGNMENT); // DOESN'T WORK
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.add(editor);

发布于 2014-05-12 17:03:23
我发现最好的方法是将组件放在JPanel中,然后明智地为面板选择正确的布局管理器。
JEditorPane editor = new JEditorPane();
editor.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
editor.setText("..large text block..");
JScrollPane scrollPane = new JScrollPane(editor);
JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout);
panel.add(Box.createVerticalGlue());
panel.add(scrollPane);
panel.add(Box.createVerticalGlue());
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.add(panel);
frame.setVisible(true);这实际上只是编辑的垂直中心,而不是编辑器中的文本,我认为这正是您所要尝试的。有关BoxLayout的更多信息,请参见http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html
https://stackoverflow.com/questions/23613016
复制相似问题