【问题标题】:Center or on the right a jtextarea in jpaneljpanel 中的 jtextarea 居中或右侧
【发布时间】:2019-08-05 04:04:13
【问题描述】:

我的 jtextarea 有这个,我尝试了很多方法将其对齐在中心或右侧,但它总是在窗口之外

这是我现在得到的结果:

使用此代码:

    Popup noticePopup = new Popup("Notice" , 1500, 900);
                JPanel noticePanel = new JPanel();
                noticePanel.setPreferredSize(new Dimension(1500,1000));
                List<List> ListNote = controller.Medicament.consultationNotice(idMedoc);
                String noticeThis =null;
                for(int n=0;n<ListNote.size();n++) {
                    noticeThis = (String) ListNote.get(n).get(1);
                }
                JTextArea noticeArea = new JTextArea(noticeThis);
                noticePopup.add(noticePanel);
                noticePanel.add(noticeArea);
                noticeArea.setAlignmentX(JTextArea.CENTER_ALIGNMENT);

谢谢

【问题讨论】:

  • JTextPane 对你有好处吗?因为JTextArea 在文本对齐方面有一些障碍。看这里:stackoverflow.com/questions/24315757/…
  • 你为什么使用Popup一个“重量级”AWT组件而其他的都是“轻量级”Swing组件?
  • 因为我需要在新页面上有这个文本

标签: java swing


【解决方案1】:

你可以尝试类似 JTextPane 而不是 JTextArea

JTextPane textPane = new JTextPane();

SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);
textPane.setParagraphAttributes(attributes, true);

如果你想使用 JTextArea 那么你需要添加滚动窗格然后 scorllpane 将成为面板的一部分请尝试下面的代码

                JTextArea text = new JTextArea(-, -);
                JScrollPane sPane = new JScrollPane( text );
                JPanel panel = new JPanel();
                panel.add( sPane );

                sPane.addAncestorListener( new AncestorListener()
                {
                    public void ancestorAdded(AncestorEvent e)
                    {
                        JScrollPane sPane = (JScrollPane)e.getComponent();
                        sPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    }
                });

希望对你有用

【讨论】:

  • 不,我想使用 jtextarea,因为我需要在其上放置变量
  • 添加了 JTextArea 请尝试
  • 我不能使用滚动窗格,因为它是在 jpanel 而不是 jframe 上扩展的(对于某些要求)并且使用 jtextarea 我试过你的 colution CRR 也不起作用......
  • i need that for put a variable on it - 为什么您认为不能将变量与 JTextPane 一起使用?使用 setText(...) 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多