如何在 JSplitPane 中将背景颜色设置为分隔线

发布于 2024-08-25 08:59:26 字数 3315 浏览 4 评论 0原文

我在 JSplitPane 中创建了一个分隔线。我无法设置分隔线的颜色。我想设置分隔线的颜色。请帮助我如何设置该分隔线的颜色。

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

public class SplitPaneDemo {

    JFrame frame;
    JPanel left, right;
    JSplitPane pane;
    int lastDividerLocation = -1;

    public static void main(String[] args) {
        SplitPaneDemo demo = new SplitPaneDemo();
        demo.makeFrame();
        demo.frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        demo.frame.show();
    }

    public JFrame makeFrame() {
        frame = new JFrame();
        // Create a horizontal split pane.
        pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        left = new JPanel();
        left.setBackground(Color.red);
        pane.setLeftComponent(left);
        right = new JPanel();
        right.setBackground(Color.green);
        pane.setRightComponent(right);

        JButton showleft = new JButton("Left");
        showleft.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(left, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showright = new JButton("Right");
        showright.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(right, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showboth = new JButton("Both");
        showboth.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                pane.setLeftComponent(left);
                pane.setRightComponent(right);
                c.add(pane, BorderLayout.CENTER);
                if (lastDividerLocation >= 0) {
                    pane.setDividerLocation(lastDividerLocation);
                }
                c.validate();
                c.repaint();
            }
        });

        JPanel buttons = new JPanel();
        buttons.setLayout(new GridBagLayout());
        buttons.add(showleft);
        buttons.add(showright);
        buttons.add(showboth);
        frame.getContentPane().add(buttons, BorderLayout.NORTH);

        pane.setPreferredSize(new Dimension(400, 300));
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.pack();
        pane.setDividerLocation(0.5);
        return frame;
    }
}

谢谢 苏尼尔·库马尔·萨胡

I have created a divider in JSplitPane. I am unable to set the color of divider. I want to set the color of divider. Please help me how to set color of that divider.

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

public class SplitPaneDemo {

    JFrame frame;
    JPanel left, right;
    JSplitPane pane;
    int lastDividerLocation = -1;

    public static void main(String[] args) {
        SplitPaneDemo demo = new SplitPaneDemo();
        demo.makeFrame();
        demo.frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        demo.frame.show();
    }

    public JFrame makeFrame() {
        frame = new JFrame();
        // Create a horizontal split pane.
        pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        left = new JPanel();
        left.setBackground(Color.red);
        pane.setLeftComponent(left);
        right = new JPanel();
        right.setBackground(Color.green);
        pane.setRightComponent(right);

        JButton showleft = new JButton("Left");
        showleft.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(left, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showright = new JButton("Right");
        showright.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(right, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showboth = new JButton("Both");
        showboth.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                pane.setLeftComponent(left);
                pane.setRightComponent(right);
                c.add(pane, BorderLayout.CENTER);
                if (lastDividerLocation >= 0) {
                    pane.setDividerLocation(lastDividerLocation);
                }
                c.validate();
                c.repaint();
            }
        });

        JPanel buttons = new JPanel();
        buttons.setLayout(new GridBagLayout());
        buttons.add(showleft);
        buttons.add(showright);
        buttons.add(showboth);
        frame.getContentPane().add(buttons, BorderLayout.NORTH);

        pane.setPreferredSize(new Dimension(400, 300));
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.pack();
        pane.setDividerLocation(0.5);
        return frame;
    }
}

Thanks
Sunil kumar Sahoo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云悠悠 2024-09-01 08:59:26

或者,由于分隔符是一个容器,因此您可以执行以下操作:

dividerContainer = (BasicSplitPaneDivider) splitPane.getComponent(2);
Component leftBtn = dividerContainer.getComponent(0);
Component rightBtn = dividerContainer.getComponent(1);
dividerContainer.setBackground(Color.white);
dividerContainer.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
dividerContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
dividerContainer.add(toolbar);
dividerContainer.setDividerSize(toolbar.getPreferredSize().height); 

Or, since the divider is a container, you can do the following:

dividerContainer = (BasicSplitPaneDivider) splitPane.getComponent(2);
Component leftBtn = dividerContainer.getComponent(0);
Component rightBtn = dividerContainer.getComponent(1);
dividerContainer.setBackground(Color.white);
dividerContainer.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
dividerContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
dividerContainer.add(toolbar);
dividerContainer.setDividerSize(toolbar.getPreferredSize().height); 
离线来电— 2024-09-01 08:59:26

此代码对我有用:

splitPane.setUI(new BasicSplitPaneUI() {
        public BasicSplitPaneDivider createDefaultDivider() {
        return new BasicSplitPaneDivider(this) {
            public void setBorder(Border b) {
            }

            @Override
                public void paint(Graphics g) {
                g.setColor(Color.GREEN);
                g.fillRect(0, 0, getSize().width, getSize().height);
                    super.paint(g);
                }
        };
        }
    });
    splitPane.setBorder(null);

您可以更改分隔线颜色“g.setColor(new Color(R,G,B))”。

This code works for me:

splitPane.setUI(new BasicSplitPaneUI() {
        public BasicSplitPaneDivider createDefaultDivider() {
        return new BasicSplitPaneDivider(this) {
            public void setBorder(Border b) {
            }

            @Override
                public void paint(Graphics g) {
                g.setColor(Color.GREEN);
                g.fillRect(0, 0, getSize().width, getSize().height);
                    super.paint(g);
                }
        };
        }
    });
    splitPane.setBorder(null);

You can change divider color "g.setColor(new Color(R,G,B))".

冬天旳寂寞 2024-09-01 08:59:26

这对我来说效果很好。首先,您使用它的常规方法创建 JFrame,例如 setDefaultCloseOperation()setBounds()getContentPane()。然后从您的类创建一个对象,然后使用它来调用整个程序中的所有其他方法,在本例中我创建了名为 app 的对象。您必须记住的一件事是不要忘记使用 ActionListener e

此外,当您从 getSource() 获取颜色更改的值时,颜色更改必须与 setBackground() 函数一起使用。

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

public class Main implements ActionListener {

  private static void createAndShowGUI() {
    Main app=new Main();
    // make frame..
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("I am a JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20,30,300,120);
    frame.setLayout(null);

    //Create a split pane
    JSplitPane myPane = new JSplitPane();
    myPane.setOpaque(true);
    myPane.setDividerLocation(150);

    app.right = new JPanel();
    app.right.setBackground(new Color(255,0,0));
    app.left = new JPanel();
    app.left.setBackground(new Color(0,255,0));
    app.left.setLayout(null);
    myPane.setRightComponent(app.right);
    myPane.setLeftComponent(app.left);
    // make buttons
    app.butt1=new JButton("Red");
    app.butt2=new JButton("Blue");
    app.butt3=new JButton("Green");
    // add and size buttons
    app.left.add(app.butt1);
    app.butt1.setBounds(10,10, 100,20);
    app.left.add(app.butt2);
    app.butt2.setBounds(10,30, 100,20);
    app.left.add(app.butt3);
    app.butt3.setBounds(10,50, 100,20);
    // set up listener
    app.butt1.addActionListener(app);
    app.butt2.addActionListener(app);
    app.butt3.addActionListener(app);
    frame.setContentPane(myPane);
    frame.setVisible(true);              
  }

  public void actionPerformed(ActionEvent e)
  {
    // check which button and act accordingly
    if (e.getSource()==butt1)
      right.setBackground(new Color(255,0,0));
    if (e.getSource()==butt2)
      right.setBackground(new Color(0,0,255)); 
    if (e.getSource()==butt3)
      right.setBackground(new Color(0,255,0));
  }


  public static void main(String[] args) {
    // start off..
    try  {        
      UIManager.setLookAndFeel(   
      "javax.swing.plaf.metal.MetalLookAndFeel"   );   
    } 
    catch (Exception e) 
    {
      System.out.println("Cant get laf"); 
    }
    Object a[]= UIManager.getInstalledLookAndFeels();
    for (int i=0; i<a.length; i++)
      System.out.println(a[i]); 


    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  // application object fields    
  int clickCount=0;  

  JLabel label;
  JButton butt1;
  JButton butt2;
  JButton butt3;
  JPanel left;
  JPanel right;
}

This worked for me fine.First you are creating JFrame with it's normal methods such as setDefaultCloseOperation(), setBounds(), getContentPane(). Then create an object from your class then use that to call all the other methods through out the program, in this case I created object called app. One thing you have to keep in mind is that don't forget to use ActionListener e.

Also color changes must go with setBackground() function, while you getting the values from getSource() for the color change.

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

public class Main implements ActionListener {

  private static void createAndShowGUI() {
    Main app=new Main();
    // make frame..
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("I am a JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20,30,300,120);
    frame.setLayout(null);

    //Create a split pane
    JSplitPane myPane = new JSplitPane();
    myPane.setOpaque(true);
    myPane.setDividerLocation(150);

    app.right = new JPanel();
    app.right.setBackground(new Color(255,0,0));
    app.left = new JPanel();
    app.left.setBackground(new Color(0,255,0));
    app.left.setLayout(null);
    myPane.setRightComponent(app.right);
    myPane.setLeftComponent(app.left);
    // make buttons
    app.butt1=new JButton("Red");
    app.butt2=new JButton("Blue");
    app.butt3=new JButton("Green");
    // add and size buttons
    app.left.add(app.butt1);
    app.butt1.setBounds(10,10, 100,20);
    app.left.add(app.butt2);
    app.butt2.setBounds(10,30, 100,20);
    app.left.add(app.butt3);
    app.butt3.setBounds(10,50, 100,20);
    // set up listener
    app.butt1.addActionListener(app);
    app.butt2.addActionListener(app);
    app.butt3.addActionListener(app);
    frame.setContentPane(myPane);
    frame.setVisible(true);              
  }

  public void actionPerformed(ActionEvent e)
  {
    // check which button and act accordingly
    if (e.getSource()==butt1)
      right.setBackground(new Color(255,0,0));
    if (e.getSource()==butt2)
      right.setBackground(new Color(0,0,255)); 
    if (e.getSource()==butt3)
      right.setBackground(new Color(0,255,0));
  }


  public static void main(String[] args) {
    // start off..
    try  {        
      UIManager.setLookAndFeel(   
      "javax.swing.plaf.metal.MetalLookAndFeel"   );   
    } 
    catch (Exception e) 
    {
      System.out.println("Cant get laf"); 
    }
    Object a[]= UIManager.getInstalledLookAndFeels();
    for (int i=0; i<a.length; i++)
      System.out.println(a[i]); 


    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  // application object fields    
  int clickCount=0;  

  JLabel label;
  JButton butt1;
  JButton butt2;
  JButton butt3;
  JPanel left;
  JPanel right;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文