更改 Jbutton 上的图标位置

Aha*_*hid 1 java swing netbeans jbutton

我一直在netbeans上开发一个java项目。我想知道当文本位于中心时如何将图标移动JButton到左角或右角。你能帮我么 ?

https://i.stack.imgur.com/PiLhq.jpg

ziL*_*iLk 5

我给你做了一个演示测试。

public class Test3 {
        private static final int WIDTH = 300;

        public Test3() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(WIDTH, 500);
            frame.setResizable(false);
            frame.setLayout(new BorderLayout());

            JPanel panel = (JPanel) frame.getContentPane();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            //my icons
            Icon iconOne = UIManager.getIcon("OptionPane.informationIcon");
            Icon iconTwo = UIManager.getIcon("OptionPane.questionIcon");
            Icon iconThree = UIManager.getIcon("OptionPane.errorIcon");
            Icon iconFour = UIManager.getIcon("OptionPane.warningIcon");

            JButton jButton1 = createButton(iconOne, "Button 1", true) ;
            JButton jButton2 = createButton(iconTwo, "Button 2 sadasddfgdgdfgd", true) ;
            JButton jButton3 = createButton(iconThree, "Button 3 sad asd a", true) ;
            JButton jButton4 = createButton(iconFour, "Button 4 sadasd asdfrfere", true) ;
            JButton jButton5 = createButton(iconTwo, "Button 5 sad", false) ;
            JButton jButton6 = createButton(iconFour, "Button 6 sadrfere", false) ;
            JButton jButton7 = createButton(iconThree, "Button 7 sadarfere", false) ;
            JButton jButton8 = createButton(iconFour, "Button 8 sadasd asdfrfere", false) ;

            panel.add(jButton1);
            panel.add(jButton2);
            panel.add(jButton3);
            panel.add(jButton4);
            panel.add(jButton5);
            panel.add(jButton6);
            panel.add(jButton7);
            panel.add(jButton8);

            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            new Test3();
        }

        private JButton createButton(Icon p_jIcon, String p_strButtonText, boolean p_bIsIconLeftSide){
            int nButtonHeight = 60;
            int nGap = 40;

            JButton jButton = new JButton();
            jButton.setIcon(p_jIcon);
            jButton.setIconTextGap(nGap);

            if(p_bIsIconLeftSide){
                jButton.setHorizontalAlignment(SwingConstants.LEFT);
                jButton.setHorizontalTextPosition(SwingConstants.RIGHT);
                jButton.setText(p_strButtonText);
            }else{
                //if you want to set icon position to right side of the button
                jButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                jButton.setHorizontalAlignment(SwingConstants.RIGHT);
                jButton.setHorizontalTextPosition(SwingConstants.LEFT);
                jButton.setText("<html><div align=left width=200px>" + p_strButtonText + "</div></html>");
            }

            Dimension jSize = new Dimension(WIDTH, nButtonHeight);
            jButton.setPreferredSize(jSize);
            jButton.setMaximumSize(jSize);
            jButton.setMinimumSize(jSize);
            return jButton;
        }
    }
Run Code Online (Sandbox Code Playgroud)

演示截图