想要改变JFrame的背景,为啥不能改变

importjava.awt.Color;importjava.awt.event.ActionEvent;importjava.awt.event.ActionList... import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class numgame extends JFrame implements ActionListener {

JFrame jfrm=new JFrame();
JLabel label=new JLabel("input a num(0~9):");
JTextField input=new JTextField(6);
JButton button =new JButton("重置");
JTextArea display=new JTextArea(10,20);
public numgame(){
JPanel inputPanel=new JPanel();
inputPanel.add(label);
inputPanel.add(input);
getContentPane().add(inputPanel,"North");
getContentPane().add(button,"South");
getContentPane().add(display,"Center");
display.setLineWrap(true);
display.setEditable(false);
button.addActionListener(this);
input.addActionListener(this);
jfrm.setBackground(Color.RED);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {

double rannum,N=10;//随机数
int num;
rannum=N*Math.random();
rannum=(int)rannum;
if(e.getSource()==input)
{
num=Integer.parseInt(input.getText());
System.out.println(rannum);
if(num>rannum)
{

display.append("Too Large!\n");
jfrm.getContentPane().setBackground(Color.RED);
input.setText("");
System.out.println("here2");
}
if(num==rannum)
{
display.append("Right,Good!\n");
jfrm.getContentPane().setBackground(Color.WHITE);
input.setText("");
}
if(num<rannum)
{
display.append("Too Small!\n");
jfrm.getContentPane().setBackground(Color.BLUE);
input.setText("");
}

}
else
{

//input.setText("");
//display.append("");
new numgame();
System.out.println("here");
}

}
public static void main(String args[]){
new numgame();
}

}
展开
 我来答
喜只来屋梨仗1a
2011-06-03 · TA获得超过173个赞
知道小有建树答主
回答量:83
采纳率:0%
帮助的人:98.5万
展开全部
import java.awt.Color;
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.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//类名要规范,应改为:NumGame
public class numgame extends JFrame implements ActionListener {

// JFrame jfrm=new JFrame(); 注释掉
JLabel label=null;
JTextField input=null;
JButton button =null;
JTextArea display=null;
public numgame(){
init();
}
public void init(){
label=new JLabel("input a num(0~9):");
input=new JTextField(6);
button =new JButton("重置");
display=new JTextArea(10,20);

JPanel inputPanel=new JPanel();
inputPanel.add(label);
inputPanel.add(input);
getContentPane().add(inputPanel,"North");
getContentPane().add(button,"South");
getContentPane().add(display,"Center");
//...这是添加上的三行...../
inputPanel.setOpaque(false);
button.setOpaque(false);
display.setOpaque(false);
//......../
display.setLineWrap(true);
display.setEditable(false);
button.addActionListener(this);
input.addActionListener(this);
// jfrm.setBackground(Color.RED);改为:
getContentPane().setBackground(Color.RED);
setSize(400,300);
setVisible(true);
}
public void reset(){
input.setText("");
display.setText("");
getContentPane().setBackground(Color.RED);
}
private static numgame thisObj = null;
public static numgame getInstance(){
if(thisObj==null){
thisObj = new numgame();
}
return thisObj;
}
public void actionPerformed(ActionEvent e) {

double rannum,N=10;//随机数
int num;
rannum=N*Math.random();
rannum=(int)rannum;
if(e.getSource()==input)
{
num=Integer.parseInt(input.getText());
System.out.println(rannum);
if(num>rannum)
{

display.append("Too Large!\n");
//你看到的所有当前组件都是加在当前窗体上的,要这个jframe何用?删掉得了
//给这个看不到的窗体改变背景颜色有意义吗?木有啊!
// jfrm.getContentPane().setBackground(Color.RED);改为:
getContentPane().setBackground(Color.RED);
input.setText("");
System.out.println("here2");
}
if(num==rannum)
{
display.append("Right,Good!\n");
// jfrm.getContentPane().setBackground(Color.WHITE);改为:
getContentPane().setBackground(Color.WHITE);
input.setText("");
}
if(num<rannum)
{
display.append("Too Small!\n");
// jfrm.getContentPane().setBackground(Color.BLUE);改为:
getContentPane().setBackground(Color.BLUE);
input.setText("");
}

}
else
{

//input.setText("");
//display.append("");
// new numgame();// 这样是不对的,一来,不该产生新的实例,浪费空间,而且达不到目的
this.reset();
System.out.println("here");
}

}
public static void main(String args[]){
getInstance();
}
}
gygwoaini
2011-05-31 · TA获得超过2687个赞
知道小有建树答主
回答量:2001
采纳率:0%
帮助的人:748万
展开全部
先把if(e.getSource()==input)
改成if(e.getSource()==botton)
===============================================
jfrm.setBackground(Color.RED);
jfrm.getContentPane().setBackground(Color.BLUE);
都改成
inputPanel.setBackground(Color.XXX);
===============================================
JPanel inputPanel;//声明放在构造方法外面,
inputPanel = new JPanel();//实例化放在构造方法里面
===============================================
如果要JTextArea的背景色,一样可以设置
display.setBackground(Color.RED);
追问
你把if(e.getSource()==input)	
改成if(e.getSource()==botton)
这个不对的,我要监听的是输入的值,对输入的值进行处理,对于button的监听是在else里进行的,改了就无法进行了。你这样修改时一个面板一个面板的进行修改,有没有整体的方法进行修改颜色啊?直接对这个JFame修改?
追答
再怎么修改外部,容器内部的东西也会覆盖外部的颜色。
另外 你说要监听输入的值是什么意思呢?每输入一个字母就执行监听方法?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式