2014-10-16 74 views
0

我试图在右边框中使JComponent不透明。使JComponent不透明

我希望做一个对象与我的具体特点,所以我正在使用JComponent,可以是不透明的

这是因为我会让一个图书馆,我不希望使用JPanelJLabel

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.image.BufferedImage; 
import java.util.ArrayList; 

public class ProbadorCodigos { 

    JFrame Frame=new JFrame(); 
    JComponent BORDEDE=new JComponent() {private static final long serialVersionUID = 2222L;}; 

    public ProbadorCodigos() { 

     Frame.setSize(500, 500); 
     Frame.setResizable(false); 
     Frame.setUndecorated(false); 
     Frame.setLayout(null); 
     Frame.setLocationRelativeTo(null); 
     Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Frame.getContentPane().setBackground(Color.darkGray); 
     Format(); 
     Action(); 
    } 


    private void Format() { 

     BORDEDE.setBounds(Frame.getWidth()-100, 0, 100, Frame.getHeight()); 
     BORDEDE.setOpaque(true); 
     BORDEDE.setVisible(true); 
     BORDEDE.setEnabled(true); 
     BORDEDE.setFocusable(true); 
     BORDEDE.setBackground(Color.red); 
     System.out.println(BORDEDE); 
    } 
    private void Action() { 


     Frame.add(BORDEDE); 
    }  

    public static void main(String[] args) { 


     ProbadorCodigos Ventana=new ProbadorCodigos(); 
     Ventana.Frame.setVisible(true); 
    } 

} 

noo

我不知道为什么它不显示不透明的,如果我用一个JLabel的作品,所以我错过了什么?

感谢您的建议和答案

+0

'“,这不是一个好主意使用大量的进口,所以我不想使用JPanel或JLabel”' - 嗯?请解释一下,因为它确实没有意义?另外,你的组件如何行为不端。你如何证明它不是不透明的? – 2014-10-16 16:58:12

+0

mmm我不想使用JPanel或JLabel,因为他们有不需要的方法 – WearFox 2014-10-16 17:00:50

+0

这并不能说明你不想使用JPanel的动机,而JPanel本质上是不透明的。另外,请在我上面的评论中提及我的其他问题。你怎么知道你的JComponent其实不是不透明的?你在它的'paintComponent(...)'方法中填充图像还是背景颜色? – 2014-10-16 17:02:08

回答

2

我一般轻松解决你的问题的建议:使用一个JPanel。除非你有充分的理由不使用这个作为你班级的基础,否则它仍然是你的问题的最佳解决方案。否则,你需要一些代码,如:

JComponent bordede = new JComponent() { 
    private static final long serialVersionUID = 2222L; 

    @Override 
    protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    int width = getWidth(); 
    int height = getHeight(); 
    g.setColor(getBackground()); 
    g.fillRect(0, 0, width, height); 
    } 
}; 

如果你只是简单地使用JPanel,那么再次没有必要。

的其他问题与您的代码:

  • 您的代码不符合Java的命名约定等会混淆其他Java程序员。
  • 您正在使用空布局和setBounds(...),这将导致创建硬性增强和调试GUI。你应该避免使用这种类型的布局。
+0

是它的作品现在..但我认为我的reazon是好的“我想让自己的JComponet我不想使用像JPanel或JLabel这样的其他人,这些扩展了JComponent” – WearFox 2014-10-16 17:24:37