簡體   English   中英

兩個按鈕將面板顏色更改為紅色或藍色

[英]Two buttons changing panel color to red or blue

我的第一篇文章在這里。 我目前在學校,通常在這里花時間在Stackoverflow上尋找作業的答案,這一次我本以為我可能會將代碼放在這里,也許我會更快,更准確地獲得幫助! 無論如何,我的問題是我寫了一個代碼,您可以在下面看到,我是新手,只研究了幾個小時。 我的問題是我不太確定如何解決此問題,我有2個按鈕,我要的是單擊第一個按鈕時面板將變為紅色,第二個按鈕時面板將變為藍色,僅紅色有效,我不知道如何實現它,所以藍色也有效。

非常感謝您的幫助! (不要害羞地指出一些錯誤或沒有按鈕需要的幫助,就像我說的,我是新來的:P)

public class FirstProgram extends JFrame {

    public FirstProgram() {

        initUI();
    }

    private void initUI() {

        JPanel panel = new JPanel();
        panel.setBackground(Color.yellow);
        getContentPane().add(panel);
        panel.setLayout(null);

        JButton Colorbutton = new JButton("Red");
        Colorbutton.setBounds(50, 60, 80, 30);
        Colorbutton.setToolTipText("Panel changes to red");
        Colorbutton.setBackground(Color.green);

        JButton Colorrbutton = new JButton("Blue");
        Colorrbutton.setBounds(1, 30, 90, 30);
        Colorrbutton.setToolTipText("Panel changes to blue");
        Colorrbutton.setBackground(Color.orange);

        Colorbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                panel.setBackground(Color.red);

            }
        });

        panel.add(Colorbutton);
        panel.add(Colorrbutton);
        setTitle("Time to change colors");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FirstProgram ex = new FirstProgram();
                ex.setVisible(true);
            }
        });
    }
}

您需要另一個ActionListener。 現在,您只有一種,而且只有一種行為。 創建另一個並綁定到“藍色”按鈕

JButton RedColorbutton = new JButton("Red");
 RedColorbutton .setBounds(50, 60, 80, 30);
 RedColorbutton.setToolTipText("Panel changes to red");
 RedColorbutton.setBackground(Color.green);

 JButton BlueColorbutton = new JButton("Blue");
 BlueColorrbutton.setBounds(1,30,90,30);
 BlueColorrbutton.setToolTipText("Panel changes to blue");
 BlueColorrbutton.setBackground(Color.orange);

 RedColorbutton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent event) {
 panel.setBackground(Color.red);
 }
 });

BlueColorbutton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent event) {
 panel.setBackground(Color.blue);
 }
 });

您已經為Colorbutton設置了一個動作監聽Colorbutton ,但沒有為Colorrbutton設置一個動作監聽Colorrbutton

將此添加到其他ActionListener旁邊

Colorrbutton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent event)
    {
        panel.setBackground(Color.blue);
    }
});

您缺少藍色JButtton的ActionListener。

類似於將ActionListener添加到ColorButton的方法,ColorrButton需要注冊一個。 順便說一句,您可能希望將ColorButton更改為redButton,將ColorrButton更改為blueButton或類似的東西,以使效果更好。

例:

    Colorrbutton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            panel.setBackground(Color.blue);
        }
    });

為了減少重復的代碼,可以通過讓類實現ActionListener來進一步簡化邏輯。

public class FirstProgram extends JFrame implements ActionListener {

然后,在實例化按鈕時,添加一個監聽器,如下所示:

redButton.addActionListener(this);
blueButton.addActionListener(this);

然后在actionPerformed的實現中,您可以執行以下操作:

public void actionPerformed(ActionEvent e) {
    switch (e.getSource()) {
        case redButton:
            panel.setBackground(Color.red);
            break;
        case blueButton:
            panel.setBackground(Color.blue);
            break;
    }
}

每當紅色或藍色按鈕執行操作時,都會觸發actionPerformed,然后邏輯確定源將從那里接管哪個按鈕。 這將增加代碼的長度,但是隨着程序的增長,它將大大降低復雜性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM