2012-04-11 20 views
4

2つのラベルの間にJSplitPaneを作成する、スイングに関する本を読んでいるときに、小さなプログラムを作成しました。 問題はJSplitPaneが(私のオペレーティングシステムのMAC OS Lionでは)ほとんど見られず、フォアグラウンドカラーのようないくつかのプロパティを設定しても動作しないように見えます。ここでJSplitPaneの色を変更する方法

はコードです:

//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

ニース源:短い、ポイントに、明確にフォーマットされ(グラフ)問題を表示します。 +1 –

答えて

5

JLabelはシンプルで透明であり、デフォルトNON_Opaqueであり、JComponentまたはJPanelにすることができます

  • 変更JLabelsあなたがJLabel#setOpaque(true)

+0

この場合、 'JLabel.setOpaque(true)'と一緒に行こうとします。実際のソースから現在のソースに変更するコード行が少なくて済む場合に限ります。もちろん、JLabel.setBackground(Color)を呼び出す必要もあります。これは行数を2倍にします。 ;) –

+0

+1また、[ここ](http://stackoverflow.com/a/10110232/230513)に示すように、コンテナの背景色および/またはコンポーネントプロパティを設定します。 – trashgod

+0

JLabelのデフォルトのプロパティ設定がわかりませんでした。 – skiabox

7

ことによって、より良い

  • 変更不透明かもしれませんSplitPane.backgroundプロパティを使用できます。下に示された。

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

    参照[* Java Swingのset(Preferred | Maximum | Minimum)Sizeメソッドを使用しないでください。*](http://stackoverflow.com/q/7229226/230513)。 – trashgod

    +0

    thats correct私は 'Whatever#setBackground'を+1したことを忘れてしまった+1 – mKorbel

    +0

    この問題も解決しました: – skiabox

    関連する問題