gpt4 book ai didi

java - 重叠两个 JButtons : Issue

转载 作者:行者123 更新时间:2023-11-30 07:20:23 24 4
gpt4 key购买 nike

听起来有点奇怪,但我正在尝试重叠两个JButtons(此处为jButton1jButton2)。我使用的是 NetBeans GUI 生成器为此。没有什么特别的,我正在使用 JLayeredPane 来实现它。

 jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

好吧,它确实重叠了,但是 JButton 显示了它自己,当被鼠标点击时。屏幕截图将清楚地说明:

enter image description here

(以高亮部分为鼠标指针)

当鼠标指针在下一个 JButton 上单击时,它会出现,隐藏第一个 JButton。 enter image description here

但是,我不希望第二个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)可能会混淆,点击了哪个按钮,但对我来说没问题。

最佳答案

What does layout manager have to do with it?

@Patricia Shanahan 指出了重叠按钮的问题。一种解决方案是让 layout做工作。在下面的例子中,butonPanel 默认有一个居中的 FlowLayout。它采用任何外观中按钮的首选大小。

附录:啊,你想要按钮重叠但保持稳定的z顺序。因为渲染是由按钮的 UI 委托(delegate)(ButtonUI 的子类)控制的,所以效果取决于外观和感觉。例如,com.sun.java.swing.plaf.nimbus.NimbusLookAndFeelcom.apple.laf.AquaLookAndFeel 的行为都符合您的要求,而许多其他行为则不然。没有创建自己的委托(delegate),如图所示 here ,您可能会尝试采用显示的方法 here ,它隐藏按钮并使用叠加图像。根据封闭容器的布局,FlowLayoutGridLayout(1, 0) 应该可以工作。

顺便说一句,不要让 GUI 设计人员决定您的 GUI 设计!

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/14075990/230513 */
public class LayoutTest {

private void display() {
JFrame f = new JFrame("LayoutTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JPanel(){

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150); // content placeholder
}
}, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(); // default: FlowLayout, centered
buttonPanel.add(new JButton("Button1"));
buttonPanel.add(new JButton("Button2"));
f.add(buttonPanel, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new LayoutTest().display();
}
});
}
}

关于java - 重叠两个 JButtons : Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com