1

我在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;
    }
}

谢谢 Sunil kumar Sahoo

4

3 回答 3

3

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

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); 
于 2010-03-15T16:09:01.810 回答
0

这段代码对我有用:

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))”。

于 2013-08-16T06:55:35.297 回答
0

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

颜色变化也必须与setBackground()功能一起使用,而您从getSource()颜色变化中获取值。

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;
}
于 2015-10-03T16:23:45.623 回答