[英]Trying to Change the Background of a JFrame from another class
Im trying to get this program running for my Spanish teacher, and I want to change the background of the first JFrame. 我正在尝试让该程序为我的西班牙语老师运行,并且我想更改第一个JFrame的背景。 Here it is.
这里是。
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);
}
});
}
}
Here is the class I want to change it from, in the Action Listener for the button at the bottom. 这是我要更改其类的类,位于底部按钮的操作侦听器中。
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
};
}
});
}
}
Thanks for any and all help! 感谢您提供的所有帮助!
Sorry, I misread your question. 抱歉,我看不懂你的问题。 All you have to do is call
getContentPane()
from within your ActionListener 您要做的就是从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);// !!
I found a way to do what you're asking. 我找到了一种方法来解决您的要求。 I added the following class variable in
switcherContent
: 我在
switcherContent
添加了以下类变量:
Container contentPane;
In the constructor, I added the following line: 在构造函数中,我添加了以下行:
contentPane = this.getContentPane();
Then in your action listener, I added the following line: 然后在您的动作侦听器中,添加了以下行:
contentPane.setBackground(Color.red);
simply add this.getContentPane().setBackground(Color.GREEN) inside button action listener; 只需在按钮动作监听器中添加this.getContentPane()。setBackground(Color.GREEN)即可;
'this' refers to your Jframe:switcherContent itself inside its class. “ this”在类中引用您的Jframe:switcherContent本身。 You can call this method to change background color from any place inside your switcherContent class.
您可以调用此方法以从switcherContent类内的任何位置更改背景颜色。 Not only in the actionListener() method.
不仅在actionListener()方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.