java - 为什么我使用 jpanel 在不同的计算机上得到不同的结果?

标签 java swing jpanel

我使用java和jpanel编写了一些程序。 但我在不同的操作系统(windows xp、mac osx)中得到不同的结果(颜色、边框...)。 这是为什么?

最佳答案

Windows 和 OSx 有不同的主题。由于您使用默认的 LookAndFeel,您会得到不同的 View 。

尝试com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

作为旁注,您还可以使用以下类来根据操作系统更改样式。

它不支持 OSx,但当然,您可以修改它。

public final class Common
    {
    // ------------------------------ CONSTANTS ------------------------------

    private static final boolean DEBUGGING = false;

    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * set the look and feel for a Swing App, based on OS and JDK version..
     */
    public static void setLaf(){
                                    //"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                                    //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
        if ( setLaf( null, 1, 6, 0, "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ) )
            {
            // won't take for anything less than 1.6.0_10
            return;
            }

        if ( setLaf( "Windows Vista", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows XP", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }

        setLaf( null, 1, 3, 0, UIManager.getSystemLookAndFeelClassName() );
        // if even that failed, give up. leave original L&F in place.
        }

    /**
     * set the look and feel for a Swing App, based on OS and JDK version..
     */
    public static void setLafNim(){
                                    //"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                                    //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
        if ( setLaf( null, 1, 6, 0, "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ) )
            {
            // won't take for anything less than 1.6.0_10
            return;
            }

        if ( setLaf( "Windows Vista", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows XP", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }

        setLaf( null, 1, 3, 0, UIManager.getSystemLookAndFeelClassName() );
        // if even that failed, give up. leave original L&F in place.
        }

    // -------------------------- STATIC METHODS --------------------------

    /**
     * Attempt to set the LAF, depending on OS and JDK being suitable.
     *
     * @param os           name of OS, null means it does not matter.
     * @param wantedMajor  java major version e.g. 1
     * @param wantedMinor  Java minor version e.g. 6
     * @param wantedBugFix Java bugfix version e.g. 14
     * @param lafClassName class name of laf to set if os matches and JDK version is sufficiently advanced.
     *
     * @return true the setting took, false if failed.
     */
    private static boolean setLaf( String os, int wantedMajor,
                                   int wantedMinor,
                                   int wantedBugFix,
                                   String lafClassName )
        {
        if ( os != null && !System.getProperty( "os.name", "unknown" ).equals( os ) )
            {
            return false;
            }

        try
            {
            UIManager.setLookAndFeel( lafClassName );
            }
        catch ( UnsupportedLookAndFeelException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: UnsupportedLookAndFeelException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( IllegalAccessException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: IllegalAccessException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( InstantiationException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: InstantiationException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( ClassNotFoundException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: ClassNotFoundException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( Exception e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: Exception" + lafClassName + e.getMessage() );
                }
            return false;
            }
        if ( DEBUGGING )
            {
            System.err.println( "Choosing L&F " + lafClassName );
            }
        return true;
        }

    }

关于java - 为什么我使用 jpanel 在不同的计算机上得到不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19226702/

相关文章:

java - 在工作线程中阻塞 Java Swing 用户的模式

java - ActionListener 上未找到符号错误

java - 可以获得父边框或选项卡的名称吗?

java - 使用 jdk 1.6 时出现 SSLHandshake 异常

java - 如何使 Java 组合框中的项目居中

java - IBM JRE gencon 策略中的歧义和详细 :gc output

java - 一个 JFrame 中的两个面板

java - 在 Java 中创建一个 Count Up 计时器以中断

java - 如何打印格式化的 BigDecimal 值?

java - Java 中的 XML 转义字符