Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Roll Dice Program Button

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Roll Dice Program Button

    Guys How to disabled button "STOP" and When you click a button"ROLL" now the button "Roll" are disabled and "STOP" are enabled.. Who knows ?


    sorry for my grammar.




    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    	public class LabEx4 extends JFrame{
     
    	private boolean stop = false;
    	private JLabel rollgame, rid1, rid2, rid3, rid4, rid5, rid6, led1, led2, led3, led4, led5, led6;
    	private JButton Roll, Stop;
    	private StartButtonHandler startB;
    	private StopButtonHandler stopB;
    	private int pict = 1;
     
     
    	public void Runner(){
    	rollgame = new JLabel("Rolling Dice for Games");
    	rollgame.setFont(new Font("Grease", Font.BOLD, 40));
     
    	ImageIcon rd1 = new ImageIcon(
    	getClass().getResource("Dice1.jpg"));
    	rid1 = new JLabel(rd1);
    	rid1.setVisible(true);
     
    	ImageIcon rd2 = new ImageIcon(
    	getClass().getResource("Dice2.jpg"));
    	rid2 = new JLabel(rd2);
    	rid2.setVisible(false);
     
    	ImageIcon rd3 = new ImageIcon(
    	getClass().getResource("Dice3.jpg"));
    	rid3 = new JLabel(rd3);
    	rid3.setVisible(false);
     
    	ImageIcon rd4 = new ImageIcon(
    	getClass().getResource("Dice4.jpg"));
    	rid4 = new JLabel(rd4);
    	rid4.setVisible(false);
     
    	ImageIcon rd5 = new ImageIcon(
    	getClass().getResource("Dice5.jpg"));
    	rid5 = new JLabel(rd5);
    	rid5.setVisible(false);
     
    	ImageIcon rd6 = new ImageIcon(
    	getClass().getResource("Dice6.jpg"));
    	rid6 = new JLabel(rd6);
    	rid6.setVisible(false);
     
    	ImageIcon ld1 = new ImageIcon(
    	getClass().getResource("Dice1.jpg"));
    	led1 = new JLabel(ld1);
    	led1.setVisible(true);
     
    	ImageIcon ld2 = new ImageIcon(
    	getClass().getResource("Dice2.jpg"));
    	led2 = new JLabel(ld2);
    	led2.setVisible(false);
     
    	ImageIcon ld3 = new ImageIcon(
    	getClass().getResource("Dice3.jpg"));
    	led3 = new JLabel(ld3);
    	led3.setVisible(false);
     
    	ImageIcon ld4 = new ImageIcon(
    	getClass().getResource("Dice4.jpg"));
    	led4 = new JLabel(ld4);
    	led4.setVisible(false);
     
    	ImageIcon ld5 = new ImageIcon(
    	getClass().getResource("Dice5.jpg"));
    	led5 = new JLabel(ld5);
    	led5.setVisible(false);
     
    	ImageIcon ld6 = new ImageIcon(
    	getClass().getResource("Dice6.jpg"));
    	led6 = new JLabel(ld6);
    	led6.setVisible(false);
     
    	Container pane = getContentPane();
    	pane.setLayout(new FlowLayout(FlowLayout.CENTER, 10,10));
    	pane.setBackground(Color.WHITE);
     
    	pane.add(rollgame);
    	pane.add(rid1);
    	pane.add(rid2);
    	pane.add(rid3);
    	pane.add(rid4);
    	pane.add(rid5);
    	pane.add(rid6);
    	pane.add(led1);
    	pane.add(led2);
    	pane.add(led3);
    	pane.add(led4);
    	pane.add(led5);
    	pane.add(led6);
     
    	JButton Roll = new JButton("ROLL");
    	Roll.setFont(new Font("Georgia", Font.BOLD, 20));
    	Roll.setPreferredSize(new Dimension(200,60));
    	startB = new StartButtonHandler();
    	Roll.addActionListener(startB);
     
    	JButton Stop = new JButton("STOP");
    	Stop.setFont(new Font("Georgia", Font.BOLD, 20));
    	Stop.setPreferredSize(new Dimension(200,60));
    	stopB = new StopButtonHandler();
    	Stop.addActionListener(stopB);
     
    	JPanel pan = new JPanel();
    	pan.setLayout(new FlowLayout(FlowLayout.CENTER, 45,40));
    	pan.setBackground(Color.WHITE);
    	pan.add(Roll);
    	pane.add(pan, BorderLayout.CENTER);
    	pan.add(Stop);
     
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setTitle("Two Roll Dice");
    	setSize(600, 500);
    	setLocation(180,50);
    	setResizable(false);
    	setVisible(true);
     
    	}
    		private class StartButtonHandler implements ActionListener{
    		public void actionPerformed(ActionEvent ev){
    		stop = false;
     
    		Thread to = new Thread(){
    		public void run(){
    			while(!stop){
    				try{
    					if(pict == 1){
    					rid1.setVisible(true);
    					rid2.setVisible(false);
    					rid3.setVisible(false);
    					rid4.setVisible(false);
    					rid5.setVisible(false);
    					rid6.setVisible(false);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(false);
    					led4.setVisible(false);
    					led5.setVisible(false);
    					led6.setVisible(true);
    					pict++;
    					sleep(200);
    					}
     
    					else if(pict == 2){
    					rid1.setVisible(false);
    					rid2.setVisible(true);
    					rid3.setVisible(false);
    					rid4.setVisible(false);
    					rid5.setVisible(false);
    					rid6.setVisible(false);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(false);
    					led4.setVisible(false);
    					led5.setVisible(true);
    					led6.setVisible(false);
    					pict++;
    					sleep(200);
     
    					}
    					else if(pict == 3){
    					rid1.setVisible(false);
    					rid2.setVisible(false);
    					rid3.setVisible(true);
    					rid4.setVisible(false);
    					rid5.setVisible(false);
    					rid6.setVisible(false);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(false);
    					led4.setVisible(true);
    					led5.setVisible(false);
    					led6.setVisible(false);
    					pict++;
    					sleep(200);
    					}
     
    					else if(pict == 4){
    					rid1.setVisible(false);
    					rid2.setVisible(false);
    					rid3.setVisible(false);
    					rid4.setVisible(false);
    					rid5.setVisible(true);
    					rid6.setVisible(false);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(true);
    					led4.setVisible(false);
    					led5.setVisible(false);
    					led6.setVisible(false);
    					pict++;
    					sleep(200);
    					}
     
    					else if(pict == 5){
    					rid1.setVisible(false);
    					rid2.setVisible(false);
    					rid3.setVisible(false);
    					rid4.setVisible(false);
    					rid5.setVisible(true);
    					rid6.setVisible(false);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(false);
    					led4.setVisible(false);
    					led5.setVisible(true);
    					led6.setVisible(false);
    					pict++;
    					sleep(200);
    					}
     
    					else{
    					rid1.setVisible(false);
    					rid2.setVisible(false);
    					rid3.setVisible(false);
    					rid4.setVisible(false);
    					rid5.setVisible(false);
    					rid6.setVisible(true);
    					led1.setVisible(false);
    					led2.setVisible(false);
    					led3.setVisible(false);
    					led4.setVisible(false);
    					led5.setVisible(false);
    					led6.setVisible(true);
    					pict=1;
    					sleep(200);
    				}
    			}
    				catch(InterruptedException e){
    					System.out.println(e);
    					}
    				}
    			}
    		};
    		to.start();
    		}
    	}
     
    	private class StopButtonHandler implements ActionListener{
    		public void actionPerformed(ActionEvent ev){
    			stop = true;
    		}
    	}
    }

    Main class
    public class MainEx4 {
     
       public static void main(String [] args){
     
    	javax.swing.SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    			new LabEx4().Runner();
    			}
    		});
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Roll Dice Program Button

    Is this the same question?
    http://www.javaprogrammingforums.com...html#post92770

    What did you find in the API doc?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 24th, 2013, 11:55 AM
  2. Can't figure out how to roll individual dice.
    By mortiz492 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2012, 02:07 PM
  3. Dice score program
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 23rd, 2012, 05:25 PM
  4. Java Dice Program
    By ebone in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2011, 11:07 PM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM