簡體   English   中英

如何更改JSplitPane的顏色

[英]How to change the color of a JSplitPane

我寫了一個小程序,在讀一本關於swing的書時,在兩個標簽之間創建了一個JSplitPane。 問題是JSplitPane幾乎看不到(至少在我的操作系統中 - MAC OS Lion)並且在它上面設置一些屬性(如前景色)似乎不起作用。

這是代碼:

//Demonstrate a simple JSplitPane


package swingexample4_6;

import javax.swing.*;
import java.awt.*;

public class SplitPaneDemo {

    //constructor
    public SplitPaneDemo()
    {
        //Create a new JFrame container.
        //Use the default border layout
        JFrame jfrm = new JFrame("Split Pane Demo");

        //Give the frame an initial size
        jfrm.setSize(380, 150);

        //Terminate the program when the user closes the application
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //--Make two labels to show the split pane
        JLabel jlab = new JLabel(" Left side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        JLabel jlab2 = new JLabel(" Right side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");

        //Set the minimum size for each label
        //This step is not technically needed to use a split pane,
        //but it enables the split pane resizing features to be
        //used to their maximum extent
        jlab.setMinimumSize(new Dimension(90, 30));
        jlab2.setMinimumSize(new Dimension(90, 30));

        //--Create a split pane
        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jlab, jlab2);

        //Code to get a list of component names in the console
        Component[] listComponents = jsp.getComponents();

        String theList;
        for (Component myComponent: listComponents)
        {
            theList = myComponent.toString();
            System.out.println(theList);
        }


        //Add the split pane to the content pane
        jfrm.getContentPane().add(jsp);

        //Display the frame
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                new SplitPaneDemo();
            }

        });
    }
}

有什么方法可以改變它的顏色,這樣才能真正脫穎而出嗎? 謝謝。

您可以使用SplitPane.background屬性,如下所示。

SplitPane背景

import javax.swing.*;
import java.awt.*;

/** @see http://stackoverflow.com/a/10110232/230513 */
public class SplitPaneDemo {

    //constructor
    public SplitPaneDemo() {
        JFrame jf = new JFrame("Split Pane Demo");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //--Make two labels to show the split pane
        JPanel left = content("Left side: ");
        JPanel right = content("Right side: ");

        //--Create a split pane
        JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, left, right);
        jsp.setDividerLocation(0.5f);

        //Add the split pane to the frame's content pane
        jf.add(jsp);
        jf.pack();

        //Display the frame
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

        //Code to get a list of component names in the console
        for (Component myComponent : jsp.getComponents()) {
            System.out.println(myComponent);
        }
    }

    private JPanel content(String s) {
        final JLabel label = new JLabel(s + "Some text.", JLabel.CENTER);
        JPanel panel = new JPanel(new GridLayout()) {

            @Override
            public Dimension getPreferredSize() {
                Dimension d = label.getPreferredSize();
                return new Dimension(d.width * 2, d.height * 3);
            }
        };
        panel.setOpaque(true);
        panel.setBackground(new Color(0xffffffc0));
        panel.add(label);
        return panel;
    }

    public static void main(String[] args) {
        UIManager.put("SplitPane.background", new Color(0xff8080ff));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneDemo();
            }
        });
    }
}

JLabel默認是NON_Opaque ,簡單就是透明,你可以

  • JLabels更改為JComponentJPanel可能會更好

  • 通過JLabel#setOpaque(true)改變不透明度JLabel#setOpaque(true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM