java - 尝试从另一个类更改 JFrame 的背景

标签 java swing background

我正在尝试为我的西类牙语老师运行这个程序,并且我想更改第一个 JFrame 的背景。在这里。

package com.jaketherey;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Inglés {

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

        JFrame frame = new switcherContent();

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(950, 850);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Inglés");
        frame.getContentPane().setBackground(Color.GREEN);
        }

    });

}

}

这是我想要更改它的类,位于底部按钮的操作监听器中。

package com.jaketherey;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class switcherContent extends JFrame {

public switcherContent(){

    //Constrains

    GridBagConstraints constr = new GridBagConstraints();

    //Layout

    GridBagLayout gBL = new GridBagLayout();

    //Buttons

    final JButton buttonSwitch = new JButton("Switch");

    //JLabel

    final JLabel phrase1 = new JLabel("Inglés");
    phrase1.setHorizontalAlignment(SwingConstants.CENTER);
    phrase1.setForeground(Color.red);
    phrase1.setFont (phrase1.getFont ().deriveFont (150.0f));

    final JLabel phrase2 = new JLabel("Permitido");
    phrase2.setHorizontalAlignment(SwingConstants.CENTER);
    phrase2.setForeground(Color.red);
    phrase2.setFont (phrase2.getFont ().deriveFont (150.0f));

    //Set Layout//
    setLayout(gBL);

    //Constraints & Adding//

    constr.gridx=0;
    constr.gridy=1;
    constr.gridheight=1;
    constr.gridwidth=1;
    constr.weightx=1.0;
    constr.weighty=1.0;
    constr.weightx=1.0;
    constr.weighty=1.0;
    gBL.setConstraints(buttonSwitch, constr);
    add(buttonSwitch);

    constr.gridx=0;
    constr.gridy=0;
    constr.gridheight=1;
    constr.gridwidth=1;
    constr.weightx=1.0;
    constr.weighty=1.0;
    constr.weightx=1.0;
    constr.weighty=1.0;
    constr.fill=constr.BOTH;
    gBL.setConstraints(phrase1, constr);
    add(phrase1);

    constr.gridx=0;
    constr.gridy=2;
    constr.gridheight=1;
    constr.gridwidth=1;
    constr.weightx=1.0;
    constr.weighty=1.0;
    constr.weightx=1.0;
    constr.weighty=1.0;
    constr.fill=constr.BOTH;
    gBL.setConstraints(phrase2, constr);
    add(phrase2);

    buttonSwitch.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {
            String check = phrase2.getText();

            if(check.equals("Permitido")){
            phrase2.setText("Prohibido");

            phrase1.setForeground(Color.green);
            phrase2.setForeground(Color.green);

            //Set Background to RED

            }
            else if(check.equals("Prohibido")){
            phrase2.setText("Permitido");

            phrase1.setForeground(Color.red);
            phrase2.setForeground(Color.red);

            //Set Background to GREEN

            };

        }

    });

}

}

感谢您的帮助!

最佳答案

抱歉,我误解了你的问题。您所要做的就是从 ActionListener 中调用 getContentPane()

        public void actionPerformed(ActionEvent e) {
            String check = phrase2.getText();

            if (check.equals("Permitido")) {
                phrase2.setText("Prohibido");

                phrase1.setForeground(Color.green);
                phrase2.setForeground(Color.green);

                getContentPane().setBackground(Color.RED);// !!

关于java - 尝试从另一个类更改 JFrame 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341096/

相关文章:

java - 自动调整字体大小

java - Swing - 删除选定的边框并更改 JComboBox 的箭头颜色

Java如何创建.png文件?

android - TableLayout 中的背景图像和背景颜色

PHP 请求 "in background"

java - Spring(-Boot) Autowiring 在裸初始化项目中始终为空

java - 如何确保包含的子项目在 Gradle 中的主项目之前运行?

java - 在jar中查找静态文件

css - 我怎样才能删除我的 ucoz 网站的背景图片?

java - Instant Enabled App Bundle Not Downloading Full APK 而是显示错误