【问题标题】:How to change the color of a JSplitPane如何更改 JSplitPane 的颜色
【发布时间】:2012-04-11 15:42:34
【问题描述】:

我在阅读一本关于摇摆的书时编写了一个小程序,它在两个标签之间创建了一个 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();
            }

        });
    }
}

有什么办法可以改变它的颜色,让它真正脱颖而出? 谢谢。

【问题讨论】:

  • 不错的来源:简短,切中要害,格式清晰并显示问题(以图形方式)。 +1

标签: java swing


【解决方案1】:

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

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

【讨论】:

  • 这也是一个不错的解决方案。不过我有一个问题:
  • 为什么UIManager.put("SplitPane.foreground", new Color(0xff8080ff)); 不能单独工作(不改变标签或拆分窗格的任何背景)?
  • 当我注释掉 panel.setBackground() 时,SplitPane.background 颜色仍然显示。注意JPanel默认是不透明的,不像JLabel
  • 其实你的答案是对上一个答案的补充,但我不能接受两个答案! :(
【解决方案2】:

JLabel默认为NON_Opaque,简单就是透明,可以

  • JLabels 更改为JComponentJPanel 可能会更好

  • 通过JLabel#setOpaque(true)更改不透明度

【讨论】:

  • 在这种情况下,我会选择JLabel.setOpaque(true),如果只是因为从当前源到工作的代码行更改较少。当然,您需要调用JLabel.setBackground(Color),这会使行数加倍。 ;)
  • +1 或者,设置容器的背景颜色和/或组件属性,如图所示here
  • 我不知道 JLabel 的默认属性设置。
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 2020-09-16
  • 2011-03-23
  • 2011-01-02
  • 2012-10-16
  • 2012-06-01
  • 2021-10-23
相关资源
最近更新 更多