在Jbutton上更改图标位置

时间:2016-04-23 18:01:54

标签: java swing netbeans jbutton

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

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

2 个答案:

答案 0 :(得分:2)

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

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;
        }
    }

demo screenshot

答案 1 :(得分:1)

基本JDK不支持。没有仅仅以文本为中心的事情。文本和图标都绘制在一起,左对齐,居中对齐或右对齐。

但是,您可以使用Component Border类。

这是一个自定义边框,允许您将组件添加到边框。因此,您可以创建包含Icon的JLabel,并将标签用作ComponentBorder

然后你可以设置按钮的文本,现在文本将在按钮及其边框的边界内居中。

基本代码:

JButton button = new JButton("Centered Text");
JLabel label = new JLabel( new ImageIcon("...") );

ComponentBorder cb = new ComponentBorder( label );
cb.setEdge( ComponentBorder.Edge.LEFT );
cb.install( button );

add(button, BorderLayout.NORTH);