2013-03-05 33 views
3

我正在嘗試創建一個JFrame,其中包含僅包含選擇RGB顏色所必需的內容的JPanel。我一直在使用JColorChooser,AbstractColorChooserPanel和ColorModel,閱讀Oracle教程,但我沒有理解如何開發出我想要的東西。我甚至還下載了OpenJDK源代碼來獲取這些類的源代碼,但仍然沒有。我想是這樣的:RGB-only JColorChooser(Java 7)

What I want

阿爾法東西應該消失,顏色代碼字段應設置爲不可見,但在單擊「是」按鈕時繼續工作,所以我可以檢索代碼(在一個actionPerformed方法中,我猜)。這也是重寫paintComponent方法的一個很好的補充。

在此先感謝。

編輯:這是我得到了什麼我現在有(上面的照片,沒有「Paint'-編輯):

for (final AbstractColorChooserPanel accp : panels) { 
    if (accp.getDisplayName().equals("RGB")) { 
    JOptionPane.showOptionDialog(Main.frame, accp, 
    "Color selection tool", JOptionPane.OK_OPTION, 
    JOptionPane.QUESTION_MESSAGE, null, null, 0); 
    } 
} 

EDIT2:到目前爲止,我現在已經能夠刪除阿爾法的東西,但我還不能'找到'顯示顏色代碼的標籤和字段,所以他們不斷顯示,另外,由於我無法訪問字段,我不能訪問顏色代碼:

achieved

這是此代碼:

package edu.utils; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GradientPaint; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.lang.reflect.Array; 
import java.lang.reflect.Field; 

import javax.swing.JColorChooser; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JSpinner; 
import javax.swing.WindowConstants; 
import javax.swing.colorchooser.AbstractColorChooserPanel; 

import edu.io.local.Log; 

public final class RGBColorChooserPanel extends JDialog implements ActionListener { 

private final JColorChooser jCC; 
private final JPanel  panel; 
private String    colorCode; 

public RGBColorChooserPanel(final String title) { 
super(edu.Main.frame); 
this.setTitle(title); 
this.jCC = new JColorChooser(); 
this.modifyJColorChooser(); 
this.panel = new JPanel() { 
    @Override 
    protected void paintComponent(final Graphics g) { 
    final Graphics2D g2d = (Graphics2D) g; 
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 

    final GradientPaint gp = new GradientPaint(0, 0, Color.BLUE, 0, this.getHeight(), 
      Color.BLACK); 

    g2d.setPaint(gp); 
    g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); 

    super.paintComponent(g); 
    } 
}; 
this.panel.add(this.jCC); 
this.panel.setOpaque(false); 
this.jCC.setOpaque(false); 
this.jCC.setPreviewPanel(new JPanel()); 
this.jCC.setColor(120, 20, 57); 
this.add(this.panel, BorderLayout.CENTER); 
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); 
this.pack(); 

final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
this.setLocation((screenSize.width - this.getWidth())/2, 
     (screenSize.height - this.getHeight())/2); 
this.setResizable(false); 
this.setVisible(true); 
} 

@Override 
public void actionPerformed(final ActionEvent e) { 
// TODO Auto-generated method stub 

} 

private void modifyJColorChooser() { 
final AbstractColorChooserPanel[] panels = this.jCC.getChooserPanels(); 
for (final AbstractColorChooserPanel accp : panels) { 
    if (!accp.getDisplayName().equals("RGB")) { 
    this.jCC.removeChooserPanel(accp); 
    } 
} 

final AbstractColorChooserPanel[] colorPanels = this.jCC.getChooserPanels(); 
final AbstractColorChooserPanel cp = colorPanels[0]; 

Field f = null; 
try { 
    f = cp.getClass().getDeclaredField("panel"); 
} catch (NoSuchFieldException | SecurityException e) { 
    Log.log(e); 
} 
f.setAccessible(true); 

Object colorPanel = null; 
try { 
    colorPanel = f.get(cp); 
} catch (IllegalArgumentException | IllegalAccessException e) { 
    Log.log(e); 
} 

Field f2 = null; 
try { 
    f2 = colorPanel.getClass().getDeclaredField("spinners"); 
} catch (NoSuchFieldException | SecurityException e4) { 
    Log.log(e4); 
} 
f2.setAccessible(true); 
Object rows = null; 
try { 
    rows = f2.get(colorPanel); 
} catch (IllegalArgumentException | IllegalAccessException e3) { 
    Log.log(e3); 
} 

final Object transpSlispinner = Array.get(rows, 3); 
Field f3 = null; 
try { 
    f3 = transpSlispinner.getClass().getDeclaredField("slider"); 
} catch (NoSuchFieldException | SecurityException e) { 
    Log.log(e); 
} 
f3.setAccessible(true); 
JSlider slider = null; 
try { 
    slider = (JSlider) f3.get(transpSlispinner); 
} catch (IllegalArgumentException | IllegalAccessException e2) { 
    Log.log(e2); 
} 
slider.setVisible(false); 
Field f4 = null; 
try { 
    f4 = transpSlispinner.getClass().getDeclaredField("spinner"); 
} catch (NoSuchFieldException | SecurityException e1) { 
    Log.log(e1); 
} 
f4.setAccessible(true); 
JSpinner spinner = null; 
try { 
    spinner = (JSpinner) f4.get(transpSlispinner); 
} catch (IllegalArgumentException | IllegalAccessException e) { 
    Log.log(e); 
} 
spinner.setVisible(false); 
Field f5 = null; 
try { 
    f5 = transpSlispinner.getClass().getDeclaredField("label"); 
} catch (NoSuchFieldException | SecurityException e1) { 
    Log.log(e1); 
} 
f5.setAccessible(true); 
JLabel label = null; 
try { 
    label = (JLabel) f5.get(transpSlispinner); 
} catch (IllegalArgumentException | IllegalAccessException e) { 
    Log.log(e); 
} 
label.setVisible(false); 

Field f6 = null; 
try { 
    f6 = transpSlispinner.getClass().getDeclaredField("value"); 
} catch (NoSuchFieldException | SecurityException e1) { 
    Log.log(e1); 
} 
f6.setAccessible(true); 
float value = 0; 
try { 
    value = (float) f6.get(transpSlispinner); 
} catch (IllegalArgumentException | IllegalAccessException e) { 
    Log.log(e); 
} 
} 
} 

PS:我知道的異常處理的古怪,但我需要管理每個語句例外,所以請上沒有怨言。

+5

看看http://stackoverflow.com/questions/12026767/java-7-jcolorchooser-disable-transparency-slider – 2013-03-05 15:31:36

+0

使用反射建議在第一個答覆看起來像一個艱難的方式來得到這樣一個簡單的東西。如果這是最快的方法,我認爲Oracle應該認真考慮重新設計JColorChooser類。無論如何,我想我可以從中得到一些東西。如果我能做到的話,我會用最終的代碼編輯我的文章。 – 2013-03-05 15:45:59

+0

通過擴展'AbstractColorChooserPanel',您更好的選擇是[*創建自定義選擇器面板*](http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html#chooserpanel)。 – trashgod 2013-03-05 16:19:34

回答