更改窗口背景颜色的JFrame按钮 [英] JFrame buttons that change background color of window

查看:189
本文介绍了更改窗口背景颜色的JFrame按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个带有按钮的程序,当您单击按钮时,请更改框架的背景颜色

I am trying to make a program with buttons, that when you click them, change the background color of the frame

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;                  
            } else if (event.getSource() == greenButton){
                color = Color.green;
            } else {
                color = Color.blue;
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(new JButton ("Red")); 
    panel.add(new JButton ("Green"));
    panel.add(new JButton ("Blue"));
    frame.add(panel);       


}

}

但是,当我单击按钮时,似乎什么也没发生,我认为这可能与由于某种原因而未激活监听器有关

Yet when I click the buttons, nothing seems to happen and I think it might have something to do with the listeners not being activated for reason

推荐答案

花点时间直观地查看您的设置...

Take a moment to visualise you setup...

您有一个JFrame.此窗口包含一个JRootPane,其中包含一个JLayerdPane,其中包含一个内容窗格".

You have a JFrame. This window has a JRootPane, which contains a JLayerdPane, which contains a the "content pane".

内容窗格通常是基本窗口的最顶层组件.

The content pane is generally the most top level component of a basic window.

为此,添加JPanel. JPanel默认情况下是不透明的.默认情况下,内容窗格使用BorderLayout,这意味着添加到默认位置的所有内容都将放置在CENTER位置,填充可用空间...

On to this, you add a JPanel. JPanel is opaque by default. By default, the content pane uses a BorderLayout, this means that anything added to the default position will be placed in the CENTER position, filling the available space...

这意味着,框架由JLayeredPane,内容窗格和JPanel覆盖. setBackground不会像其他一些方法一样委派给内容窗格,但是,对于您而言,这无济于事,因为您添加的JPanel现在涵盖了它...

This means, you frame is covered by a JLayeredPane, content pane AND your JPanel. setBackground does not delegate to the content pane like some of the other methods, but, in your case, it wouldn't help, as the JPanel you add is now covering it...

除了LadyRacheya的建议外,您还有两种选择.

In addition to LadyRacheya suggestions, you have two choices.

您可以使JPanel透明...

JPanel panel = new JPanel();
panel.setOpaque(false);

并更改内容窗格的背景颜色...

And change the background color of the content pane...

getContentPane().setBackground(color);

或者您可以简单地更改JPanel的背景颜色....

OR you can simply change the background color of the JPanel....

final JPanel panel = new JPanel();

//...

panel.setBackground(color);

这篇关于更改窗口背景颜色的JFrame按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆