【问题标题】:JTextField customization issueJTextField 自定义问题
【发布时间】:2022-01-21 16:44:33
【问题描述】:

我制作了一个框架并在其中放置了一个按钮和一个文本字段。 我正在使用 setMargin 方法在插入符号和文本字段的边框之间设置一个边距,并且在我为其添加边框之前工作得很好。

添加边框后setMargin调用方法不起作用。

能否请您帮助我了解问题的根源并找到同时具有边框和一定边距的替代方法?

public class main extends JFrame  {

    public static void main(String[]args){
        JTextField textfield0;
        JButton button0;
        
        Border border7=BorderFactory.createDashedBorder(new Color(0xA524FF), 2, 5, 4, true);
        Border border8=BorderFactory.createCompoundBorder();
        Border border01=BorderFactory.createLineBorder(Color.RED);
        Border border02=BorderFactory.createLineBorder(Color.YELLOW);
        Border border9=BorderFactory.createCompoundBorder(border01, border02);
        
        textfield0=new JTextField();
        textfield0.setPreferredSize(new Dimension(300,30));
        textfield0.setFont(new Font("Consolas",Font.BOLD,15));
        textfield0.setCaretColor(Color.RED);
        textfield0.setBackground(Color.CYAN);
        textfield0.setForeground(Color.MAGENTA);
        textfield0.setText("name");
        //textfield0.setBorder(border9);
        textfield0.setSelectedTextColor(Color.YELLOW);
        textfield0.setMargin(new Insets(0,7,0,5));
        textfield0.setCaretPosition(0);
        textfield0.setSelectionColor(Color.PINK);

        button0=new JButton();
        button0.setText("submit");
        button0.setPreferredSize(new Dimension(100,30));    
        button0.setFocusable(false);
        button0.setBackground(textfield0.getBackground());
        button0.setFont(textfield0.getFont());
        button0.setBorder(textfield0.getBorder());
        
        JFrame frame00=new JFrame();
        frame00.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame00.setLayout(new FlowLayout()); 
        frame00.add(button0);
        frame00.add(textfield0);
        frame00.pack();
        frame00.setVisible(true);
    }
}

【问题讨论】:

标签: java swing awt border jtextfield


【解决方案1】:

我们可以在需要一些额外步骤的 JavaDoc 中找到答案:

设置文本组件的边框与其文本之间的边距。 文本组件的默认 Border 对象将使用此值 创建适当的边距。 但是,如果设置了非默认边框 文本组件,这是 Border 对象的责任 创建适当的边距空间(否则此属性将 有效地被忽略)。

来源:https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setMargin-java.awt.Insets-

要做到以上,我们可以做两件事之一,我们可以使用复合边框,如下所示:Java Swing - setting margins on TextArea with Line Border 或者我们可以采取以下步骤:

同时覆盖 AbstractBorder.getBorderInsets(Component c)AbstractBorder.getBorderInsets(Component c, Insets insets) 到 提供正确的插图

来源:https://docs.oracle.com/javase/tutorial/uiswing/components/border.html#custom

我强烈建议您阅读上述链接,以更好地了解边界以及如何使用它们。

【讨论】:

    【解决方案2】:

    如果您阅读 Border 的 javadoc,它指出:“使用 EmptyBorder 创建纯边框(此机制替换其前身 setInsets)”

    因此,您可以使用带有 PlainBorder 的 CompoundBorder 和您正在设置的边框,或者覆盖现有 Border 上的 getBorderInsets(Component c)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-09
      • 2011-09-26
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 2015-05-27
      相关资源
      最近更新 更多