【问题标题】:Buttons overlap or disappear按钮重叠或消失
【发布时间】:2020-12-16 07:11:56
【问题描述】:

我的程序中有一个错误示例。我创建了 4 个按钮:1、2、3、4。按钮 2 与按钮 4 重叠,我只是为 2 和 4 添加了事件。

如果我 单击按钮 2,它将被隐藏,而按钮 4 将被显示。 如果我单击 Button 4,将显示 Button 2,而 Button 4 将再次被 Button 2 覆盖。好像发生了什么,但是,当 完成上述操作后,我单击按钮 1 或按钮 3,按钮 4 将 显示出来,当我指向它(不是点击)时,它会消失。

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);
    }
}

Single column of colored buttons

【问题讨论】:

  • container.setLayout(null); 先解决这个问题。 Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 感谢您的评论,我已经知道了,我只想创建一个固定大小的程序。如果我使用 FlowLayout 或 BorderLayout 或其他东西,我不知道如何创建 2 个按钮重叠。
  • 一般提示: 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度 - 以显示应如何使用额外空间。 3) 屏幕截图显示一列三个彩色按钮。对于大小相同的单列组件,我会使用GridLayout
  • 我只想在同一位置显示2个按钮,当我点击按钮1时,按钮2会出现并且按钮1被隐藏
  • "当我点击按钮 1 时,按钮 2 会出现并且按钮 1 被隐藏" 要么使用 CardLayout,如 this answer 所示,要么只有一个单击单个按钮并更改其状态(文本和颜色等)。另见What is the XY problem?

标签: java swing jbutton layout-manager


【解决方案1】:

这是一个 GUI,其中按钮 2 变为按钮 4。

我是如何做到这一点的?简单的。我只用了三个JButtons

我更改了第二个按钮的文本和第二个按钮的背景颜色,就像 Andrew Thompson 建议的那样。

这是完整的可运行代码。你知道吗,这是一个最小的可运行示例!

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();
        }
        
    }

}

【讨论】:

  • 谢谢兄弟,但下面的评论解决了我想要的问题
【解决方案2】:

您只是在 actionPerformed() 方法中有一个错误。您需要更改两个 JButtons 的可见性,而不仅仅是一个。

这是您的代码。我只添加了两行,它们由注释 // ADDED THIS LINE 指示

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);
        btn4.setVisible(false);  // Initially we only want to see 'btn2'.

        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.setVisible(true);  // ADDED THIS LINE
            }
        });
        btn4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btn2.setVisible(true);
                btn4.setVisible(false);  // ADDED THIS LINE
            }
        });
        this.setSize(400, 500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new UI("UI"));
    }
}

请注意,虽然代码没有它也可以工作,但我认为您应该将btn4 的初始可见性设置为false。我在上面的代码中也这样做了。

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 2019-08-20
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多