听起来有点奇怪,但我试图重叠两个JButtons
(这里jButton1
和jButton2
)。我正在为此使用 NetBeans GUI 构建器。没什么特别的,我正在使用 JLayeredPane 使其成为可能。
jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
好吧,它确实重叠,但是JButton
当用鼠标单击时,它会显示它自己。一张截图就清楚了:
(将高亮部分作为鼠标指针)
和
当鼠标指针单击 nextJButton
时,它会出现,隐藏第一个 JButton。
但是,我不希望在鼠标单击时JButton
弹出第二个(与第一个重叠)。JButton
我该怎么做??好吧,我认为这可能很关键。
这是代码,如果需要:
public class NewJFrame1 extends javax.swing.JFrame {
public NewJFrame1() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLayeredPane1 = new javax.swing.JLayeredPane();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLayeredPane2 = new javax.swing.JLayeredPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(51, 51, 51));
jButton1.setText("jButton1");
jButton1.setBounds(0, 40, 73, 23);
jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton2.setText("jButton2");
jButton2.setBounds(40, 40, 73, 23);
jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(139, 139, 139)
.addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(161, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JLayeredPane jLayeredPane2;
private javax.swing.JPanel jPanel1;
}
更新 :
如果我不清楚,我又来了。
1)我不想制作单独的按钮,它们应该重叠。
2)重叠区域可能不同,按钮的大小。
3)第二个 JButton 不应该高于第一个 JButton,当点击时。(解决方案,但不是如何布局按钮。)
4)可能会有混淆,点击了哪个按钮,但对我来说没问题。