【问题标题】:JEditorPane vertical aligmentJEdi​​torPane 垂直对齐
【发布时间】:2014-06-30 01:13:09
【问题描述】:

我尝试将中心垂直对齐应用于 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);

【问题讨论】:

  • 请编辑您的问题以包含mcve,以在上下文中显示您的方法。
  • 您是否正在寻找JEditorPane 中的文本居中垂直对齐?或者您只想将小部件放在中心?
  • Braj,将小部件放置在中心的解决方案更简单。你的答案是有效的,但一个太复杂了。感谢您花时间回答!
  • 在这里找到它Centering text vertically in JEditorPane.。有关更多信息,请查看此线程 Vertical Alignment of Text in JEditorPane

标签: java swing jeditorpane


【解决方案1】:

我发现最好通过将组件放置在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

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2012-03-14
    • 2011-06-06
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多