【问题标题】:Weird black borders around JComponents in Swing JavaSwing Java中JComponents周围奇怪的黑色边框
【发布时间】:2015-05-16 22:21:16
【问题描述】:

我创建了一个带有可移动组件的 UI。当我创建一个它看起来像最上面的图片。一旦我开始移动它,按钮周围的空间就会变成黑色,如中间所示。当我进入另一个窗口(例如 safari)时,单选框周围的空间也会变黑。任何想法为什么?

编辑:

3button 组合是一个扩展的 JPalet 类,它嵌入到框架中的另一个面板 JPanel1 中。一旦我在 JPanel1 中重新验证并重新绘制,该错误就会再次消失。或者当我将一个 3button 放在另一个上时(这可能也会触发重绘)。

我移动 3Button 并使用 MouseAdapter MouseDrag 将 mousemotionlistener 粘在 JButton 上

编辑:

希望这段代码 sn-p 足够小。

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);
        }
    }
}

【问题讨论】:

  • 你自己画的吗?你是如何移动它的?如果可能,请使用minimal, complete example 来重现该行为。
  • 有一个最小的工作示例,请参阅编辑

标签: java swing movable


【解决方案1】:
setBackground(new java.awt.Color(0, 0, 0, 0f));

那么问题是您使用的是透明颜色,所以背景没有正确绘制。

您可以使用:

setOpaque( false );

有关透明度为何会导致问题的更多信息,请查看Background With Transparency

另外,不要使用空布局。您可以使用简单的 BorderLayout。将单选按钮添加到 BorderLayout.LINE_START 和 BorderLayout.LINE_END,并将按钮添加到 BorderLayout.CENTER。

当您使用布局管理器时,组件将确定组件的首选大小,以便可以在其他面板中使用。否则,您将需要覆盖组件的 getPreferredSize()getMinimumSize()getMaximumSize() 方法以返回正确的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2019-12-18
    • 2011-10-21
    相关资源
    最近更新 更多