class timecolor extends Thread { int x,y,z;
public timecolor(int x,int y,int z)
{
super();
this.x=x;
this.y=y;
this.z=z; }
public void run()
{
while(true)
{
x=x+30;
y=y+30;
z=z-23;
try
{
this.sleep(1000);
}
catch(Exception ex)
{
ex.printStackTrace();
}}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Testmain tm=new Testmain();
timecolor a2=new timecolor(x,y,z);
a2.start();
setBkColor(tm.getContentPane(),new Color(x,y,z));
tm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}public static void setBkColor(Container c,Color clr)
{
Component[] cArr = c.getComponents(); for (int i = 0; i < cArr.length; i ++)
{
cArr[i].setBackground(clr);
} c.setBackground(clr);
} 这3段是我的代码,我想要实现背景颜色的变化 ,可是现在就是变不了
public timecolor(int x,int y,int z)
{
super();
this.x=x;
this.y=y;
this.z=z; }
public void run()
{
while(true)
{
x=x+30;
y=y+30;
z=z-23;
try
{
this.sleep(1000);
}
catch(Exception ex)
{
ex.printStackTrace();
}}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Testmain tm=new Testmain();
timecolor a2=new timecolor(x,y,z);
a2.start();
setBkColor(tm.getContentPane(),new Color(x,y,z));
tm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}public static void setBkColor(Container c,Color clr)
{
Component[] cArr = c.getComponents(); for (int i = 0; i < cArr.length; i ++)
{
cArr[i].setBackground(clr);
} c.setBackground(clr);
} 这3段是我的代码,我想要实现背景颜色的变化 ,可是现在就是变不了
public static void main(String[] args) {
// TODO Auto-generated method stub
Testmain tm=new Testmain();
timecolor a2=new timecolor(x,y,z);
a2.start();
setBkColor(tm.getContentPane(),new Color(a2.x,a2.y,a2.z));
tm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
你把c.setBackground(new Color(a2.x,a2.y,a2.z));
替换掉 setBkColor(tm.getContentPane(),new Color(a2.x,a2.y,a2.z));
这行
import java.sql.*;
import java.text.SimpleDateFormat;
import java.awt.event.*;
import java.util.*;
public class Testmain extends JFrame implements ActionListener{ /**
* @param args
*/
JTable jt;
public static int x=0,y=100,z=255;
JLabel jb,firstjb,timejb;
JButton jb1,jb2,jb3,jb4,jb5;
JPanel jp1,jp2,jp3;
JTextField jtf;
JMenuBar jm;
public JMenu jmn1,jmn2,jmn3,jmn4;
JMenuItem jmit11,jmit12,jmit21,jmit31,jmit41;
Connection conn=null;
JScrollPane jsp=null;
stumodel sm;
ResultSet rs=null;
PreparedStatement pstmt=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Testmain tm=new Testmain();
timeLabel a2=new timeLabel(x,y,z);
a2.start();
setBkColor(tm.getContentPane(),new Color(a2.x,a2.y,a2.z));
tm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Testmain()
{
timecolor a2=new timecolor(x,y,z);
a2.start();
jm=new JMenuBar();
setJMenuBar(jm);
{
jmn1 = new JMenu();
jm.add(jmn1);
jmn1.setText("菜单选项");
{
jmit11 = new JMenuItem();
jmn1.add(jmit11);
jmit11.setText("进入学生管理系统");
jmit11.addActionListener(this);
}
{
jmit12 = new JMenuItem();
jmn1.add(jmit12);
jmit12.setText("退出系统");
jmit12.addActionListener(this);
}
jmn2 = new JMenu();
jm.add(jmn2);
jmn2.setText("添加操作");
{
jmit21 = new JMenuItem();
jmn2.add(jmit21);
jmit21.setText("添加信息");
jmit21.addActionListener(this);
}
jmn3 = new JMenu();
jm.add(jmn3);
jmn3.setText("修改操作");
{
jmit31 = new JMenuItem();
jmn3.add(jmit31);
jmit31.setText("修改信息");
jmit31.addActionListener(this);
}
jmn4 = new JMenu();
jm.add(jmn4);
jmn4.setText("删除操作");
{
jmit41 = new JMenuItem();
jmn4.add(jmit41);
jmit41.setText("删除信息");
jmit41.addActionListener(this);
}
}
jp1=new JPanel();
jtf=new JTextField(10);
jb1=new JButton("查询");
jb1.addActionListener(this);
jb5=new JButton("返回");
jb5.addActionListener(this);
jb=new JLabel("请输入名字");
jp1.add(jb);
jp1.add(jtf);
jp1.add(jb1);
jp1.add(jb5);
jp2=new JPanel();
jb2=new JButton("添加");
jb2.addActionListener(this);
jb3=new JButton("修改");
jb3.addActionListener(this);
jb4=new JButton("删除");
jb4.addActionListener(this);
timejb=new JLabel("asfaf");
jp2.add(timejb);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
//jp3=new JPanel();
//firstjb=new JLabel("欢迎进入学生管理系统");
//jp3.add(firstjb);
//jp3.setVisible(true);
sm=new stumodel();
jt=new JTable(sm);
jsp=new JScrollPane(jt);
//firstjb.setVisible(true);
jp2.setVisible(false);
jp1.setVisible(false);
jsp.setVisible(false);
jm.setVisible(true);
//firstjb.setBounds(60, 80, 100, 100);
this.add(jsp);
//this.add(jp3);
//jp3.setLayout(null);
this.add(jp1,"North");
this.add(jp2,"South");
this.setSize(500,300);
//UIManager.put( "Panel.background ",new Color(200,220,250));
//this.setBackground(setBkColor());
this.setLocation(400, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void setBkColor(Container c,Color clr)
{
Component[] cArr = c.getComponents(); for (int i = 0; i < cArr.length; i ++)
{
cArr[i].setBackground(clr);
} c.setBackground(clr);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jmit11)
{
timeLabel a1 = new timeLabel(timejb);
a1.start();
//firstjb.setVisible(false);
jp2.setVisible(true);
jp1.setVisible(true);
jsp.setVisible(true);
//jp3.setVisible(false);
//jm.setVisible(false);
}
if(e.getSource()==jmit12)
{
System.exit(1);
}
if(e.getSource()==jmit21)
{
new stuadd(this,"添加学生",true);
sm=new stumodel();
jt.setModel(sm);
}
if(e.getSource()==jmit31)
{
int rownum=this.jt.getSelectedRow();
if(rownum==-1)
{
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
new stuup(this,"修改学生",true,sm,rownum);
sm=new stumodel();
jt.setModel(sm);
}
if(e.getSource()==jmit41)
{
sqlconn sqc=new sqlconn();
int rownum=this.jt.getSelectedRow();
if(rownum==-1)
{
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
String stuids=(String)sm.getValueAt(rownum, 0);
try {
pstmt=sqc.getConnection().prepareStatement("delete from java_students where stuid=?");
pstmt.setString(1, stuids);
pstmt.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try {
if(rs!=null) rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null) sqc.getClose();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
sm=new stumodel();
jt.setModel(sm);
}
if(e.getSource()==jb1)
{
System.out.print("成功");
String name=this.jtf.getText().trim();
String sql="select * from java_students where stuname='"+name+"'";
sm=new stumodel(sql);
jt.setModel(sm);
}
else if(e.getSource()==jb2)
{
new stuadd(this,"添加学生",true);
sm=new stumodel();
jt.setModel(sm);
}
else if(e.getSource()==jb3)
{
int rownum=this.jt.getSelectedRow();
if(rownum==-1)
{
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
new stuup(this,"修改学生",true,sm,rownum);
sm=new stumodel();
jt.setModel(sm);
}
else if(e.getSource()==jb4)
{
sqlconn sqc=new sqlconn();
int rownum=this.jt.getSelectedRow();
if(rownum==-1)
{
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
String stuids=(String)sm.getValueAt(rownum, 0);
try {
pstmt=sqc.getConnection().prepareStatement("delete from java_students where stuid=?");
pstmt.setString(1, stuids);
pstmt.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try {
if(rs!=null) rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null) sqc.getClose();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
sm=new stumodel();
jt.setModel(sm);
}
else if(e.getSource()==jb5)
{
sm=new stumodel();
jt.setModel(sm);
}
}}
重绘下