问题描述
我正在尝试在发生鼠标单击的位置添加 JTextField.文本字段的大小应根据输入的文本而变化.下面是我的代码.
I am trying to add a JTextField at a position where a mouse click happens.The size of the text field should be variable based on the text entered. Below is my code.
public class Paint {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test TextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(400, 250);
final JTextField text = new JTextField();
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent me) {
}
});
frame.setVisible(true);
}
}
我想在鼠标点击的位置添加文本字段.文本字段的大小应根据在文本字段中输入的文本而增加.有人可以让我知道如何实现鼠标点击方法来实现相同的目标吗?
I want to add the text field at the position where the mouse click happened. The size of the text field should increase based on the text entered in the text field. Can someone let me know how to implement the mouse click method to achieve the same?
我尝试使用事件 x 和 y 位置设置文本框的边界
I tried to set bounds of the text box using the event x and y positions like
text.setBounds(event.getX(), event.getY(), event.getX(), event.getY());
但是这样 textField 的大小与全帧相同.但我想要一个大小不一的小文本框
but with this the size of textField is same as the full frame. but i want a small text box with varying size
推荐答案
首先,我们需要移除布局以便能够在我们想要的地方添加组件
First, we need to remove the layout to be able to add component where we want
frame.setLayout(null);
然后,我们可以使用以下命令检查事件的结果:
Then, we can check the result of the event with :
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
System.out.println(event.getX() + " " + event.getY());
}
}
我们可以注意到,如果我们点击左上角,我们无法获得 (0,0) 坐标.因为 frame
是完整的屏幕,所以我们需要使用 ContentPane
或者我们可以简单地使用 JPanel
保持简单.
we can notice if we click on the upper left corner that we can't get a (0,0) coordinate. Because A frame
is the complete screen, so we need to use the ContentPane
or we can simply use a JPanel
keep it simple.
final JPanel panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
panel.addMouseListener(new MouseAdapter() {}); //Use the MouseAdapter to not be force to override the full interface of MouseListener.
现在,让我们添加一个点击组件.首先,我们在活动期间创建它(每次创建一个新的并设置大小:
Now, let's add a component on click. First, we create it during the event (to create a new one each time and we set the size :
final JTextField text = new JTextField();
text.setSize(new Dimension(50, 18)); //I took those randomly...
重要的部分是位置,将其设置为组件,如:
The important part is the location, set it to the component like :
text.setLocation(event.getX(), event.getY());
并将其添加到面板
panel.add(text);
结果是在点击的特定位置添加了一个新的JTextField
.
The result is a new JTextField
added at the specific position of the click.
public static void main(String[] args) {
final JFrame frame = new JFrame("Test TextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 250);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
final JTextField text = new JTextField("abc");
text.setLocation(event.getX(), event.getY());
text.setSize(new Dimension(50, 18));
panel.add(text);
}
});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
请注意,这不是 Swing 的最佳用法,布局可以轻松构建高效的屏幕.
这篇关于在单击鼠标的位置添加了 JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!