簡體   English   中英

如何使用 JRadioButton 更改 JPanel 的背景顏色

[英]How do I change the background color of a JPanel using JRadioButton

我想要做的是我正在嘗試使用 JRadioButtons 更改 JPanel 的背景顏色,但是當我單擊 JRadioButton 時,JPanel 的背景顏色不會改變

這是我寫的代碼。 ActionListener 有什么問題,它似乎在這里不起作用

我在這里使用了 JRadioButtons 並創建了其中的 3 個紅色、藍色、綠色。 我將它們放在一個 ButtonGroup 中。 在此之后,我添加了一個與之關聯的 actionListener。 然后我創建了一個 JPanel 並為其添加了邊框。 它的名字是顏色框。 然后在 class changeListener 中我創建了一個 if 和 else 語句,說明如果 e.getSource 或 red(Button).isSelected() 那么 colorBox 面板的背景將設置為紅色,對於 rest 也是如此按鈕我做了同樣的事情,但是在我單擊 JRadioButton 后該面板的顏色沒有改變。 這里有什么問題? 我該如何糾正這個

package aui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import java.awt.event.*;

public class E_3_2  extends JFrame 
{   private static int FRAME_WIDTH = 500, FRAME_HEIGHT = 400;
    private JRadioButton red,blue,green;
    private ButtonGroup group;
    private JPanel panel, colorBox;
    private ActionListener listener;
    public static void main(String[] args)
    {
        JFrame frame = new E_3_2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Change the background Color of a Panel");
        frame.setVisible(true);
    }
    
    public E_3_2()
    {
        red = new JRadioButton("Red");
        red.addActionListener(listener);
        blue = new JRadioButton("Blue");
        red.addActionListener(listener);
        green = new JRadioButton("Green");
        green.addActionListener(listener);
        
    
        group = new ButtonGroup();
        group.add(red);
        group.add(blue);
        group.add(green);
        
        setLayout(new BorderLayout());
        
        panel = new JPanel();
        panel.add(blue);
        panel.add(green);
        panel.add(red);
        add(panel, BorderLayout.WEST);
//      
        
        colorBox = new JPanel();
        colorBox.setBorder(new TitledBorder(new EtchedBorder(), "Color Box"));
        colorBox.setSize(150, 150);
        add(colorBox);
        
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }
    
    class ChangeListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
            String clickedButton = e.getActionCommand();
            System.out.println(clickedButton + " clicked");
            if(e.getSource() == red || red.isSelected())
            {
                colorBox.setBackground(Color.red);
            }
            else if(e.getSource() == blue || blue.isSelected())
            {
                colorBox.setBackground(Color.blue);
            }
            else if (e.getSource() == green || green.isSelected())
            {
                colorBox.setBackground(Color.green);
            }
        }
    }
}

您的listener器 object 被聲明為ActionListener 為了使用您自己的偵聽器,您需要使用使用重寫方法的ChangeListener

為了提高效率,您可以嘗試使用匿名內部 class 只是為了讓自己更輕松。 例如:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event)
    {
    
    }
});

你從來沒有為private ActionListener listener分配任何東西,為了解決這個問題,添加行listener = new ChangeListener(); 給你的構造函數。

此外,藍色按鈕不起作用,因為您從未給它一個監聽器。

您的構造函數現在看起來像這樣:

public E_3_2()
    {
        listener = new ChangeListener();

        red = new JRadioButton("Red");
        red.addActionListener(listener);
        blue = new JRadioButton("Blue");
        blue.addActionListener(listener);
        green = new JRadioButton("Green");
        green.addActionListener(listener);

        ...
}

暫無
暫無

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

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