如何将 JButton 放入 JTextField (Java) 中?
我希望在 JTextField 内有一个 JButton(带有文件夹图标图像),例如在 JTextField 的最右侧,这样当单击时,该按钮会打开一个 JFileChooser,并且当选择文件时,会显示该文件出现在 JTextField 内。
我已经编写了这段代码,但没有任何显示。
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
I would like to have a JButton (with a folder icon image) inside a JTextField, like over on the far right of the JTextField, so that when clicked, the button opens up a JFileChooser, and when a file is selected, the path to the file appears inside the JTextField.
I have made this code, but nothing shows up.
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可能会发现组件边框很有帮助。它允许您使用 Border API 在文本字段中显示按钮。
You may find the Component Border helpfull. It allows you to display a button in the text field by using the Border API.
根据 Shakedown 的建议,我认为您可以相对轻松地获得所需的效果。您要做的就是拥有一个
JPanel
,其中包含文本区域和旁边的按钮。接下来,将文本字段设置为不绘制任何边框,并为JPanel
提供斜角边框。现在按钮看起来像是位于文本区域内。可能需要一些微调,但它应该可以工作。Building on what Shakedown suggested, I think you can get the desired effect relatively easily. What you do is have a
JPanel
that contains both the text area and, beside it, the button. Next, set the text field to not draw any borders and give theJPanel
a bevel border. Now it will look like the button is inside the text area. It might take some fine tuning, but it should work.您
不能不想在文本字段中放置按钮。您需要将您的意图分解为几个部分 - 事实上,是 3 个部分。首先,您需要一个父容器,或者包含文本字段和按钮的容器;一个
JPanel
就足够了。然后你需要真正的组件,我所说的真正的组件是指那些真正能做某事的组件。这些是您的
JTextField
和JButton
- 继续将它们添加到JPanel
中。为了添加它们并让它们按照您想要的方式显示(按钮位于角落),您需要为JPanel
指定布局。此布局将定义添加的组件在JPanel
中的位置(视觉上)。现在您已将这些内容添加到
JPanel
中,您只能使用JPanel
进行工作,而不必考虑所包含的JTextField
和JButton
。You
can'tdon't want to put a button in a text field. You need to break out your intent into several components - 3, in fact.First you're going to need a parent container, or something that will contain both your text field and also the button; a
JPanel
should suffice.Then you need your real components, and by real I mean the ones that actually do something. These are your
JTextField
andJButton
- go ahead and add these to theJPanel
. In order to add them and have them appear how you want (with the button in the corner), you're going to need to specify a layout for yourJPanel
. This layout will define where added components go (visually) inside theJPanel
.Now that you've added those things into your
JPanel
, you can work only with yourJPanel
instead of thinking in terms of the containedJTextField
andJButton
.因为 Pyrite 还没有发布他的最终解决方案,所以这是我的:
Because Pyrite has not posted his final solution, here is mine:
好吧,我可能认为你已经得到了答案,但对于其他想要轻松做到这一点的人来说,因为我认为其他答案有点太复杂了。
因此,您需要做的是,当您创建
JTextField
时,您还要创建JButton
。我们自己看看代码:就这么简单,我在按钮上使用了 setBounds() ,因为我可以将它放置在任何我想要的地方,至于文本字段,您也可以使用框架/面板布局,但这只是为了演示它是如何工作的。
Well I probably think you got your answer, but for others who want to do this easily, cuz I think the other answers are a bit too complicated.
So, what you need to do is, when you create the
JTextField
you create theJButton
as well. Well see the code for yourself:And its that easy, I used
setBounds()
on the button cuz I can place it anywhere I want, as for the textField you can use a frame/panel layout aswell, but this was for just demonstrating how it works.这将在 2024 年发挥作用。本质上,您可以将 JButton 和 JTextField 放入带有 BoxLayout 的 JPanel 中。然后设置 JTextfield 的大小以填充 JPanel 的其余部分。
This is working in 2024. Essentially you put a JButton and JTextField inside of a JPanel with BoxLayout. Then you set the size of the JTextfield to fill up the rest of the JPanel.