2015-10-05 31 views
1

我一直在學習Java只是幾個星期,所以記住..我已經設法解決很多問題,直到這一個我自己:如何使用JColorChooser在其他類中給出Color?

我想要實現的是在選項卡式視圖中使用JComponent,以便在一個標籤你可以選擇一種顏色(我的println顯示它實際上獲得了sRGB值)。然後在另一個類中,我應該能夠獲得這個值並用它着色JPanel。 我可以通過Color對象嗎?或者什麼是最佳實現方式。我在這裏嘗試的並不是太好。很抱歉的亂碼 - 我是一個新手... 這裏是顏色選擇的主要部分:

public class Colors extends JPanel implements ChangeListener { 

    private JColorChooser jcc = null; 
    protected JLabel title; 
    static Color newColor; 

    public Colors() { 
     super(new BorderLayout()); 

     //Set up the banner at the top of the window 
     title = new JLabel(); 

     //Set up color chooser for setting text color 
     jcc = new JColorChooser(title.getForeground()); 
     jcc.getSelectionModel().addChangeListener(this); 
     jcc.setBorder(BorderFactory.createTitledBorder(
       "Choose New Color")); 

     AbstractColorChooserPanel[] panels=jcc.getChooserPanels(); 
     for(AbstractColorChooserPanel p:panels) { 
      String displayName = p.getDisplayName(); 
      switch (displayName) { 
       case "HSV": 
        jcc.removeChooserPanel(p); 
        break; 
       case "HSL": 
        jcc.removeChooserPanel(p); 
        break; 
       case "CMYK": 
        jcc.removeChooserPanel(p); 
        break; 
       case "RGB": 
        jcc.removeChooserPanel(p); 
        break; 
      } 
     } 
     add(jcc, BorderLayout.PAGE_END); 
    } 



    public void stateChanged(ChangeEvent e) { 
     Color newColor = jcc.getColor(); 
     title.setForeground(newColor); 
     System.out.println("color = " + newColor); 
    } 

    public static Color getNewCol() { 
     System.out.println("this now =" + newColor); 
     return newColor; 
    } 

    public static Component createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("Color Selector"); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     //Create and set up the content pane. 
     JComponent newContentPane = new Colors(); 
     return newContentPane; 

    } 
} 

然後我試圖讓所選擇的顏色保持在Main.Java類這種方式(這可能是錯誤的)。我也注意到,價值似乎仍然總是空 - 也許我在一個錯誤的方式(α值丟失)

a.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Click add"); 
       value++; 

       if (value < 21) { 


        JButton jb1 = new JButton(); 

        //How to get the COLOR from the JColorChooser class? 
        Color box = Colors.getNewCol(); 

        jb1.setBackground(box); 
        jb1.setOpaque(true); 

        //FOR TEST ONLY jb1.setBackground(Color.BLUE); 
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
        panel.add(jb1); 
        panel.setVisible(true); 

        //window.add(paneeli); 

        window.pack(); 

        d = new Dimension(700, 500); 
        window.setSize(d); 

也許答案是太明顯了,但我就是不能看到它的那一刻實例。

回答

2

我會拿你的靜態newColor變量,並使其非靜態。在我的ChangeListener中,我將觸發JPanel的先天PropertyChangeSupport,以便可以通知偵聽器。關鍵是使用觀察者設計模式 - 例如:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.*; 
import javax.swing.event.*; 

@SuppressWarnings("serial") 
public class TestColors extends JPanel { 
    private JTabbedPane tabbedPane = new JTabbedPane(); 
    private Colors colors = new Colors(); 
    private JPanel colorDisplayPanel = new JPanel(); 

    public TestColors() { 
     tabbedPane.add("Colors", colors); 
     tabbedPane.add("Color Display", colorDisplayPanel); 
     setLayout(new BorderLayout()); 
     add(tabbedPane); 

     // add a PropertyChangeListener to our Colors isntance. 
     colors.addPropertyChangeListener(Colors.NEW_COLOR, 
       new PropertyChangeListener() { 

        @Override 
        public void propertyChange(PropertyChangeEvent evt) { 
         Color color = (Color) evt.getNewValue(); 
         colorDisplayPanel.setBackground(color); 
        } 
       }); 
    } 

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

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

@SuppressWarnings("serial") 
class Colors extends JPanel implements ChangeListener { 
    public static final String NEW_COLOR = "new color"; 
    private JColorChooser jcc = null; 
    protected JLabel title; 
    private Color newColor = null; 

    public Colors() { 
     super(new BorderLayout()); 

     // Set up the banner at the top of the window 
     title = new JLabel("This is my Banner!", SwingConstants.CENTER); 

     // Set up color chooser for setting text color 
     jcc = new JColorChooser(title.getForeground()); 
     jcc.getSelectionModel().addChangeListener(this); 
     jcc.setBorder(BorderFactory.createTitledBorder("Choose New Color")); 

     add(jcc, BorderLayout.CENTER); 
     add(title, BorderLayout.PAGE_START); 
    } 

    public void stateChanged(ChangeEvent e) { 
     // Color newColor = jcc.getColor(); 
     Color oldValue = newColor; 
     newColor = jcc.getColor(); 

     // fire a notification to the Colors JPanel's property change support 
     // object. Any listeners will be notified of the color change 
     firePropertyChange(NEW_COLOR, oldValue, newColor); 
     title.setForeground(newColor); 
    } 

    public Color getNewCol() { 
     System.out.println("this now =" + newColor); 
     return newColor; 
    } 

} 
+0

這是一個很好的提示!我實際上並沒有意識到.beans庫中的JPanel的PropertyChangeListener。我已經採納了對Colors.java的建議更改,並且也採用了Main.java中的大多數部分。您的代碼證明可以採用JColorChooser的顏色,並將其用作frontPg的背景顏色。然而,我仍然在爲如何在新的JButton背景中使用其他方法中的(選定的)Color(用戶應該選擇新按鈕的顏色,而「添加」應該用新的clr顯示新的btn)努力工作。 –

+0

我仍然卡住由於PropertyChangeListener等是非常嚴格定義的(並且不允許返回值),所以無法傳遞Color的實例。我應該用我自己的方法擴展它們以將Color傳遞給其他類還是什麼是最好的方法? –

+0

@ JackGarfield:屬性更改監聽器** do **具有有效的返回值 - 從PropertyChangeEvent返回的'getNewValue()'。但無論如何,另一種解決方案是隻要偵聽器通知偵聽器就從被監聽類中調用getter方法如果沒有,請提出一個新問題併發布你的[mcve]。 –