Reputation: 293
I'm trying to make a JComponent
opaque in the right border.
I want make a object with my specific characteristics so i'm using a JComponent
that can be opaque
this is because I will make a library, and I don't want to use JPanel
or JLabel
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);
}
}
I don't know why it don't shows opaque, if I use a JLabel
works so what I missing?
thanks for your advices and answers
Upvotes: 0
Views: 393
Reputation: 285405
My suggestion for general ease of solving your problem: use a JPanel. Until you show good reason for not using this as the basis for your class, it remains in my mind the best solution for your problem. Otherwise, you'll need some code like:
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);
}
};
Which again is not necessary if you simply used a JPanel.
Other problems with your code:
setBounds(...)
which will result in the creation of a rigid hard to enhance and debug GUI. You should avoid using this type of layout.Upvotes: 2