Java 按钮重叠或消失
我有一个程序错误的例子。我创建了4个按钮:1、2、3、4。按钮2与按钮4重叠,我刚刚添加了2和4的事件 如果我 点击按钮2,它将被隐藏,按钮4将被显示。 如果我点击按钮4,就会显示按钮2,按钮4 将再次被按钮2覆盖。好像发生了什么,但是,什么时候 我点击按钮1或按钮3完成上述操作后,按钮4将 当我指向它(而不是单击)时,它将消失Java 按钮重叠或消失,java,swing,jbutton,layout-manager,Java,Swing,Jbutton,Layout Manager,我有一个程序错误的例子。我创建了4个按钮:1、2、3、4。按钮2与按钮4重叠,我刚刚添加了2和4的事件 如果我 点击按钮2,它将被隐藏,按钮4将被显示。 如果我点击按钮4,就会显示按钮2,按钮4 将再次被按钮2覆盖。好像发生了什么,但是,什么时候 我点击按钮1或按钮3完成上述操作后,按钮4将 当我指向它(而不是单击)时,它将消失 public class UI extends JFrame { public UI(String title) { Container co
public class UI extends JFrame {
public UI(String title) {
Container container = this.getContentPane();
container.setLayout(null);
JButton btn1 = new JButton("1");
btn1.setBounds(10, 10, 50, 50);
btn1.setBackground(Color.RED);
JButton btn2 = new JButton("2");
btn2.setBounds(10, 70, 50, 50);
btn2.setBackground(Color.GREEN);
JButton btn3 = new JButton("3");
btn3.setBounds(10, 130, 50, 50);
btn3.setBackground(Color.BLUE);
JButton btn4 = new JButton("4");
btn4.setBounds(10, 70, 50, 50);
btn4.setBackground(Color.YELLOW);
container.add(btn1);
container.add(btn2);
container.add(btn3);
container.add(btn4);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(false);
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn2.setVisible(true);
}
});
this.setSize(400, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
这是一个GUI,其中按钮2变为按钮4 我是怎么做到的?简单。我只使用了三个
JButtons
我改变了第二个按钮的文本和第二个按钮的背景色,就像安德鲁·汤普森建议的那样
下面是完整的可运行代码。你知道吗,这是一个最小的可运行示例
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JButtonExampleGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JButtonExampleGUI());
}
private Color buttonColor;
private JButton button2;
private String buttonText;
public JButtonExampleGUI() {
this.buttonColor = Color.GREEN;
this.buttonText = "2";
}
@Override
public void run() {
JFrame frame = new JFrame("JButton Example GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButtonPanel(),BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
panel.setPreferredSize(new Dimension(400, 500));
Font font = panel.getFont().deriveFont(60f);
JButton button1 = new JButton("1");
button1.setBackground(Color.RED);
button1.setForeground(Color.WHITE);
button1.setFont(font);
panel.add(button1);
button2 = new JButton(buttonText);
button2.addActionListener(new Button2Listener());
button2.setBackground(buttonColor);
button2.setFont(font);
panel.add(button2);
JButton button3 = new JButton("3");
button3.setBackground(Color.BLUE);
button3.setForeground(Color.WHITE);
button3.setFont(font);
panel.add(button3);
return panel;
}
public void updateButton2() {
button2.setText(buttonText);
button2.setBackground(buttonColor);
}
public class Button2Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
String text = button.getText();
if (text.contentEquals("2")) {
buttonText = "4";
buttonColor = Color.YELLOW;
} else {
buttonText = "2";
buttonColor = Color.GREEN;
}
updateButton2();
}
}
}
您只是在
actionPerformed()
方法中有一个错误。您需要更改两个按钮的可见性,而不仅仅是一个按钮
这是你的密码。我只添加了两行,它们由注释//添加了这一行
公共类UI扩展JFrame{
公共用户界面(字符串标题){
容器容器=this.getContentPane();
container.setLayout(null);
JButton btn1=新JButton(“1”);
btn1.立根(10,10,50,50);
btn1.立根背景(颜色:红色);
JButton btn2=新JButton(“2”);
btn2.立根(10,70,50,50);
btn2.立根背景(颜色为绿色);
JButton btn3=新JButton(“3”);
btn3.立根(10,130,50,50);
btn3.立根背景(颜色:蓝色);
JButton btn4=新JButton(“4”);
btn4.立根(10,70,50,50);
btn4.立根背景(颜色:黄色);
setVisible(false);//最初我们只想看到'btn2'。
容器。添加(btn1);
容器。添加(btn2);
容器。添加(btn3);
容器。添加(btn4);
btn2.addActionListener(新的ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
btn2.setVisible(假);
btn4.setVisible(true);//添加了此行
}
});
btn4.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
btn2.setVisible(真);
btn4.setVisible(false);//添加了此行
}
});
这个。设置大小(400500);
此.setDefaultCloseOperation(关闭时退出);
此.setLocationRelativeTo(空);
此.setVisible(true);
}
公共静态void main(字符串[]args){
invokeLater(()->新UI(“UI”);
}
}
请注意,尽管代码在没有它的情况下可以工作,但我认为您应该将btn4
的初始可见性设置为false。我在上面的代码中也这样做了。container.setLayout(null)代码>首先修复该问题。Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。请改用布局管理器,或者使用布局填充和边框。谢谢你的评论,我已经知道了,我只想创建一个固定大小的程序。如果我使用FlowLayout或BorderLayout或其他东西,我不知道如何创建两个按钮重叠。一般提示:1)为了更快地获得更好的帮助,请添加一个或。2) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度,以显示应如何使用额外的空间。3) 屏幕截图显示了一列三个彩色按钮。对于一列大小相同的组件,我会使用GridLayout
。我只想在同一位置显示两个按钮,当我单击按钮1时,按钮2将显示,按钮1隐藏“当我单击按钮1时,按钮2将显示,按钮1隐藏”或者使用如中所示的,或者只有一个按钮,单击时更改其状态(文本和颜色等)。另请参见“谢谢brooo”,但下面的评论解决了我的问题