2010-03-15 2 views
1

JSplitPane에 분배기를 만들었습니다. 구분선의 색상을 설정할 수 없습니다. 구분선의 색을 설정하고 싶습니다. 해당 구분선의 색상을 설정하는 방법을 알려주세요.JSplitPane에서 분배기에 BackGround 색상을 설정하는 방법

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

감사 선일 쿠마 Sahoo

답변

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); 
+0

첫 번째 링크가 정확한 위치로 이동하지 않는 것 같습니다. – Totem

+0

음, 거의 7 년 전의 일입니다. 그래서 놀랍지 않습니다. 참조 된 코드가 너무 오래되어 더 이상 사용할 수 없습니다. – Ascalonian

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 (새 색상 (R, G, B))"를 변경할 수 있습니다.

+0

코드는 만족 스럽지만 항상 설명이 있어야합니다. 이것은 길지 않아도되지만 예상됩니다. – peterh

0

제게는 setDefaultCloseOperation(), setBounds(), getContentPane()과 같은 정상적인 방법으로 JFrame을 작성하고 있습니다. 그런 다음 클래스에서 객체를 만든 다음이 객체를 사용하여 프로그램을 통해 다른 모든 메서드를 호출합니다.이 경우 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; 
} 
+0

코드는 만족 스럽지만 항상 설명이 있어야합니다. 이것은 길지 않아도되지만 예상됩니다. – peterh

+1

하지만 의견이 도움이 될 것이라고 생각했습니다. – AVI

관련 문제