2016-04-11 20 views
0

問題がありました。問題:Swing-JButtonのより良い拡張機能を作り始めました。 残りの問題の1つ:「半透明」(this.setBackground(new Color(100,100,100,90));のような)を設定すると、視覚系が変わってしまいます。マウスの上にマウスを置くたびに、最後のスウィング要素が文字列(JRadioButtonやJCheckBoxなど)私はバックグラウンドで表示されます。半透明JButton:オブジェクトが背景に表示される

私の現在のボタン:

private boolean transparent; 
private boolean drawImage; 
private final int width; 
private final int height; 
int marginWidth=15; 
int marginHeight=15; 

public MyButton(String text, String command){ 
    super(text); 
    this.setDoubleBuffered(true); 
    this.setOpaque(); 
    this.setActionCommand(command); 

    this.setBackground(ParameterPool.COLOR_BACKGROUND_SECOND); 
    this.setBorder(null); 

    this.width = (int) this.getPreferredSize().getWidth()+marginWidth; 
    this.height = (int) this.getPreferredSize().getHeight()+marginHeight; 

    this.setPreferredSize(new Dimension(this.width, this.height)); 

} 


public void setTransparent() { 
    this.transparent = true; 
    this.setOpaque(false); 
} 

public void setOpaque() { 
    this.transparent = false; 
    this.setOpaque(true); 
} 
@Override 
protected void paintComponent(Graphics g){ 
    Graphics2D g2d = (Graphics2D)g; 


    g2d.fillRoundRect(0,0,width,height,18,18); 

    g2d.setColor(Color.darkGray); 
    g2d.drawRoundRect(0,0,width, height,18,18); 

    FontRenderContext frc = new FontRenderContext(null, false, false); 
    Rectangle2D r = getFont().getStringBounds(getText(), frc); 
    float xMargin = (float)(width-r.getWidth())/2; 
    float yMargin = (float)(height-getFont().getSize())/2; 
    g2d.drawString(getText(), xMargin, (float)getFont().getSize() + yMargin); 
    this.setSize(width, height); 
} 

public JPanel inTransparentPanel(){ 
    JPanel ret = new JPanel(); 
    ret.setOpaque(false); 
    ret.setDoubleBuffered(true); 
    ret.add(this); 
    return ret; 
} 

もう一つ:私もpaintComponent(...)を上書きせずにこれを試してみました - 方法。無効。

+1

'protected void paintComponent(Graphics g){ Graphics2D g2d =(Graphics2D)g; .. 'は' protected void paintComponent(Graphics g){sper.pintComponent(g);}にする必要があります。 Graphics2D g2d =(Graphics2D)g; .. [MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿するとすぐに役立ちます。 –

+1

1) 'this.setSize(width、height);'ペイントメソッド内で、再描画をトリガする可能性のある何もしないでください。無限ループが発生します。 2) 'ret.setDoubleBuffered(true);' 'ret'はデフォルトでダブルバッファされた' JPanel'です。 –

+0

ご協力ありがとうございます。 - スーパーはまったく私が欲しいものではありません - setSizeは完全に間違っています。ありがとうございます。 これは問題ではないようです。私はそれを以下に述べるように考え出した。 – blackwodka86

答えて

1

[OK]をクリックするだけでこの問題が「偶然」修正されました。パネルの1つがまだJava awt Paneであることがわかりました。決してそれを問題とは考えませんでした。

+0

はい、ミックスライト(Swing)と重い(AWT)の重量成分は歴史的に問題でした。オラクルはそれを修正したと思われますが、現在は2つを混合することを推奨しているSwing愛好家は誰も知らないです。 –

+0

ああ、言及するのをほとんど忘れてしまった。その問題は、上記の情報源から私たちが知ることができるものではなく、すぐに[mcve]と識別されるでしょう。もちろん、あなたはおそらくMCVEを作る過程でこの問題を発見したでしょう;) –

関連する問題