按钮重叠或消失

wswtfjt7  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(295)

我有一个程序出错的例子。我创建了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 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);
    }
}

单列彩色按钮

yrdbyhpb

yrdbyhpb1#

您只是在 actionPerformed() 方法。你需要改变两者的可见性 JButton s、 不止一个。
这是你的密码。我只添加了两行,它们由注解指示 // 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` 错误的。我在上面的代码中也做过。
vwkv1x7d

vwkv1x7d2#

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

    }

}

相关问题