[英]JButtons Overlap Components When Hovered On
我已經建立了一個JPanel,它位於JButton的頂部(Z軸)。 懸停在此JPanel上時,如果還懸停了JButton,則JButton會自動在所有組件之上重新繪制。 這對於我的程序正常運行是不理想的。 關於為什么發生這種情況以及如何解決此問題的任何想法? 感謝您提供的任何幫助!
這是我的代碼的快速簡單的副本:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 102));
panel.setBounds(0, 0, 169, 261);
contentPane.add(panel);
panel.setVisible(false);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(!panel.isVisible());
}
});
btnNewButton.setBounds(68, 70, 130, 70);
contentPane.add(btnNewButton);
JPanel從頂部(Z軸)開始,直到將JButton懸停在上面(即使JPanel覆蓋了JButton)。 我希望這些信息足以滿足您的要求。
在Swing UI中,幾乎總是使用布局管理器。 看到此內容以了解如何使用布局管理器: https : //docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
因此,在您的代碼中刪除以下行:
contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);
並執行類似的操作:
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.