Buttons overlap or disappear

你。 提交于 2021-01-29 20:24:08

问题


I have an example of a error in my program. I created 4 buttons: 1, 2, 3, 4. Button 2 overlaps button 4, and i just added events for 2 and 4.

If I click on Button 2, it will be hidden, and Button 4 will be displayed. And if I click on Button 4, Button 2 will be displayed, and Button 4 will be covered by Button 2 again. As if anything happened, but, when I click on Button 1 or Button 3 after doing the above, Button 4 will be displayed, when I point at it (not click), it will disappear.

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


回答1:


You simply have an error in the actionPerformed() methods. You need to change the visibility of both JButtons, not just one.

Here is your code. I only added two lines and they are indicated by the comment // 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"));
    }
}

Note that although the code works without it, I think you should set the initial visibility of btn4 to false. I have also done this in the above code.




回答2:


Here's a GUI where button 2 changes into button 4.

How did I make this happen? Simple. I only used three JButtons.

I changed the text of the second button and the background color of the second button, just like Andrew Thompson suggested.

Here's the complete runnable code. What do you know, it's a minimal runnable example!

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

}


来源:https://stackoverflow.com/questions/65318746/buttons-overlap-or-disappear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!