JEdi​​torPane 垂直对齐 [英] JEditorPane vertical aligment

查看:37
本文介绍了JEdi​​torPane 垂直对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对 JEditorPane 中的文本应用中心垂直对齐.但文本仍然与顶部对齐.我哪里弄错了?

I try to apply center vertical aligment to text in JEditorPane. But text still is aligned to the top. Where did I mistake?

    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);

推荐答案

我发现通过将组件放在 JPanel 中然后巧妙地选择正确的布局管理器来进行任何特殊对齐总是最好的为面板.

I find it is always best to do any special alignment by placing your components in a JPanel and then smartly choosing the correct layout manager for the panel.

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

This really just centers the editor vertically, not the text within the editor which I think is what you are trying for. For more on BoxLayout see http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

这篇关于JEdi​​torPane 垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆