自定义JTextField [英] Customizing JTextField

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

问题描述

我想知道如何自定义jtextfield的ui,这样我就可以创建圆角矩形边框,而文档不会超出边框。

I would like to know how to customize the ui of a jtextfield so I could create rounded rectangular border without the document going outside the border.

到目前为止,我认为我已经尝试了我能想到的大部分内容,我创建了一个新的FieldView类,并根据我自定义边框绘制圆形rects更改了paint方法中的形状,这是我设法摆脱白色的唯一方法textfield文档/视图是将它设置为不透明但我认为应该有另一种方法而不设置opaque值。

So far I think I have tried the most of what I can think of, I have created a new FieldView class and changed the shape in the paint method according to my customized border that draw rounded rects, the only way I sort of managed to get rid of the white textfield document/view was to set it opaque but I think there should be another way without setting the opaque value.

您是否有任何自定义jtextfield laf的经验请回信,我甚至没有运气阅读Core Swing高级书籍,如果您尝试使用google搜索,请让我知道搜索短语,因为我尝试过关键字,如造型,定制,ui,plaf,laf等等。

Have You any experience in customizing the laf of a jtextfield please write back, I have even read the Core Swing advanced book without luck and if you try searching with google please let me know the search phrase as I have tried with keywords like "styling","customizing","ui","plaf","laf" and what not.

我真诚地希望你能给我一个正确方向的推动,我希望没有人会为此做出一个火焰战争,我真的用尽了我所能想到的所有资源。

I sincerely hope you can give me a nudge in the right direction and I hope nobody will make a flamewar out of this, I have truly used all my resources I can think of.

真诚的问候。

推荐答案

昨天我想解决几乎同样的问题,我的想法得到了一些启发,我终于找到了解决方案。

I wanted to solve almost same problem yesterday and I got some inspiration by your thought and I finally find the solution.


  1. 要在JTextField的边框内创建文档,您可以使用




javax.swing.border.EmptyBorder.EmptyBorder(Insets borderInsets)

javax.swing.border.EmptyBorder.EmptyBorder(Insets borderInsets)

2.避免四个空白区域JTextField的角落,你可以使用

2.To avoid the white space in four corners of JTextField, you can use


g2d.setStroke(new BasicStroke(12));

g2d.setStroke(new BasicStroke(12));

在绘制圆形矩形之前。笔划的宽度取决于您的需求,只需将其宽度足以覆盖角落的空间。

before drawing the round rectangle.The width of stroke is based on your demand and just make it wide enough to cover the space in corner.

这是代码:

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.AbstractBorder;
import javax.swing.border.EmptyBorder;


public class JTextFieldTest {
    JTextField textField;
    boolean activate = false;

    public void createUI(){
        JFrame frame = new JFrame("Test JTextField");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);

        MainPanel mainPanel = new MainPanel();
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        frame.add(mainPanel,BorderLayout.CENTER);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        JTextFieldTest jTextFieldTest = new JTextFieldTest();
        jTextFieldTest.createUI();
    }

    public void setActivate(boolean activate){
        this.activate = activate;
    }

    @SuppressWarnings("serial")
    class MainPanel extends JPanel{
        public MainPanel(){

            textField = new JTextField("Please input:");
            Font fieldFont = new Font("Arial", Font.PLAIN, 20);
            textField.setFont(fieldFont);
            textField.setBackground(Color.white);
            textField.setForeground(Color.gray.brighter());
            textField.setColumns(30);
            textField.setBorder(BorderFactory.createCompoundBorder(
                    new CustomeBorder(), 
                    new EmptyBorder(new Insets(15, 25, 15, 25))));
            textField.addActionListener(new FieldListener());
            textField.addMouseListener(new FieldMouseListener());


            add(textField,BorderLayout.CENTER);
            setBackground(Color.blue);
            setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        }
    }

    @SuppressWarnings("serial")
    class CustomeBorder extends AbstractBorder{
        @Override
        public void paintBorder(Component c, Graphics g, int x, int y,
                int width, int height) {
            // TODO Auto-generated method stubs
            super.paintBorder(c, g, x, y, width, height);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setStroke(new BasicStroke(12));
            g2d.setColor(Color.blue);
            g2d.drawRoundRect(x, y, width - 1, height - 1, 25, 25);
        }   
    }

    class FieldListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println(textField.getText());
        }

    }

    class FieldMouseListener implements MouseListener{
        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            if(activate == false){
                textField.setText("");
            }
            activate = true;
            textField.setForeground(Color.black);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    }
}

这是效果:

有关详细信息,您可以浏览如何制作圆角矩形JTextField

For more details , you can browse How to make a round rectangle JTextField

这篇关于自定义JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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