Results 1 to 9 of 9
  1. #1
    tonyibm is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default the problem of setting background color of Jradiobutton

    i want to set the background color of Jradiobutton to be Green, and then wait for some time before the next action, so i use the Thread.sleep, but i found a problem:

    when i execute the code, the background color of the Jradiobutton do not show up, and at the same time, the selected status of the Jradiobutton do not show up too,

    i use breakpoint debug to debug the code row by row, and found that when that row of code for background color setting is executed, there is response from Jradiobutton, only after i add JOptionPane.showMessageDialog(null,"*******"), and when this row of code is executed, the background color and selected stauts of Jradiobutton all sho up,

    a part of the codes is as follows:

    jb1.setBackground(Color.GREEN);
    try
    {
    Thread.sleep(1000)
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }

    seems that Thread.sleep caused this problem, but i do not know why, and also do not know how to solve it,

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: the problem of setting background color of Jradiobutton

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tonyibm is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: the problem of setting background color of Jradiobutton

    thanks,

    i had used Timer, but i found the control on the time accuracy is not good, which caused some problem on my code,

    as my code is for randomly get a english word from database, and then four answers, namely, a english word test function,
    i found if i used Timer, after several rounds, there will be some problem,

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: the problem of setting background color of Jradiobutton

    Quote Originally Posted by tonyibm View Post
    thanks,

    i had used Timer, but i found the control on the time accuracy is not good, which caused some problem on my code,

    as my code is for randomly get a english word from database, and then four answers, namely, a english word test function,
    i found if i used Timer, after several rounds, there will be some problem,
    Perhaps we can help you solve some of those problems if you tell us more about them. But regardless, calling Thread.sleep(...) on the Swing event thread is not a viable solution.

  5. #5
    tonyibm is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: the problem of setting background color of Jradiobutton

    Thanks,

    i tried to use Swing Timer, my code is as follows:

    jb1.setBackground(Color.GREEN);


    Timer timer = new Timer(1000,
    new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    test1();
    }
    });

    timer.setRepeats(false);
    timer.start();

    method test1() is to get the next group of english word, but the problem i found is that:

    the first one or two group is ok, then the groups after that will get slower and slower speed of getting to the next group after the setting background color is completed, and there will be some skip during that, for example, after group 4, it is expected to get group 5, but after some time, and serious screen flicker, i get maybe group 9 or 10, and this problem get more and more serious,

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: the problem of setting background color of Jradiobutton

    No way to tell what is wrong based on the code you've posted, but it does sound like a bug in your code. Please post more, perhaps an SSCCE if possible, and please do use [code] [/code] tags when posting code as Darryl has already suggested with his links.

  7. #7
    tonyibm is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: the problem of setting background color of Jradiobutton

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.text.DecimalFormat;
    import java.io.*;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.ArrayList;
    
    import javax.swing.BorderFactory;
    import javax.swing.JRadioButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import java.util.Random;
    import javax.swing.JComboBox;
    import javax.swing.JButton;
    import javax.swing.ButtonGroup;
    import javax.swing.Timer;
    
    public class EnglishWordTest extends JFrame implements ActionListener
    {
       public JFrame f = null;
       public Container contentPane= null;
       public JRadioButton jb1,jb2,jb3,jb4 =null;
       public JLabel jl1,jl2,jl3 =null;
       public Insets inset;
       public GridBagLayout gbl;
       public ButtonGroup group;
       
       public JComboBox jcb =null;
       public JButton jbu = null;
       public final static String driver = "com.mysql.jdbc.Driver";
       public final static String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8";
       public final static String user = "root";	
       public final static String password = "261210";	
       public static Connection conn = null;
       public static Statement ps = null;
       public ResultSet rs = null;
       public static ArrayList<String> english_word;
       public static ArrayList<String> sound_mark;
       public static ArrayList<String> explaination;
       public static int[] array =null;
       public static int[] word = null;
       public static int[] word1 =null;
       public String index1 =null;
       public String className = null;
       public int totalword,wrongword;
       public String ratio;
       public Timer tim;
       
       public static void main(String[] args)
       {	
    	   
    	   EnglishWordTest test = new EnglishWordTest();
    	   test.initiate();
    	   
    	   
       }
       
       public void initiate()
       {
    	   gbl = new GridBagLayout();
    	   
    	   f = new JFrame();
    	   contentPane = f.getContentPane();
    	   contentPane.setLayout(gbl);
    
    	   jcb = new JComboBox();
    	   jcb.addItem("大学英语四级");
    	   jcb.addItem("大学英语六级");
    	   jcb.addItem("研究生考试英语");
    	   jcb.addItem("新词汇");
    	   
    	   jcb.setBorder(BorderFactory.createTitledBorder("请选择课本:"));
    	   
    	   jbu = new JButton("确定");
    	   jbu.addActionListener(this);
    	   
    	   contentPane.add(jcb);
    	   contentPane.add(jbu);
    	   
    	   gbl.setConstraints(jcb, new GridBagConstraints(10,10,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
    	   gbl.setConstraints(jbu, new GridBagConstraints(10,20,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
    	   
    	   f.pack();
    	   f.setVisible(true);
    	   f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    	   
    	   Dimension screenSize=java.awt.Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕的大小
    	   Dimension frameSize=f.getSize();//这里的this可替换成窗体的名字,下同
    	   f.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);//设置位置
    	     
    	   f.setAlwaysOnTop(true);
       }
       public void start()
       {
    	   GetEnglishWordFromDB();
    	   close();
    	   test();
    	   test1();
    	   
       }
       public void test()
       {
    
    	 gbl = new GridBagLayout();
    	 
    	 f = new JFrame();
         contentPane = f.getContentPane();
    	 contentPane.setLayout(gbl);
    	 
    	 jl1 = new JLabel();
    	 
    	 jb1 = new JRadioButton();
    	
    	 jb2 = new JRadioButton();
    	 
    	 jb3 = new JRadioButton();
    	 
    	 jb4 = new JRadioButton();
    	 
    	 group = new ButtonGroup();
    	 group.add(jb1);
    	 group.add(jb2);
    	 group.add(jb3);
    	 group.add(jb4);
    	 
         contentPane.add(jb1);
         contentPane.add(jb2);
         contentPane.add(jb3);
         contentPane.add(jb4);
         contentPane.add(jl1);
         
         f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
         
         f.setAlwaysOnTop(true);
    
    	 f.setVisible(true);
    	 
    	 f.addWindowListener(new WindowAdapter()
    	 {
    		 public void windowClosing(java.awt.event.WindowEvent evt) 
    		 {
    			 
    			 f.dispose();
    			 DecimalFormat decimal = new DecimalFormat("0.00%");
    			 decimal.setRoundingMode(RoundingMode.HALF_UP);
    			 
    			 ratio = decimal.format(((totalword -wrongword)*1.00)/(totalword*1.00));
    			 
    			 JOptionPane.showMessageDialog(null,"总共" + totalword + "个单词" + "\n" + wrongword + "个错误" + "\n" + "正确百分比为 " + ratio);
             }
    
         });
    	 
       }
       
       public void test1()
       {
    	   
    	   	 totalword ++;
    	   	 
    	   	 f.setTitle("第 " + totalword + " 个单词");
    	   	 
    	   	 group.clearSelection();
    	   	 
    		 getRandomArray(0,english_word.size(),4);
    		 word = array;
    		 getRandomArray(0, 4, 4);
    		 word1 = array;
    	   
    		 if(sound_mark.get(word[0]) ==null)
    			 jl1.setText(english_word.get(word[0]));
    		 else
    			 jl1.setText(english_word.get(word[0]) + "  [" + sound_mark.get(word[0]) + "]");
    		 
    		 jl1.setFont(new Font("",Font.BOLD,30));
    		 jl1.setForeground(Color.blue);
    		 gbl.setConstraints(jl1, new GridBagConstraints(10,10,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0));
    		 
    		 jb1.setText(explaination.get(word[word1[0]]));
    		 jb1.setFont(new Font("",Font.BOLD,25));
    		 jb1.setBackground(Color.cyan);
    		 gbl.setConstraints(jb1, new GridBagConstraints(10,20,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0));
    		 jb1.addActionListener(this);
    		 jb1.setBackground(Color.white);
    		 
    		 jb2.setText(explaination.get(word[word1[1]]));
    		 jb2.setFont(new Font("",Font.BOLD,25));
    		 gbl.setConstraints(jb2, new GridBagConstraints(10,30,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0));
    		 jb2.addActionListener(this);
    		 jb2.setBackground(Color.white);
    		 
    		 jb3.setText(explaination.get(word[word1[2]]));
    		 jb3.setFont(new Font("",Font.BOLD,25));
    		 gbl.setConstraints(jb3, new GridBagConstraints(10,40,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0));
    		 jb3.addActionListener(this);
    		 jb3.setBackground(Color.white);
    		 
    		 jb4.setText(explaination.get(word[word1[3]]));
    		 jb4.setFont(new Font("",Font.BOLD,25));
    		 gbl.setConstraints(jb4, new GridBagConstraints(10,50,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0));
    		 jb4.addActionListener(this);
    		 jb4.setBackground(Color.white);
    		 
    		 f.pack();
    		 
    		 Dimension screenSize=java.awt.Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕的大小
    	     Dimension frameSize=f.getSize();//这里的this可替换成窗体的名字,下同
    	     f.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);//设置位置
    		 
       }
       
       public void GetEnglishWordFromDB()
       {
    	   try 
    	   {
    	    Class.forName(driver);
    	    conn = DriverManager.getConnection(url,user,password);
    	    System.out.println("连接成功了");
    	   } 
    	   catch (Exception e) 
    	   {
    		   JOptionPane.showMessageDialog(null,"加载驱动失败......");
    	   }
    		try
    		{
    
    			ps  = conn.createStatement();
    			rs = ps.executeQuery(className);
    			
    			english_word = new ArrayList<String>();
    			sound_mark = new ArrayList<String>();
    			explaination = new ArrayList<String>();
    			
    			while(rs.next())
    			{
    				english_word.add(rs.getString(1));
    				sound_mark.add(rs.getString(2));
    				explaination.add(rs.getString(3));
    				
    			}
    	   		
    		}
    		catch(Exception e)
    		{
    			JOptionPane.showMessageDialog(null,"查询失败!" + "\n" + e.getMessage());
    	
    		}
    		finally
    		{
    			
    		}
    	}
    
       public void close()
       {
    	   try 
    	   {
    	    ps.close();
    	    conn.close();
    	   } 
    	   catch (Exception e) 
    	   {
    	    JOptionPane.showMessageDialog(null,"数据库连接关闭失败......");
    	   }
    	}
       
       public void actionPerformed(ActionEvent b)
       {
    
    	   if(b.getSource()==jbu) //如果点击的是确定按钮
    	   {
    		   switch(jcb.getSelectedItem().toString())
    		   {
    		   case "大学英语四级":
    			   className = "select * from english_word_CET4";
    			   break;
    		   case "大学英语六级":
    			   className = "select * from english_word_CET6";
    			   break;
    		   case "研究生考试英语":
    			   className = "select * from english_word_ETM";
    			   break;
    		   case "新词汇":
    			   className = "select * from english_word_NEW";
    			   break;
    		   }
    		   
    	   f.dispose();
    	   f = null;
    	   start();
    	   
           }
    	   
    	   if(jb1.isSelected()==true)
    	   {
    		   
    		   if(jb1.getText() == explaination.get(word[0]))
    		   {
    
    			   jb1.setBackground(Color.GREEN);
    			   
    			  
    			   Timer timer = new Timer(1000, 
    					   						new ActionListener() 
    			   								{
    				   								public void actionPerformed(ActionEvent e) 
    				   							{
    				   									test1();
    				   							}
    			   								});
    			   
    			   timer.setRepeats(false);
    			   timer.start(); 
    			   
    		   }
    		   else
    		   {
    			   jb1.setBackground(Color.red);
    
    			   f.setAlwaysOnTop(false);
    			   
    			   JOptionPane.showMessageDialog(null,"正确答案为:" + explaination.get(word[0]));
    			   
    			   f.setAlwaysOnTop(true);
    			   
    			   wrongword ++;
    			   
    			   test1();
    			   
    		   }
    			   
    	   }
    
    	   if(jb2.isSelected()==true)
    	   {
    		   
    		   if(jb2.getText() == explaination.get(word[0]))
    		   {
    
    			   jb2.setBackground(Color.GREEN);
    			   
    			   
    			   Timer timer = new Timer(1000, 
    							new ActionListener() 
    							{
    								public void actionPerformed(ActionEvent e) 
    								{
    									test1();
    								}
    							});
    
    			   timer.setRepeats(false);
    			   timer.start(); 
    
    		   }
    		   else
    		   {
    
    			   jb2.setBackground(Color.red);
    
    			   f.setAlwaysOnTop(false);
    			   
    			   JOptionPane.showMessageDialog(null,"正确答案为:" + explaination.get(word[0]));
    			   
    			   f.setAlwaysOnTop(true);
    			   
    			   wrongword ++;
    			   test1();
    			   
    		   }
    	   }
    
    	   if(jb3.isSelected()==true)
    	   {
    		   
    		   if(jb3.getText() == explaination.get(word[0]))
    		   {
    
    			   jb3.setBackground(Color.green);
    
    			   
    			   Timer timer = new Timer(1000, 
    						new ActionListener() 
    						{
    							public void actionPerformed(ActionEvent e) 
    							{
    								test1();
    							}
    						});
    
    		   timer.setRepeats(false);
    		   timer.start(); 
    		   
    		   }
    		   else
    		   {
    			   jb3.setBackground(Color.red);
    
    			   f.setAlwaysOnTop(false);
    			   
    			   JOptionPane.showMessageDialog(null,"正确答案为:" + explaination.get(word[0]));
    			   
    			   f.setAlwaysOnTop(true);
    			   
    			   wrongword ++;
    			   test1();
    		   }
    	   }
    
    	   if(jb4.isSelected()==true)
    	   {
    
    		   if(jb4.getText() == explaination.get(word[0]))
    		   {
    
    			   jb4.setBackground(Color.GREEN);
    
    			   
    			   Timer timer = new Timer(1000, 
    						new ActionListener() 
    						{
    							public void actionPerformed(ActionEvent e) 
    							{
    								test1();
    							}
    						});
    
    		   timer.setRepeats(false);
    		   timer.start(); 
    			   
    		   }
    		   else
    		   {
    			   jb4.setBackground(Color.red);
       
    			   f.setAlwaysOnTop(false);
    			   
    			   JOptionPane.showMessageDialog(null,"正确答案为:" + explaination.get(word[0]));
    			   
    			   f.setAlwaysOnTop(true);
    			   
    			   wrongword ++;
    			   test1();
    		   }
    	   }
    	   
       }
       
       public int[] getRandomArray(int begin, int end, int count) 
       {  
    	    int length = end - begin;
    	    if(count > length)
    	    {  
    	        throw new RuntimeException("IllegalArgumentsException: "  + "\"count\" shoud NOT greater than (end - benin)");  
    	    }  
    	    //顺序递增的数组   
    	    int[] ori = new int[length];
    	    for (int i = 0; i < length; i++) 
    	    {  
    	        ori[i] = i + begin;
    	    }  
    	      
    	    array = new int[count];
    	    int index, temp;  
    	    Random random = new Random();  
    	    for (int i = 0; i < count; i++) 
    	    {  
    	        //从ori中随机取值,赋给array   
    	        index = random.nextInt(length);
    	        array[i] = ori[index];
    	        
    	        //把取过的ori数组中的元素跟数组中的最后一个元素交换位置,    
    	        temp = ori[index];  
    	        ori[index] = ori[length-1];  
    	        ori[length-1] = temp;  
    	        //数组长度减1,下次循环将从剩下的值中继续随机抽取,因为取过的元素已经换到最后的位置,再通过数组长度减1,将数组的最后一个元素排除在外
    	        length--;  
    	    }  
    	  
    	    return array;  
    	}  
       
    }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: the problem of setting background color of Jradiobutton

    Your stated problem is to do with the background of a JRadioButton. So why have you posted code containing ArrayLists and database access?

    Did you even bother to go through the SSCCE link? If you don't take advice already given, do you think members here will continue to advise you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    tonyibm is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: the problem of setting background color of Jradiobutton

    Quote Originally Posted by DarrylBurke View Post
    Your stated problem is to do with the background of a JRadioButton. So why have you posted code containing ArrayLists and database access?

    Did you even bother to go through the SSCCE link? If you don't take advice already given, do you think members here will continue to advise you?

    db
    Thanks,I have already resolved the problem,

Similar Threads

  1. BackGround JRadioButton
    By antonello12 in forum AWT / Swing
    Replies: 1
    Last Post: 10-10-2009, 06:33 AM
  2. Setting selected JRadioButton in ButtonGroup
    By emceenugget in forum AWT / Swing
    Replies: 3
    Last Post: 01-29-2009, 06:05 PM
  3. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  4. JRadioButton set object color
    By King8654 in forum AWT / Swing
    Replies: 4
    Last Post: 04-24-2008, 09:52 AM
  5. Problem Setting offscreen background
    By D34N0 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2007, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •