如何将 JButton 放入 JTextField (Java)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3671461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:56:35  来源:igfitidea点击:

How to put a JButton inside a JTextField (Java)?

javajbuttonjtextfieldjfilechooser

提问by Pyrite

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 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该文件出现在 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);

    }
}

回答by camickr

You may find the Component Borderhelpfull. It allows you to display a button in the text field by using the Border API.

您可能会发现组件边框很有帮助。它允许您使用 Border API 在文本字段中显示按钮。

回答by Tikhon Jelvis

Building on what Shakedown suggested, I think you can get the desired effect relatively easily. What you do is have a JPanelthat contains both the text area and, beside it, the button. Next, set the text field to not draw any borders and give the JPanela bevel border. Now it will look like the button is inside the text area. It might take some fine tuning, but it should work.

基于Shakedown的建议,我认为您可以相对轻松地获得所需的效果。你所做的是有一个JPanel包含文本区域和旁边的按钮。接下来,将文本字段设置为不绘制任何边框并提供JPanel斜角边框。现在看起来按钮在文本区域内。这可能需要一些微调,但它应该可以工作。

回答by Nate W.

You can'tdon't want toput a button in a text field. You need to break out your intent into several components - 3, in fact.

不能不想把一个按钮,文本字段。您需要将您的意图分解为几个组成部分 - 实际上是 3 个组成部分。

First you're going to need a parent container, or something that will contain both your text field and also the button; a JPanelshould suffice.

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

Then you need your real components, and by real I mean the ones that actually do something. These are your JTextFieldand 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.

然后你需要你真正的组件,真正的我指的是那些真正做某事的组件。这些是您的JTextFieldJButton- 继续将这些添加到JPanel. 为了添加它们并让它们以您想要的方式出现(使用角落里的按钮),您需要为您的JPanel. 此布局将定义添加的组件在JPanel.

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

现在,您已经添加了这些东西进入你的JPanel,你可以用你只有工作JPanel,而不是在包含的思维JTextFieldJButton

回答by Killgnom

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

因为 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);

enter image description here

在此处输入图片说明