简体   繁体   English

Swing Java中JComponents周围的黑色怪异边框

[英]Weird black borders around JComponents in Swing Java

I created a UI with movable components. 我创建了带有可移动组件的UI。 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. 3button组合是扩展的JPalet类,它嵌入在框架中的另一个面板JPanel1中。 As soon as I revalidate and repaint in JPanel1, the bug disappears again. 当我在JPanel1中重新验证并重新绘制时,该错误再次消失。 Or when I whipe one 3button over the other (which probably also triggers repaint). 或者,当我在另一个按钮上鞭打一个3按钮时(可能还会触发重新绘制)。

I move the 3Button around with a mousemotionlistener stuck to the JButton with a MouseAdapter MouseDrag 我将3Button移动,并使用mouseMouseDrag鼠标固定在JButton上的mousemotionlistener

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. 您可以使用简单的BorderLayout。 Add the radio buttons to the BorderLayout.LINE_START and BorderLayout.LINE_END and the button to the BorderLayout.CENTER. 将单选按钮添加到BorderLayout.LINE_START和BorderLayout.LINE_END,然后将按钮添加到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. 否则,您将需要重写组件的getPreferredSize()getMinimumSize()以及getMaximumSize()方法以返回正确的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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