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

新手请教如何动态改变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条回答 默认 最新

  • sinat_36304417 2016-10-04 07:04
    关注

    额,懂了,在JFrame上设置JPanel为ContentPane;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料