按钮重叠或消失

Buttons overlap or disappear

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

如果我 点击Button 2,它会被隐藏,Button 4会显示出来。 如果我点击按钮 4,就会显示按钮 2,而按钮 4 将再次被按钮 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

这是一个 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();
        }
        
    }

}

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

这是您的代码。我只添加了两行,它们由注释 // 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。我在上面的代码中也这样做了。