如何将 JButton 放入 JTextField (Java) 中?

发布于 2024-09-18 11:04:12 字数 567 浏览 7 评论 0原文

我希望在 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 技术交流群。

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

发布评论

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

评论(6

一个人练习一个人 2024-09-25 11:04:12

您可能会发现组件边框很有帮助。它允许您使用 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.

ヅ她的身影、若隐若现 2024-09-25 11:04:12

根据 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 the JPanel 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.

土豪 2024-09-25 11:04:12

不能不想在文本字段中放置按钮。您需要将您的意图分解为几个部分 - 事实上,是 3 个部分。

首先,您需要一个父容器,或者包含文本字段和按钮的容器;一个 JPanel 就足够了。

然后你需要真正的组件,我所说的真正的组件是指那些真正能做某事的组件。这些是您的 JTextFieldJButton - 继续将它们添加到 JPanel 中。为了添加它们并让它们按照您想要的方式显示(按钮位于角落),您需要为 JPanel 指定布局。此布局将定义添加的组件在 JPanel 中的位置(视觉上)。

现在您已将这些内容添加到 JPanel 中,您只能使用 JPanel 进行工作,而不必考虑所包含的 JTextFieldJButton

You can't don'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 and JButton - go ahead and add these to the JPanel. 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 your JPanel. This layout will define where added components go (visually) inside the JPanel.

Now that you've added those things into your JPanel, you can work only with your JPanel instead of thinking in terms of the contained JTextField and JButton.

小草泠泠 2024-09-25 11:04:12

因为 Pyrite 还没有发布他的最终解决方案,所以这是我的:

my_button = new JButton("x");
JFormattedTextField my_textfield = new JFormattedTextField("Nr.");
my_textfield.setBorder(javax.swing.BorderFactory.createEmptyBorder());

JPanel textfield_with_button = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Border lowered_bevelborder = BorderFactory.createLoweredBevelBorder();
textfield_with_button.setBorder(lowered_bevelborder);

textfield_with_button.add(my_textfield);
textfield_with_button.add(my_button);

在此处输入图像描述

Because Pyrite has not posted his final solution, here is mine:

my_button = new JButton("x");
JFormattedTextField my_textfield = new JFormattedTextField("Nr.");
my_textfield.setBorder(javax.swing.BorderFactory.createEmptyBorder());

JPanel textfield_with_button = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Border lowered_bevelborder = BorderFactory.createLoweredBevelBorder();
textfield_with_button.setBorder(lowered_bevelborder);

textfield_with_button.add(my_textfield);
textfield_with_button.add(my_button);

enter image description here

半衾梦 2024-09-25 11:04:12

好吧,我可能认为你已经得到了答案,但对于其他想要轻松做到这一点的人来说,因为我认为其他答案有点太复杂了。
因此,您需要做的是,当您创建 JTextField 时,您还要创建 JButton。我们自己看看代码:

JButton button = new JButton();
button.setBounds(50, 5, 50, 25);
button.setBackground(Color.black);
JTextField textField = new JTextField();
textField.setBounds(20, 60, 100, 35);
textField.setBackground(Color.white);
textField.add(button);

就这么简单,我在按钮上使用了 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 the JButton as well. Well see the code for yourself:

JButton button = new JButton();
button.setBounds(50, 5, 50, 25);
button.setBackground(Color.black);
JTextField textField = new JTextField();
textField.setBounds(20, 60, 100, 35);
textField.setBackground(Color.white);
textField.add(button);

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-09-25 11:04:12

这将在 2024 年发挥作用。本质上,您可以将 JButton 和 JTextField 放入带有 BoxLayout 的 JPanel 中。然后设置 JTextfield 的大小以填充 JPanel 的其余部分。

    private JPanel createButtonAndTextFieldCombo(JButton button, JTextField textField) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, 0));
        panel.setBackground(textField.getBackground());
        panel.setBorder(textField.getBorder());
        textField.setMinimumSize(new Dimension(panel.getWidth(), panel.getHeight()));
        textField.setBorder(BorderFactory.createEmptyBorder());
        panel.add(textField);
        panel.add(button);
        return panel;
    }

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.

    private JPanel createButtonAndTextFieldCombo(JButton button, JTextField textField) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, 0));
        panel.setBackground(textField.getBackground());
        panel.setBorder(textField.getBorder());
        textField.setMinimumSize(new Dimension(panel.getWidth(), panel.getHeight()));
        textField.setBorder(BorderFactory.createEmptyBorder());
        panel.add(textField);
        panel.add(button);
        return panel;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文