Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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 自定义按钮在mac上不工作(ButtonUI)_Java_Macos_Swing_Jbutton_Look And Feel - Fatal编程技术网

Java 自定义按钮在mac上不工作(ButtonUI)

Java 自定义按钮在mac上不工作(ButtonUI),java,macos,swing,jbutton,look-and-feel,Java,Macos,Swing,Jbutton,Look And Feel,我有一个应用程序,它在所有地方使用自定义按钮来显示文本和图标。在windows和linux上工作得很好,但现在OSX用户正在抱怨。文本不会显示在mac上,只显示“…”。代码似乎很简单,但我对Mac电脑一无所知。我怎样才能解决这个问题 测试用例: package example.swingx; import example.utils.ResourceLoader; import com.xduke.xlayouts.XTableLayout; import javax.swing.*;

我有一个应用程序,它在所有地方使用自定义按钮来显示文本和图标。在windows和linux上工作得很好,但现在OSX用户正在抱怨。文本不会显示在mac上,只显示“…”。代码似乎很简单,但我对Mac电脑一无所知。我怎样才能解决这个问题

测试用例:

package example.swingx;
import example.utils.ResourceLoader;
import com.xduke.xlayouts.XTableLayout;    
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.awt.event.ActionEvent;

public class SmallButton extends JButton {
    private static ComponentUI ui = new SmallButtonUI();
    public SmallButton() {
        super();
        /*  final RepaintManager repaintManager = RepaintManager.currentManager(this);
        repaintManager.setDoubleBufferingEnabled(false);
        setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);*/
    }
    public SmallButton(AbstractAction action) {
        super(action);
    }
    public SmallButton(String text) {
        super(text);
    }
    public SmallButton(Icon icon) {
        super(icon);
    }
    public void updateUI() {
        setUI(ui);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel buttonPanel = new JPanel(new XTableLayout());
        SmallButton firstSmallButton = new SmallButton("One");
        SmallButton secondSmallButton = new SmallButton("Two");
        SmallButton thirdSmallButton = new SmallButton();

        ImageIcon cameraIcon = (ImageIcon) ResourceLoader.getIcon("camera");
        SmallButton fourth = new SmallButton();

        fourth.setAction(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Fourth button pressed!");
            }
        });
        fourth.setIcon(cameraIcon);

        buttonPanel.add(firstSmallButton, "+");
        buttonPanel.add(secondSmallButton, "+");
        buttonPanel.add(thirdSmallButton, "+");
        buttonPanel.add(fourth, "+");

        final Container container = frame.getContentPane();
        container.add(buttonPanel);
        frame.pack();
        frame.setVisible(true);
    }
}
用户界面:


编辑:调试后,两个平台上的getMinimumSize()似乎是相同的。另外,当我停在使用图形的任何地方时,似乎mac的transY值为47,而linux的transY值为0。这47似乎也输入到剪辑区域。哪里可以设置呢?\

您对首选尺寸的计算不正确

BasicButtonUI
使用
SwingUtilities.layoutCompoundLabel
,已检查。在标签中,如果字符串太长,则添加省略号,但按钮的大小通常适合其整个文本

如果没有更好地理解您的上下文,我将使用一个,如下所示。我还展示了一个简单的
BasicButtonUI
示例,使用了一个较小的派生
Font
。可与一起用于测试

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * @see https://stackoverflow.com/a/14599176/230513
 * @see https://stackoverflow.com/a/11949899/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(new Color(0xfff0f0f0));
        f.setLayout(new GridLayout(0, 1));
        f.add(createToolBar(f));
        f.add(variantPanel("mini"));
        f.add(variantPanel("small"));
        f.add(variantPanel("regular"));
        f.add(variantPanel("large"));
        JPanel customPanel = new JPanel();
        customPanel.add(createCustom("One"));
        customPanel.add(createCustom("Two"));
        customPanel.add(createCustom("Three"));
        f.add(customPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static JPanel variantPanel(String size) {
        JPanel variantPanel = new JPanel();
        variantPanel.add(createVariant("One", size));
        variantPanel.add(createVariant("Two", size));
        variantPanel.add(createVariant("Three", size));
        return variantPanel;
    }

    private static JButton createVariant(String name, String size) {
        JButton b = new JButton(name);
        b.putClientProperty("JComponent.sizeVariant", size);
        return b;
    }

    private static JButton createCustom(String name) {
        JButton b = new JButton(name) {

            @Override
            public void updateUI() {
                super.updateUI();
                setUI(new CustomButtonUI());
            }
        };
        return b;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }

    private static class CustomButtonUI extends BasicButtonUI {

        private static final Color BACKGROUND_COLOR = new Color(173, 193, 226);
        private static final Color SELECT_COLOR = new Color(102, 132, 186);

        @Override
        protected void paintText(Graphics g, AbstractButton b, Rectangle r, String t) {
            super.paintText(g, b, r, t);
            g.setColor(SELECT_COLOR);
            g.drawRect(r.x, r.y, r.width, r.height);
        }

        @Override
        protected void paintFocus(Graphics g, AbstractButton b,
            Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
            super.paintFocus(g, b, viewRect, textRect, iconRect);
            g.setColor(Color.blue.darker());
            g.drawRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
        }

        @Override
        protected void paintButtonPressed(Graphics g, AbstractButton b) {
            if (b.isContentAreaFilled()) {
                g.setColor(SELECT_COLOR);
                g.fillRect(0, 0, b.getWidth(), b.getHeight());
            }
        }

        @Override
        protected void installDefaults(AbstractButton b) {
            super.installDefaults(b);
            b.setFont(b.getFont().deriveFont(11f));
            b.setBackground(BACKGROUND_COLOR);
        }

        public CustomButtonUI() {
            super();
        }
    }

    private static JToolBar createToolBar(final Component parent) {
        final UIManager.LookAndFeelInfo[] available =
            UIManager.getInstalledLookAndFeels();
        List<String> names = new ArrayList<String>();
        for (UIManager.LookAndFeelInfo info : available) {
            names.add(info.getName());
        }
        final JComboBox combo = new JComboBox(names.toArray());
        String current = UIManager.getLookAndFeel().getName();
        combo.setSelectedItem(current);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int index = combo.getSelectedIndex();
                try {
                    UIManager.setLookAndFeel(
                        available[index].getClassName());
                    SwingUtilities.updateComponentTreeUI(parent);
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                }
            }
        });
        JToolBar bar = new JToolBar("L&F");
        bar.setLayout(new FlowLayout(FlowLayout.LEFT));
        bar.add(combo);
        return bar;
    }
}

导入java.awt.Color;
导入java.awt.Component;
导入java.awt.EventQueue;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.awt.Rectangle;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.AbstractButton;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JToolBar;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.plaf.basic.BasicButtonUI;
/**
*@见https://stackoverflow.com/a/14599176/230513
*@见https://stackoverflow.com/a/11949899/230513
*/
公开课考试{
专用void display(){
JFrame f=新JFrame(“测试”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 立根背景(新颜色(0xFFF0));
f、 setLayout(新的GridLayout(0,1));
f、 添加(createToolBar(f));
f、 添加(variantPanel(“迷你”);
f、 添加(variantPanel(“小”);
f、 添加(variantPanel(“常规”);
f、 添加(variantPanel(“大”);
JPanel customPanel=新的JPanel();
添加(创建自定义(“一”);
添加(创建自定义(“两”);
添加(创建自定义(“三”);
f、 添加(自定义面板);
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
专用静态JPanel variantPanel(字符串大小){
JPanel variantPanel=新的JPanel();
variantPanel.add(创建变量(“一”,大小));
variantPanel.add(创建变量(“两个”,大小));
variantPanel.add(创建变量(“三”,大小));
返回variantPanel;
}
私有静态JButton createVariant(字符串名称、字符串大小){
JButton b=新JButton(名称);
b、 putClientProperty(“JComponent.sizeVariant”,size);
返回b;
}
私有静态JButton createCustom(字符串名称){
JButton b=新JButton(名称){
@凌驾
公共void updateUI(){
super.updateUI();
setUI(新的CustomButtonUI());
}
};
返回b;
}
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
新测试().display();
}
});
}
私有静态类CustomButtonUI扩展了BasicButtonUI{
私有静态最终颜色背景色=新颜色(173193226);
私有静态最终颜色选择_颜色=新颜色(102、132、186);
@凌驾
受保护的void paintText(图形g、抽象按钮b、矩形r、字符串t){
超级文本(g,b,r,t);
g、 设置颜色(选择颜色);
g、 drawRect(r.x、r.y、r.宽度、r.高度);
}
@凌驾
受保护的空白油漆焦点(图形g、抽象按钮b、,
矩形viewRect、矩形textRect、矩形iconRect){
super.paintFocus(g、b、viewRect、textRect、iconRect);
g、 setColor(Color.blue.darker());
g、 drawRect(viewRect.x、viewRect.y、viewRect.width、viewRect.height);
}
@凌驾
受保护的空白油漆按钮按下(图形g,抽象按钮b){
如果(b.isContentAreaFilled()){
g、 设置颜色(选择颜色);
g、 fillRect(0,0,b.getWidth(),b.getHeight());
}
}
@凌驾
受保护的默认值(抽象按钮b){
super.1(b);
b、 setFont(b.getFont().deriveFont(11f));
b、 立根背景(背景色);
}
公共自定义按钮(NUI){
超级();
}
}
私有静态JToolBar createToolBar(最终组件父级){
最终UIManager.LookAndFeelInfo[]可用=
UIManager.getInstalledLookAndFeels();
列表名称=新的ArrayList();
对于(UIManager.LookAndFeelInfo信息:可用){
name.add(info.getName());
}
最终JComboBox组合=新的JComboBox(names.toArray());
字符串current=UIManager.getLookAndFeel().getName();
组合设置选择项(当前);
combo.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
int index=combo.getSelectedIndex();
试一试{
UIManager.setLookAndFeel(
可用[index].getClassName());
SwingUtilities.updateComponentTreeUI(父级);
}捕获(例外e){
e、 printStackTrace(System.err);
}
}
});
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * @see https://stackoverflow.com/a/14599176/230513
 * @see https://stackoverflow.com/a/11949899/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(new Color(0xfff0f0f0));
        f.setLayout(new GridLayout(0, 1));
        f.add(createToolBar(f));
        f.add(variantPanel("mini"));
        f.add(variantPanel("small"));
        f.add(variantPanel("regular"));
        f.add(variantPanel("large"));
        JPanel customPanel = new JPanel();
        customPanel.add(createCustom("One"));
        customPanel.add(createCustom("Two"));
        customPanel.add(createCustom("Three"));
        f.add(customPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static JPanel variantPanel(String size) {
        JPanel variantPanel = new JPanel();
        variantPanel.add(createVariant("One", size));
        variantPanel.add(createVariant("Two", size));
        variantPanel.add(createVariant("Three", size));
        return variantPanel;
    }

    private static JButton createVariant(String name, String size) {
        JButton b = new JButton(name);
        b.putClientProperty("JComponent.sizeVariant", size);
        return b;
    }

    private static JButton createCustom(String name) {
        JButton b = new JButton(name) {

            @Override
            public void updateUI() {
                super.updateUI();
                setUI(new CustomButtonUI());
            }
        };
        return b;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }

    private static class CustomButtonUI extends BasicButtonUI {

        private static final Color BACKGROUND_COLOR = new Color(173, 193, 226);
        private static final Color SELECT_COLOR = new Color(102, 132, 186);

        @Override
        protected void paintText(Graphics g, AbstractButton b, Rectangle r, String t) {
            super.paintText(g, b, r, t);
            g.setColor(SELECT_COLOR);
            g.drawRect(r.x, r.y, r.width, r.height);
        }

        @Override
        protected void paintFocus(Graphics g, AbstractButton b,
            Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
            super.paintFocus(g, b, viewRect, textRect, iconRect);
            g.setColor(Color.blue.darker());
            g.drawRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
        }

        @Override
        protected void paintButtonPressed(Graphics g, AbstractButton b) {
            if (b.isContentAreaFilled()) {
                g.setColor(SELECT_COLOR);
                g.fillRect(0, 0, b.getWidth(), b.getHeight());
            }
        }

        @Override
        protected void installDefaults(AbstractButton b) {
            super.installDefaults(b);
            b.setFont(b.getFont().deriveFont(11f));
            b.setBackground(BACKGROUND_COLOR);
        }

        public CustomButtonUI() {
            super();
        }
    }

    private static JToolBar createToolBar(final Component parent) {
        final UIManager.LookAndFeelInfo[] available =
            UIManager.getInstalledLookAndFeels();
        List<String> names = new ArrayList<String>();
        for (UIManager.LookAndFeelInfo info : available) {
            names.add(info.getName());
        }
        final JComboBox combo = new JComboBox(names.toArray());
        String current = UIManager.getLookAndFeel().getName();
        combo.setSelectedItem(current);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int index = combo.getSelectedIndex();
                try {
                    UIManager.setLookAndFeel(
                        available[index].getClassName());
                    SwingUtilities.updateComponentTreeUI(parent);
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                }
            }
        });
        JToolBar bar = new JToolBar("L&F");
        bar.setLayout(new FlowLayout(FlowLayout.LEFT));
        bar.add(combo);
        return bar;
    }
}