【问题标题】:How do I align text within a JTextField?如何在 JTextField 中对齐文本?
【发布时间】:2013-03-13 03:26:09
【问题描述】:

我有一个边界相当大的 JTextField,但是文本的行为不像我想要的那样。

 _________________
|                 |
|                 |
|text             |
|                 |
|_________________|

我该怎么做才能让我的文字像这样对齐

 _________________
|text             |
|                 |
|                 |
|                 |
|_________________|

编辑: 使用JTextArea 解决了我的问题。谢谢。

【问题讨论】:

  • 一开始为什么会有这样的文本字段?为什么不让它拥有它喜欢的高度,并拥有一个好看的 UI?
  • 我该怎么做?目前我有一个setBounds(10,10,200,400),我要把它改成setLocation(10,10)setPreferredSize(200,400)吗?
  • 你永远不应该打电话给setBounds()setLocation()。这是布局管理器的工作。您也不应该调用 setPreferredSize() 。 JTextField 根据您在构造它时指定的列数知道它的首选大小。使用布局管理器:docs.oracle.com/javase/tutorial/uiswing/layout/using.html
  • 为什么不选择 JTextArea?
  • 你们评论员为什么坚持质疑在你从未见过的用户界面上看起来不错的东西?

标签: java swing alignment jtextfield


【解决方案1】:

JTextArea 与顶部对齐。

或者使用JLabel:

JLabel myLabel = new JLabel("my text");

和电话:

myLabel.setHorizontalAlignment(SwingConstants.LEFT);
myLabel.setVerticalAlignment(SwingConstants.TOP);

布局管理器是执行此操作的另一种方式:http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

【讨论】:

    【解决方案2】:

    这是一个很老的问题,但我需要它,这为我解决了这个问题。

    请注意:此解决方案假定不使用 i18n。如果您启用了 i18n 功能,请查看 javax.swing.plaf.basic.BasicTextFieldUI.create(Element) 以找到正确的类。

    您可以自定义 FieldView 中的垂直文本位置:

    public static class MyTextfieldUI extends com.sun.java.swing.plaf.windows.WindowsTextFieldUI /* TODO which UI class is really used? */ {
    
        public static ComponentUI createUI(JComponent c) {
            return new MyTextfieldUI();
        }
    
        @Override
        public View create(Element elem) {
            return new FieldView(elem) {
    
                @Override
                protected Shape adjustAllocation(final Shape shape) {
                    if (shape instanceof Rectangle) {
                        final Rectangle result = (Rectangle) super.adjustAllocation(shape);
                        /* set vertical text position to top */
                        result.y = ((Rectangle) shape).y;
                        return result;
                    }
    
                    return super.adjustAllocation(shape);
                }
            };
        }
    }
    
    public class Test extends JPanel {
    
    private Test() {
        super(new BorderLayout());
    
        final JTextField northField = new JTextField();
        northField.setBackground(Color.YELLOW);
        northField.setText("north");
    
        final JTextField eastField = new JTextField();
        eastField.setBackground(Color.GREEN);
        eastField.setText("east");
        eastField.setPreferredSize(new Dimension(200, -1));
    
        final JTextField centerField = new JTextField();
        centerField.setBackground(Color.CYAN);
        centerField.setText("center");
        centerField.setHorizontalAlignment(SwingConstants.CENTER);
    
        final JTextField westField = new JTextField();
        westField.setBackground(Color.GRAY);
        westField.setText("west");
        westField.setHorizontalAlignment(SwingConstants.RIGHT);
        westField.setPreferredSize(new Dimension(200, -1));
    
        final JTextField southField = new JTextField();
        southField.setBackground(Color.MAGENTA);
        southField.setText("south");
        southField.setHorizontalAlignment(SwingConstants.RIGHT);
    
        add(northField, BorderLayout.NORTH);
        add(eastField, BorderLayout.EAST);
        add(centerField, BorderLayout.CENTER);
        add(westField, BorderLayout.WEST);
        add(southField, BorderLayout.SOUTH);
    }
    
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    
        /* Set custom look and feel for all text fields */
        UIManager.put("TextFieldUI", MyTextfieldUI.class.getName());
    
        final JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        frame.add(new Test());
        frame.setSize(800, 500);
        frame.setVisible(true);
    }
    

    }

    【讨论】:

      【解决方案3】:
      mytext.setHorizontalAlignment(SwingConstants.LEFT);
      mytext.setVerticalAlignment(SwingConstants.TOP);
      

      这就够了,适用于 JTextField

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 2018-01-25
        • 1970-01-01
        • 2016-03-26
        • 2021-06-14
        • 1970-01-01
        相关资源
        最近更新 更多