2012-10-26 20 views
7

Pytanie znajduje się w tytule.Jak zmienić kolor JSeparatora?

Jestem obecnie robi coś takiego:

jSperator = new JSeparator(); 
jSeparator1.setForeground(new java.awt.Color(255, 51, 51)); 

Ale separator zachować jego domyślny kolor, coś 212,212,212.

Odpowiedz

11

trzeba zmienić ’Background’ zamiast ’Foreground’

logika może być różny dla Nimbus Look and Feel

metalu L & F

enter image description here

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

public class GridBagSeparator1 { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Laying Out Components in a Grid"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL); 
     sep.setBackground(Color.black); 
     JSeparator sep1 = new JSeparator(SwingConstants.HORIZONTAL); 
     sep1.setBackground(Color.blue); 
     JSeparator sep2 = new JSeparator(SwingConstants.HORIZONTAL); 
     sep2.setBackground(Color.green); 
     JSeparator sep3 = new JSeparator(SwingConstants.HORIZONTAL); 
     sep3.setBackground(Color.red); 

     frame.setLayout(new GridLayout(4, 0)); 
     frame.add(sep); 
     frame.add(sep1); 
     frame.add(sep2); 
     frame.add(sep3); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+2

Zmiana tła nie rozwiązuje problemu. Używam Matiss, konstruktora GUI Netbeans do budowania interfejsu na Macu. Może to ograniczenie wyglądu i stylu. – nathan

+4

Może trzeba użyć 'UIManager' i zmienić' Separator.foreground' – trashgod

+4

Z Sythetica Look And Feel, musiałem zmienić tło i ustawić właściwość nieprzezroczystą na true. –

4

JSeparator ma 2 kolory, jeden dla linia, jedna dla cienia. Możesz zmienić oba ustawienia kolorów odpowiednio na tle i na pierwszym planie.

JSeparator sep = new JSeparator(); 
sep.setForeground(Color.green); // top line color 
sep.setBackground(Color.green.brighter()); // bottom line color