8 Replies - 658 Views - Last Post: 07 November 2010 - 03:53 PM Rate Topic: -----

#1 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

Beginner Ask Questions

Posted 06 November 2010 - 08:19 AM

Hi All,

I am a beginner in java programming and would like to ask some questions that I could not found in the tutorials here and other topics.

I would like to create a simple application with a button and a label. The variable should start with 2 and every time when press the button should increase with 2.

I am on that stage and I stuck a bit because the button is just active once. I need a little help how to fix it, thanks.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.*;
//import statements
//Check if window closes authomatically.Otherwise add suitable code.
/**
 *
 * @author I
 */
public class ButtonAndLabel extends JFrame implements ActionListener {
    JLabel jlbLabel1 = new JLabel();
    //JLabel jlbLabel2 = new JLabel();
    JButton jbtMyButton = new JButton();
    private int a;
    public static void main(String args[]){
        new ButtonAndLabel();
    }
    ButtonAndLabel(){
         //layout tells the computer where to put all the components
        FlowLayout floMyLayout = new FlowLayout();
        setLayout(floMyLayout);
        //creata the components
        jlbLabel1.setText("Hello");
       //jlbLabel2.setText("Whatever");
        jbtMyButton.setText("Press me");
         //put the components on the window
          add(jlbLabel1);
       add(jlbLabel2);
          add(jbtMyButton);

        a = 2;
                
         // I want to keep an eye on the button to see when is has been pressed
         jbtMyButton.addActionListener(this);
         if (a>=2){
                 a = a+2;
                 }
            else{
            System.out.println("OUT");
            }
       
          this.setSize(100, 100);
          //pack()
          setVisible(true);
          }
          //what do we do when the button has been pressed?
          public void actionPerformed(ActionEvent e){
             
              jlbLabel1.setText("Result " + a);
             
          }
          
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Beginner Ask Questions

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Beginner Ask Questions

Posted 06 November 2010 - 09:43 AM

I changed your code a bit so that it is easier to read and more conventional. I also set up your listener. Feel free to ask questions.

/**
 * @Title: ButtonAndLabel.java
 * @Author: doychin_90
 * @Date: Nov 6, 2010
 */

import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.*;
 
public class ButtonAndLabel extends JFrame {

	private int a = 2;
	JPanel myPanel;
    JLabel myLabel;
    JButton myButton;
	
    public static void main(String args[]){
	
        new ButtonAndLabel();
		
    }
	
    ButtonAndLabel(){
	
		setTitle("Title"); // Set the title of the window.
		setSize(100,100); // Set the size of the window.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Tells the computer what do you when the window is closed.
	
        FlowLayout myLayout = new FlowLayout(); // Tells the computer where to place each component.
        setLayout(myLayout);
		
		myLabel = new JLabel("Label"); // Creates a label named myLabel.
		myButton = new JButton("Button"); // Creates a button named myButton.
		
		myPanel = new JPanel();
		myPanel.add(myLabel); // Add myLabel to the window.
		myPanel.add(myButton); // Add myButton to the window.
		
		myButton.addActionListener(new ButtonListener()); // Listen for myButton being pressed.
		
		add(myPanel);
		setVisible(true);
		
	}
	
	public class ButtonListener implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
	
			if (a >= 2) {
				a += 2;
				JOptionPane.showMessageDialog(null,"Result " + a,"Result",JOptionPane.PLAIN_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null,"OUT","Result",JOptionPane.PLAIN_MESSAGE);
			}
		
		}
		
	}
	
}


Also, I recommend you check out these tutorials by jinnyishere: Swing Tutorial Parts I-V

This post has been edited by Jambr: 06 November 2010 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

Re: Beginner Ask Questions

Posted 06 November 2010 - 12:07 PM

Thank you for the time you spend on my code Jambr! I modify my first code a bit to have your idea and it is working,however, my initial idea was the numbers to be placed into the label. I mean I do not need another window to pop up every time when I press the button with the result, just numbers to increase next to the button, in the label. I am sure that it is easier than your suggestion but I can't figure out how to do it.
Was This Post Helpful? 0
  • +
  • -

#4 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Beginner Ask Questions

Posted 06 November 2010 - 04:29 PM

Perhaps this is what you are looking for:

/**
 * @Title: ButtonAndLabel.java
 * @Author: doychin_90
 * @Date: Nov 6, 2010
 */

import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.*;
 
public class ButtonAndLabel extends JFrame {

	private int a = 2;
	JPanel myPanel;
    JLabel myLabel;
    JButton myButton;
	
    public static void main(String args[]){
	
        new ButtonAndLabel();
		
    }
	
    ButtonAndLabel(){
	
		setTitle("Title"); // Set the title of the window.
		setSize(100,90); // Set the size of the window.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Tells the computer what do you when the window is closed.
	
        FlowLayout myLayout = new FlowLayout(); // Tells the computer where to place each component.
        setLayout(myLayout);
		
		myLabel = new JLabel(Integer.toString(a)); // Creates a label named myLabel.
		myButton = new JButton("Button"); // Creates a button named myButton.
		
		myPanel = new JPanel();
		myPanel.add(myLabel); // Add myLabel to the window.
		myPanel.add(myButton); // Add myButton to the window.
		
		myButton.addActionListener(new ButtonListener()); // Listen for myButton being pressed.
		
		add(myPanel);
		setVisible(true);
		
	}
	
	public class ButtonListener implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
	
			if (a >= 2) {
				a += 2;
				myLabel.setText(Integer.toString(a));
			} else {
				JOptionPane.showMessageDialog(null,"OUT","Result",JOptionPane.PLAIN_MESSAGE);
			}
		
		}
		
	}
	
}


What you need to do is to take your integer a, and pass it to myLabel. Unfortunately, in Java, this can't simply be done with myLabel.setText(a);, instead you must use myLabel.setText(Integer.toString(a)); to convert the integer to a string so that it can be passed to myLabel.

When you click the button, the label now increments by 2.

This post has been edited by Jambr: 06 November 2010 - 04:32 PM

Was This Post Helpful? 1
  • +
  • -

#5 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

Re: Beginner Ask Questions

Posted 07 November 2010 - 11:52 AM

YES! It is working and it is what I want.It was giving me again the other window with the result but I modified myButton.addActionListener(new ButtonListener()); with myButton.addActionListener(this); and everything start working as I want.
Was This Post Helpful? 0
  • +
  • -

#6 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

Re: Beginner Ask Questions

Posted 07 November 2010 - 12:08 PM

As I moved forward in different tutorials I found difficult to present a rolling dice. I created I program to display randomly numbers between 1-6. However, I can't display a picture of a dice containing the selected number?Any ideas?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.ImageIcon;


public class OneToSix extends JFrame implements ActionListener {

    JFrame jFrame = new JFrame();
    JLabel jlbLabel = new JLabel();
    JButton jbtButton = new JButton();
    Random generator = new Random();
    private int randomIndex;
    

    public static void main(String agrs[]){
        new OneToSix();
    }
     OneToSix() {
        setSize(110,110);
        FlowLayout floMyLayout = new FlowLayout();
        setLayout(floMyLayout);

        jbtButton.setText("Aggregate");
        jlbLabel.setText(Integer.toString(randomIndex));
        add(jlbLabel);
        add(jbtButton);

        jbtButton.setSize(100,100);
        setVisible(true);
        jbtButton.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
        if (randomIndex<7){
        randomIndex = generator.nextInt(7);
        jlbLabel.setText(Integer.toString(randomIndex));
    }
 else{
            System.out.println("OUT");
 }

    }
    
}
 


Was This Post Helpful? 0
  • +
  • -

#7 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Beginner Ask Questions

Posted 07 November 2010 - 12:11 PM

Sorry, I forgot to change JOptionPane.showMessageDialog(null,"OUT","Result",JOptionPane.PLAIN_MESSAGE); to myLabel.setText("OUT");. That should fix it as well, although your way works too.
Was This Post Helpful? 0
  • +
  • -

#8 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

Re: Beginner Ask Questions

Posted 07 November 2010 - 03:45 PM

Do you have any ideas of my last request?
Was This Post Helpful? 0
  • +
  • -

#9 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Beginner Ask Questions

Posted 07 November 2010 - 03:53 PM

If you rephrased the request for me I might be able to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1