sinat_36304417
2016-10-04 06:53
采纳率: 57.1%
浏览 4.0k
已采纳

新手请教如何动态改变java中JPanel的背景颜色啊

代码如下:

 package extends1;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Main extends JFrame implements ItemListener{
    private ButtonGroup bg=new ButtonGroup();
    private JRadioButton rbColorr=new JRadioButton("red");
    private JRadioButton rbColorg=new JRadioButton("green");
    private JRadioButton rbColorb=new JRadioButton("blue");
    private JPanel jpl=new JPanel();
    public Main()
    {
        bg.add(rbColorr);
        bg.add(rbColorg);
        bg.add(rbColorb);
        jpl.add(rbColorr);
        jpl.add(rbColorg);
        jpl.add(rbColorb);
        rbColorr.addItemListener(this);
        rbColorg.addItemListener(this);
        rbColorb.addItemListener(this);
        this.add(jpl);
        this.setSize(300,300);
        this.setVisible(true);
    }
    public void itemStateChanged(ItemEvent e)
    {
        Object color=e.getItemSelectable();
        if(color==rbColorr)
            this.getContentPane().setBackground(Color.red);
        else if(color==rbColorg)
            this.getContentPane().setBackground(Color.green);
        else
            this.getContentPane().setBackground(Color.blue);;
    }
    public static void main(String[] args)throws Exception
    {
        new Main();
    }
}



  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题