我使用可移動組件創建了一個UI。當我創建一個它看起來像最上面的圖片。只要我開始移動它,按鈕周圍的空間會變成黑色,如中間所見。當我進入另一個窗口(例如safari)時,收音機箱周圍的空間也會變黑。任何想法爲什麼?Swing Java中JComponent的怪異黑色邊框
編輯:
的3button組合是獲取嵌入另一面板JPANEL1擴展JPalet類中的幀。只要我在JPanel1中重新驗證並重新繪製,該錯誤就會再次消失。或者當我鞭one一個3button在另一個(這可能也觸發重繪)。
我四處移動3Button與堅持將JButton有MouseAdapter MouseDrag
編輯的MouseMotionListener:
希望此代碼段是足夠小。
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](http://stackoverflow.com/help/mcve),它重現了這種行爲。 – Radiodef
有一個最小的工作示例,請參閱編輯 – Josephus87