2015-11-19 54 views
0

想要將我的背景和JSliders保持爲程序的默認顏色,在我的JLabel中將紅色,綠色和藍色的文本顏色更改爲合適的顏色。我不關心高度和寬度的顏色,而只是JSliders旁邊的文字,它們表示紅色,藍色和綠色。任何幫助表示讚賞!謝謝!向我的JSliders旁邊的JLabel添加顏色

import java.awt.*; 
import javax.swing.*; 
import java.io.*; 
import java.net.*; 
import javax.swing.event.*; 
public class ControlApp extends JFrame 
{ 
    private JPanel mainPanel; 
    private PrintWriter out; 
    private JSlider height,width,red,green,blue; 
    private String aspect; 
    private String value; 

public static void main(String []args) 
{ 
    new ControlApp(); 
} 

public ControlApp() 
{ 
    super("ControlApp"); 
    mainPanel = new JPanel(); 
    add(mainPanel,BorderLayout.CENTER); 

    //SliderListener sl = new SliderListener(); 

    height = new JSlider(JSlider.VERTICAL, 0,500,0); 
    //height.addChangeListener(sl); 
    width = new JSlider(JSlider.VERTICAL, 0,500,0); 
    //width.addChangeListener(sl); 
    red = new JSlider(JSlider.VERTICAL, 0,255,0); 
    //red.addChangeListener(sl);  
    green = new JSlider(JSlider.VERTICAL, 0,255,0); 
    //green.addChangeListener(sl); 
    blue = new JSlider(JSlider.VERTICAL, 0,255,0); 
    //blue.addChangeListener(sl); 

    mainPanel.add(new JLabel("Height")); 
    mainPanel.add(height,BorderLayout.CENTER); 
    mainPanel.add(new JLabel("Width")); 
    mainPanel.add(width,BorderLayout.CENTER); 
    mainPanel.add(new JLabel("Red")); 
    mainPanel.add(red,BorderLayout.CENTER); 
    mainPanel.add(new JLabel("Green")); 
    mainPanel.add(green,BorderLayout.CENTER); 
    mainPanel.add(new JLabel("Blue")); 
    mainPanel.add(blue,BorderLayout.CENTER); 

    setLocationRelativeTo(null); 
    this.setSize(500,250); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 
} 
} 
+0

那不是我問。 ChangeListener將套接字傳遞給另一個類來更改JFrame的背景。我希望能夠在我的JLabel中更改我的文本(紅色,綠色,藍色)的顏色。 – ttrigger10

+0

@HovercraftFullOfEels – ttrigger10

+0

好吧,我的壞 - 只是設置他們的前景色。但是,爲什麼在向使用FlowLayout的JPanel中添加組件時使用BorderLayout常量(錯誤)? –

回答

1

ttrigger10 -

你要設置這些的JLabel的前景色,讓他們成爲一個特定的顏色與setForgroundColor方法。看到修改後的代碼,下面來改變顏色:

JLabel redLabel, blueLabel, greenLabel; 
    redLabel = new JLabel("Red"); 
    redLabel.setForeground(Color.RED); 
    greenLabel = new JLabel("Green"); 
    greenLabel.setForeground(Color.GREEN); 
    blueLabel = new JLabel("Blue"); 
    blueLabel.setForeground(Color.BLUE); 

    mainPanel.add(new JLabel("Height")); 
    mainPanel.add(height, BorderLayout.CENTER); 
    mainPanel.add(new JLabel("Width")); 
    mainPanel.add(width, BorderLayout.CENTER); 
    mainPanel.add(redLabel); 
    mainPanel.add(red, BorderLayout.CENTER); 
    mainPanel.add(greenLabel); 
    mainPanel.add(green, BorderLayout.CENTER); 
    mainPanel.add(blueLabel); 
    mainPanel.add(blue, BorderLayout.CENTER); 
+0

好吧。我只需要爲每個創建一個JPanel。謝謝! – ttrigger10

+0

歡迎您!是的,你需要預先聲明JLabel,以便修改它們。如果您使用mainPanel.add()內聯創建它們,您會遇到一個普通標籤(缺少CSS /其他樣式) – TheGrandPackard

2

你不應該使用BorderLayout的常數增加它默認使用的FlowLayout一個JPanel時。實際上,我建議您完全使用完全不同的佈局,即GridLayout,以便所有列都排列得很整齊。另一種選擇是使用的TitledBorder在你JSlider的是這樣的:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.util.EnumMap; 
import java.util.Map; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 
import javax.swing.event.*; 

public class ControlPanel extends JPanel { 
    private static final int GAP = 3; 
    private Map<Control, JSlider> sliderMap = new EnumMap<>(Control.class); 

    public ControlPanel() { 
     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     setLayout(new GridLayout(1, 0, GAP, GAP)); 
     for (Control control : Control.values()) { 
      JSlider slider = new JSlider(JSlider.VERTICAL, 0, control.getMaxValue(), 0); 
      slider.setPaintLabels(true); 
      slider.setPaintTicks(true); 
      slider.setPaintTrack(true); 
      slider.setMajorTickSpacing(100); 
      slider.setMinorTickSpacing(20); 
      slider.addChangeListener(new SliderListener(control)); 
      sliderMap.put(control, slider); 
      JPanel panel = new JPanel(new BorderLayout()); 
      TitledBorder titleBorder = BorderFactory.createTitledBorder(control.getText()); 
      titleBorder.setTitleColor(control.getColor()); 
      panel.setBorder(titleBorder); 
      panel.add(slider); 
      add(panel);    
     } 
    } 

    private class SliderListener implements ChangeListener { 
     private Control control; 

     public SliderListener(Control control) { 
      this.control = control; 
     } 

     @Override 
     public void stateChanged(ChangeEvent e) { 
      JSlider slider = (JSlider) e.getSource(); 
      int value = slider.getValue();    
      System.out.printf("Control: %s, Value: %d%n", control, value); 
     } 

    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Control Panel"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new ControlPanel()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 

public enum Control { 
    HEIGHT("Height", 500, Color.BLACK), WIDTH("Width", 500, Color.black), RED("Red", 255, Color.RED), 
    GREEN("Green", 255, Color.GREEN), BLUE("Blue", 255, Color.BLUE); 

    private String text; 
    private int maxValue; 
    private Color color; 

    private Control(String text, int maxValue, Color color) { 
     this.text = text; 
     this.maxValue = maxValue; 
     this.color = color; 
    } 

    public String getText() { 
     return text; 
    } 

    public int getMaxValue() { 
     return maxValue; 
    } 

    public Color getColor() { 
     return color; 
    } 

} 

其中爲顯示:

enter image description here

+0

感謝您的輸入,現在我知道何時使用哪種佈局! @HovercraftFullOfEels – ttrigger10