简体   繁体   English

使用Java Swing更改JTextfield属性

[英]JTextfield property change using Java Swing

I use the following code for changing the properties of a JTextField. 我使用以下代码更改JTextField的属性。 It worked properly. 它工作正常。 My problem is how to change the JTextField properties by passing parameters without using the default functions. 我的问题是如何通过传递参数而不使用默认功能来更改JTextField属性。 For example if I want to change the JTextField LEFT property means using parameter (control name,left,100) like this. 例如,如果我想更改JTextField LEFT属性,则意味着使用像这样的参数(控件名称,left,100)。 How do I change a JTextField property by passing parameters to the JTextField Property using the following code? 如何通过使用以下代码将参数传递给JTextField属性来更改JTextField属性?

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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