简体   繁体   中英

Weird black borders around JComponents in Swing Java

I created a UI with movable components. As I create one it looks like the top most picture. As soon as I start moving it, the space around the button becomes black as seen in the middle. When I go into another window (Eg safari) the space around the radio boxes also turn black. Any ideas why?

在此处输入图片说明

在此处输入图片说明

EDIT:

The 3button combo is an extended JPalet class that gets embedded in another panel JPanel1 that in a frame. As soon as I revalidate and repaint in JPanel1, the bug disappears again. Or when I whipe one 3button over the other (which probably also triggers repaint).

I move the 3Button around with a mousemotionlistener stuck to the JButton with a MouseAdapter MouseDrag

EDIT:

Hope this code snippet is small enough.

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class UiTestFrame {

    public UiTestFrame() {
        buildUi();
    }

    void buildUi() {
        JFrame frame = new JFrame("TestFrame");

        XButton jButton = new XButton();
        frame.add(jButton);
        jButton.setSize(jButton.getPreferredSize());
        jButton.setLocation(10, 10);
        frame.revalidate();
        frame.repaint();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    private class XButton extends javax.swing.JPanel {

        public XButton() {

            initComponents();
            jButton1.addMouseMotionListener(new MotionEvent(this));
        }

        private void initComponents() {

            jButton1 = new javax.swing.JButton();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();

            setBackground(new java.awt.Color(0, 0, 0, 0f));
            setPreferredSize(new java.awt.Dimension(130, 30));
            setLayout(null);

            jButton1.setText("jButton1");
            jButton1.setAlignmentY(0.0F);
            jButton1.setContentAreaFilled(false);
            jButton1.setFocusPainted(false);
            jButton1.setFocusable(false);
            jButton1.setMargin(new java.awt.Insets(-2, -2, -2, -2));
            add(jButton1);
            jButton1.setBounds(19, 0, 90, 30);

            jRadioButton1.setEnabled(false);
            jRadioButton1.setFocusable(false);
            add(jRadioButton1);
            jRadioButton1.setBounds(0, 0, 20, 30);

            jRadioButton2.setEnabled(false);
            jRadioButton2.setFocusable(false);
            jRadioButton2.setName(""); // NOI18N
            add(jRadioButton2);
            jRadioButton2.setBounds(100, 0, 30, 30);
        }

        private javax.swing.JButton jButton1;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;

    }

    private class MotionEvent extends MouseAdapter {

        javax.swing.JComponent button;
        Point pt;

        public MotionEvent(javax.swing.JComponent button) {
            this.button = button;
        }

        public void mouseMoved(MouseEvent e) {
            pt = e.getPoint();
        }

        public void mouseDragged(MouseEvent e) {
            Point p = SwingUtilities.convertPoint(button, e.getPoint(), button.getParent());
            button.setLocation(p.x - pt.x, p.y - pt.y);
        }
    }
}
setBackground(new java.awt.Color(0, 0, 0, 0f));

Then problem is you are using a transparent color, so the background isn't being painted properly.

Instead you can use:

setOpaque( false );

For more information on why transparency can cause problems check out Background With Transparency .

Also, don't use a null layout. You can use a simple BorderLayout. Add the radio buttons to the BorderLayout.LINE_START and BorderLayout.LINE_END and the button to the BorderLayout.CENTER.

When you use layout managers the component will determine the components preferred size so it can be used in other panels. Otherwise you will need to override the getPreferredSize() and getMinimumSize() and getMaximumSize() methods of your component to return the proper size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM