Jump to content

Simply GUI Question

- - - - -

  • Please log in to reply
3 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Posted Image



import javax.swing.*;


import java.awt.*;

import java.awt.event.*;



public class StandFord {

	

	public static void main(String[] args) 

	{ 

		new obj();

			

    }


}


class obj extends JFrame{

	

	public obj()

	{	

	JFrame fr = new JFrame("Welcome");  fr.setLocation(400,300); fr.setSize(300, 300);

	

	Container cont =fr.getLayeredPane();	

	cont.setLayout(new BorderLayout());

	

	// UP PANEL

	JPanel UpPanel =new JPanel();

	UpPanel.setLayout(new GridLayout(1,1));	

	final JLabel answer = new JLabel();

	UpPanel.add(answer);	 

	cont.add(UpPanel,   BorderLayout.NORTH);

	

	// DOWN PANEL

	JPanel downPanel =new JPanel();

	downPanel.setLayout(new GridLayout(1,1));

	final JButton but1 = new JButton("Click It");

	downPanel.add(but1);

	cont.add(downPanel,  BorderLayout.SOUTH);

	

	

	 ActionListener l = new ActionListener()

	 {

		 public void actionPerformed (ActionEvent e)

		 {

			 

			 if(e.getSource()==but1)

			 {		

				 

				 answer.setText("hi");

				

			 }

		 }

				

	 };

	but1.addActionListener(l);

	

	


	fr.setVisible(true);	

	}

	

}




Question 1)

Now I want to change something.

when I click , my program writes:
hi


Then If I click again, I want to write:
hi
hi2


then...
hi
hi2
hi3



Question 2)
could I see what happens each line in my code, step-by-step? For example like FireBug or Visual Studio has, when I Debug it step by step..
GNU/Linux Is the Best.

#2
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
1. You need to declare an int as a class variable and set it to 0. This variable can then be appended to the output string and incremented when the button is clicked.

If you want the output to be on separate lines as described then it's a little trickier as the JLable doesn't process a \n as expected. However the JLabel will accept simple html so you can wrap the out put in html tags and have each line of output as a new paragraph.

My code for the action listener is below. Count is the int I declared to keep track of the number of times the button has been clicked.

         public void actionPerformed (ActionEvent e)
         {
             
                if(e.getSource()==but1)
                {        
                      if (count == 0) {
                           answer.setText("<html><p>hi</p></html>");
                           count++;
                     } else {
                           String ansStr = answer.getText();
                           ansStr = ansStr.replace("</html>","");
                           ansStr += "<p>hi" + Integer.toString(count) + "</p></html>";
                           answer.setText(ansStr);
                           count++;
                     }
                }
         }

2) You probably need a full IDE with a debugger. I'd imagine that both eclipse and netbeans have a debugger but it's a while since I've used either so I can't be 100%.
There's probably a more elegant way of achieving the same result but this works.
If there's a new way, I'll be the first in line.

But, it better work this time.

#3
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

Hignar said:

1. You need to declare an int as a class variable and set it to 0. This variable can then be appended to the output string and incremented when the button is clicked.

If you want the output to be on separate lines as described then it's a little trickier as the JLable doesn't process a \n as expected. However the JLabel will accept simple html so you can wrap the out put in html tags and have each line of output as a new paragraph.

My code for the action listener is below. Count is the int I declared to keep track of the number of times the button has been clicked.


         public void actionPerformed (ActionEvent e)

         {

             

                if(e.getSource()==but1)

                {        

                      if (count == 0) {

                           answer.setText("<html><p>hi</p></html>");

                           count++;

                     } else {

                           String ansStr = answer.getText();

                           ansStr = ansStr.replace("</html>","");

                           ansStr += "<p>hi" + Integer.toString(count) + "</p></html>";

                           answer.setText(ansStr);

                           count++;

                     }

                }

         }


2) You probably need a full IDE with a debugger. I'd imagine that both eclipse and netbeans have a debugger but it's a while since I've used either so I can't be 100%.
There's probably a more elegant way of achieving the same result but this works.



1 ) thanks a lot!
2) could you give me a link of the full IDE with Debugger, please? :)
GNU/Linux Is the Best.

#4
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
I'm assuming from your sig that you're a linux user in which case you'd probably be best obtaining either eclipse or netbeans from distros repositories (both have been available in all the distros I've tried).

Failing that websites are

Eclipse - Eclipse - The Eclipse Foundation open source community website.
Netbeans - Welcome to NetBeans
If there's a new way, I'll be the first in line.

But, it better work this time.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users