Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在文本字段中添加按钮?_Java_Swing_Jbutton_Jtextfield - Fatal编程技术网

Java 如何在文本字段中添加按钮?

Java 如何在文本字段中添加按钮?,java,swing,jbutton,jtextfield,Java,Swing,Jbutton,Jtextfield,我正在使用swing组件在java中创建一个文本字段。我想创建一个类似Mozilla或其他浏览器中出现的搜索文本字段 我在文本字段中添加了一个按钮。我已经设置了JTextField的边框布局。一切正常,但每当在文本字段中写入大文本时(当它达到文本字段的给定大小时),它就会转到按钮后面。正如你们每个人都看到的,搜索栏中不会出现这种情况。文本不能在按钮后面,而必须在按钮和文本之间有一些间隙 有人知道如何做到这一点吗?正如上面提到的,查看代码可能会有所帮助,尤其是布局管理器。 但是,您可以尝试以下方法

我正在使用swing组件在java中创建一个文本字段。我想创建一个类似Mozilla或其他浏览器中出现的搜索文本字段

我在文本字段中添加了一个按钮。我已经设置了
JTextField
的边框布局。一切正常,但每当在文本字段中写入大文本时(当它达到文本字段的给定大小时),它就会转到按钮后面。正如你们每个人都看到的,搜索栏中不会出现这种情况。文本不能在按钮后面,而必须在按钮和文本之间有一些间隙


有人知道如何做到这一点吗?

正如上面提到的,查看代码可能会有所帮助,尤其是布局管理器。 但是,您可以尝试以下方法(如果尚未尝试):

  • 调用集合列

  • 根据布局管理器的不同,调用setPreferredSize/setMaximumSize/setMinimumSize。 但我会尽量避免这种解决方案,因为它是像素级的维护


  • 问候

    可以从以下内容开始:

    闪烁光标位于文本字段的最右边。

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    class ButtonsInTextField {
    
        JPanel gui = new JPanel(new GridBagLayout());
        JTextField textField;
    
        ButtonsInTextField(int cols) {
            JPanel textFieldWithButtonsPanel = new JPanel(new FlowLayout(
                    SwingConstants.LEADING, 5, 1));
            textField = new JTextField(cols);
            textFieldWithButtonsPanel.add(textField);
    
            addButtonToPanel(textFieldWithButtonsPanel, 8);
            addButtonToPanel(textFieldWithButtonsPanel, 16);
            addButtonToPanel(textFieldWithButtonsPanel, 24);
    
            // WARNING:  Not sensitive to PLAF change!
            textFieldWithButtonsPanel.setBackground(textField.getBackground());
            textFieldWithButtonsPanel.setBorder(textField.getBorder());
            textField.setBorder(null);
            // END WARNING:  
    
            gui.add(textFieldWithButtonsPanel);
        }
    
        private final void addButtonToPanel(JPanel panel, int height) {
            BufferedImage bi = new BufferedImage(
                    // find the size of an icon from the system, 
                    // this is just a guess
                    24, height, BufferedImage.TYPE_INT_RGB);
            JButton b = new JButton(new ImageIcon(bi));
            b.setContentAreaFilled(false);
            //b.setBorderPainted(false);
            b.setMargin(new Insets(0,0,0,0));
            panel.add(b);
        }
    
        public final JComponent getGui() {
            return gui;
        }
    
        public final JTextField getField() {
            return textField;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    ButtonsInTextField bitf = new ButtonsInTextField(20);
                    JOptionPane.showMessageDialog(null, bitf.getGui());
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }
    

    作为替代解决方案,您可以使用,它允许您将按钮用作边框,使其显示在文本字段中。

    显示您的代码。如果没有这一点,就很难帮助您使用什么布局??。。我猜你没有使用setBounds()方法。正如@Markus所说的,发布你的代码。如果可能的话,附加屏幕截图会更好…有一个边界技巧你可以使用,不用代码,或者你可以使用SwingLabs的BuddySupport API,SwingX库,for和或参阅(是)。这是最好的方法。不要将按钮作为文本字段的子组件插入,这很难正确操作,而且几乎没有任何好处。这个答案是非常完整和解释性的,如果我可以的话,我会投两次赞成票:-)太好了!
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    class ButtonsInTextField {
    
        JPanel gui = new JPanel(new GridBagLayout());
        JTextField textField;
    
        ButtonsInTextField(int cols) {
            JPanel textFieldWithButtonsPanel = new JPanel(new FlowLayout(
                    SwingConstants.LEADING, 5, 1));
            textField = new JTextField(cols);
            textFieldWithButtonsPanel.add(textField);
    
            addButtonToPanel(textFieldWithButtonsPanel, 8);
            addButtonToPanel(textFieldWithButtonsPanel, 16);
            addButtonToPanel(textFieldWithButtonsPanel, 24);
    
            // WARNING:  Not sensitive to PLAF change!
            textFieldWithButtonsPanel.setBackground(textField.getBackground());
            textFieldWithButtonsPanel.setBorder(textField.getBorder());
            textField.setBorder(null);
            // END WARNING:  
    
            gui.add(textFieldWithButtonsPanel);
        }
    
        private final void addButtonToPanel(JPanel panel, int height) {
            BufferedImage bi = new BufferedImage(
                    // find the size of an icon from the system, 
                    // this is just a guess
                    24, height, BufferedImage.TYPE_INT_RGB);
            JButton b = new JButton(new ImageIcon(bi));
            b.setContentAreaFilled(false);
            //b.setBorderPainted(false);
            b.setMargin(new Insets(0,0,0,0));
            panel.add(b);
        }
    
        public final JComponent getGui() {
            return gui;
        }
    
        public final JTextField getField() {
            return textField;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    ButtonsInTextField bitf = new ButtonsInTextField(20);
                    JOptionPane.showMessageDialog(null, bitf.getGui());
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }