在运行时更改 JTextField 的位置

发布于 2024-12-25 20:14:34 字数 70 浏览 3 评论 0原文

我正在使用 Java Swing。我创建一个文本字段和一个按钮。如果我单击该按钮,文本字段应向左移动 100。如何做到这一点?

Am using Java Swing. I create one text field and a button. If I click the button, the text field should move left by 100. How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2025-01-01 20:14:34

ALittleToTheLeft.java screen shot

从一次移动 5px 的微调器转换为一次移动 100px 的按钮,留给读者作为练习。 ;)

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;

class ALittleToTheLeft {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));

                JTextField input = new JTextField(10);

                // not strictly necessary for some components,
                // but good for ones that already have a border.
                final JPanel inputContainer = new JPanel(new GridLayout());
                inputContainer.add(input);
                inputContainer.setBorder(new EmptyBorder(0,200,0,0));
                gui.add(inputContainer);

                SpinnerNumberModel inputAlignmentModel =
                    new SpinnerNumberModel(200,0,200,5);

                final JSpinner inputAlignment =
                    new JSpinner(inputAlignmentModel);
                inputAlignment.addChangeListener( new ChangeListener() {
                    public void stateChanged(ChangeEvent ce) {
                        int leftPad = ((Integer)inputAlignment.
                            getValue()).intValue();
                        int rightPad = 200-leftPad;
                        inputContainer.setBorder(
                            new EmptyBorder(0,leftPad,0,rightPad));
                    }
                });
                gui.add(inputAlignment);

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

ALittleToTheLeft.java screen shot

Converting from the spinner that moves 5px at a time, to a button that moves 100px at a time, is left as an exercise for the reader. ;)

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;

class ALittleToTheLeft {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));

                JTextField input = new JTextField(10);

                // not strictly necessary for some components,
                // but good for ones that already have a border.
                final JPanel inputContainer = new JPanel(new GridLayout());
                inputContainer.add(input);
                inputContainer.setBorder(new EmptyBorder(0,200,0,0));
                gui.add(inputContainer);

                SpinnerNumberModel inputAlignmentModel =
                    new SpinnerNumberModel(200,0,200,5);

                final JSpinner inputAlignment =
                    new JSpinner(inputAlignmentModel);
                inputAlignment.addChangeListener( new ChangeListener() {
                    public void stateChanged(ChangeEvent ce) {
                        int leftPad = ((Integer)inputAlignment.
                            getValue()).intValue();
                        int rightPad = 200-leftPad;
                        inputContainer.setBorder(
                            new EmptyBorder(0,leftPad,0,rightPad));
                    }
                });
                gui.add(inputAlignment);

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}
作妖 2025-01-01 20:14:34

以下是 jtextfiled

  • JTextField() 的不同构造函数
    构造一个新的 TextField。 JTextField(文档 doc、字符串文本、int 列)
    构造一个使用给定文本存储模型和给定列数的新 JTextField。
  • JTextField(int
    列)

    构造一个具有指定列数的新空 TextField。
  • JTextField(字符串文本)
    构造一个使用指定文本初始化的新 TextField。
  • JTextField(字符串文本,整数列)
    构造一个使用指定文本和列初始化的新 TextField。

您还可以使用

  • setAction(Action a)、
  • setActionCommand(String command)、
  • setColumns(int columns)、
  • setDocument(Document doc)、
  • setFont(Font f)、
  • setHorizo​​ntalAlignment( intalignment),
  • setScrollOffset(intscrollOffset)

参考JTextField 类

编辑

要移动文本字段,需要两个类属性 x,y,每次单击时,根据需要移动更改 x 和 y 的值,然后在单击按钮时设置文本字段的边界。如下

textField.setBounds(x, y,width,height);

following are different constructor for jtextfiled

  • JTextField()
    Constructs a new TextField. JTextField(Document doc, String text, int columns)
    Constructs a new JTextField that uses the given text storage model and the given number of columns.
  • JTextField(int
    columns)

    Constructs a new empty TextField with the specified number of columns.
  • JTextField(String text)
    Constructs a new TextField initialized with the specified text.
  • JTextField(String text, int columns)
    Constructs a new TextField initialized with the specified text and columns.

also you can change different properties of jtextfield using methods like

  • setAction(Action a),
  • setActionCommand(String command),
  • setColumns(int columns),
  • setDocument(Document doc),
  • setFont(Font f),
  • setHorizontalAlignment(int alignment),
  • setScrollOffset(int scrollOffset)

refer Class JTextField

EDIT

to move the textfield take two class attributes x,y and on every click change the values of x and y as you want to move, then set bounds of textfield on click of the button.as follow

textField.setBounds(x, y,width,height);
北风几吹夏 2025-01-01 20:14:34

在构造函数中你可以这样设置

int x=100,y=100,width=100,height=20;              
jtextfield.setBounds(x,y,width,height)
add(jtextField);

添加按钮的动作执行事件......

然后在动作事件中

  jTextField.setBounds(x,y+200,width,height);
  add(jtextField);
  setVisbile(true);

in construtor u can set like this

int x=100,y=100,width=100,height=20;              
jtextfield.setBounds(x,y,width,height)
add(jtextField);

Then add action performed event for button......

in the action event

  jTextField.setBounds(x,y+200,width,height);
  add(jtextField);
  setVisbile(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文