JFrame按钮彼此靠近

时间:2014-07-23 10:55:34

标签: java swing layout jframe jbutton

我正在使用JFrame创建虚拟钢琴,使用按钮来表示按键。

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
getContentPane().setLayout(layout); 
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(94, 94, 94).addComponent(
jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, 
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)

这就是我的想法

virtual piano

我希望按钮彼此靠近而没有间隙。是否有可能删除每个按钮之间的空格?

1 个答案:

答案 0 :(得分:0)

使用分层窗格,它支持组件的重叠,但您可以操作窗格以使组件彼此非常接近而不会重叠。

JLayeredPane overlap = new JLayeredPane();   
getContentPane().add(overlap);
// then you overlap.add(ITEM YOU WANT, new Integer(1));
//integers determine how you want what to overlap in an ordered fashion