简体   繁体   中英

JTextfield property change using Java Swing

I use the following code for changing the properties of a JTextField. It worked properly. My problem is how to change the JTextField properties by passing parameters without using the default functions. For example if I want to change the JTextField LEFT property means using parameter (control name,left,100) like this. How do I change a JTextField property by passing parameters to the JTextField Property using the following code?

package textfield;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class textf implements ActionListener
{
    JTextField txt;
    JButton width;
    JFrame frame;
    JPanel panel;
    //Font font = UIManager.getFont("txt.font");
    public textf()
    {
       frame=new JFrame();
       panel=new JPanel();
       txt=new JTextField(10);
       width=new JButton("width");
       width.setBounds(20, 200, 50, 40);

    panel.add(txt);
    panel.add(width);
    width.addActionListener(this);
    frame.add(panel);



       // n.getContentPane().add(new textf());
        frame.setSize(440,310);
        frame.setVisible(true);

    }
    public void actionPerformed(ActionEvent e)
    {
    txt.setBorder(BorderFactory.createLineBorder(Color.red));
    txt.setHorizontalAlignment(javax.swing.JTextField.LEFT);
     txt.setSize(100,100);
      txt.setPreferredSize(new Dimension(1000, 90));

       txt.putClientProperty("txt.sizeVariant", "small");

        txt.setFont(new Font("Comic Sans",Font.ITALIC,20));
        txt.setForeground(Color.BLUE);
        txt.setHorizontalAlignment(JTextField.RIGHT);
    }
    public static void main(String[] args)
    {
        new textf();

    }


}

我想您想设置JTextField的位置(如果没有,请解释一下“ LEFT”属性的含义)

tf.setBounds(LEFT,tf.getY(),tf.getWidth(),tf.getHeight());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM