LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2006, 11:44 AM   #1
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Rep: Reputation: 0
How to label answer in applet ???????


I don’t know how to label the answer of a formula in applet and how I can put a word before and after the answer .

 
Old 04-27-2006, 12:48 PM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hi again mema_UAE!

Sorry, but I not quite understood your question. If you post some code or some more detailed explanation of what you are trying to accomplish I may be able to give you a hand. Don't be ashamed to draw what you want in a paper and post here if you have to
 
Old 04-27-2006, 01:57 PM   #3
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
In this example the answer is work and appears as a label but I want to print a The answer is:
before the label answer and after label answer I want to print this word Square

Also I want to print the a title ("square program"); center above the code and in the new line I want to print (" enter a number"); center


code:


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;



public class answer extends Applet implements ActionListener{

Label l1=new Label ("square program");

Label l2=new Label (" enter a number");

TextField t1=new TextField(20);
Button b1=new Button("Square");
Label l3=new Label("answer");


public void init()
{
add(l1);
add(l2);

add(t1);
add(b1);
b1.addActionListener(this);
add(l3);

}



public void actionPerformed(ActionEvent e)
{
int answer;

if (e.getSource()==b1)
{
answer=Integer.parseInt(t1.getText());
answer=answer*answer;

l3.setText(Integer.toString(answer));

}

}


}
 
Old 04-27-2006, 04:17 PM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
OK. That's a little bit more complicated than it looks and if you are going to stick with GUI/Applet programming, it might be a good idea to learn about Layout Managers and how they work:

http://java.sun.com/docs/books/tutor...out/using.html

I did some modifications to your code, but it is far from perfect:

Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.*;


public class Answer extends Applet implements ActionListener {

    private TextField tfDisplay = new TextField(20);

    private Button btnSquare = new Button("Square");

    private Label lblAnswer = new Label("Answer");

    public void init() {
		
	setLayout(new BorderLayout());
	setSize(320, 240);
		
	Panel topPanel = new Panel();
	Font bigFont = new Font("serif", Font.BOLD, 28);
	Label lblTitle = new Label("Square Program");
	lblTitle.setFont(bigFont);
	topPanel.add(lblTitle);
		
	Panel centerPanel = new Panel();
	centerPanel.add(new Label("Enter a number: "), BorderLayout.WEST);
	centerPanel.add(tfDisplay);
	centerPanel.add(btnSquare);
		
	Panel bottomPanel = new Panel();
	bottomPanel.add(lblAnswer);
		
	add(topPanel, BorderLayout.NORTH);
	add(centerPanel, BorderLayout.CENTER);
	add(bottomPanel, BorderLayout.SOUTH);
		
		
	btnSquare.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
	int answer;

	if (e.getSource() == btnSquare) {
		answer = Integer.parseInt(tfDisplay.getText());
		answer = answer * answer;
		lblAnswer.setText(Integer.toString(answer));

	}

    }

}
 
Old 04-27-2006, 05:28 PM   #5
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

It’s actually perfect ………and it much better and understandable than my codes ……………………… but I still need the answer of how I can but a comment after and before the answer. Fore ample If I enter number 2 the result should print
The Number is : 4 [this is the answer] %


 
Old 04-27-2006, 05:37 PM   #6
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Cool. I'm glad you liked. It's very hard coded still, but I'm sure you will fix it . To get the label where you want, in one line of code, it would be:

Code:
	Panel bottomPanel = new Panel();
	bottomPanel.add(new Label("The answer is: "));
	bottomPanel.add(lblAnswer);
That part in bold is the line you've to add to the above code ^_^.
 
Old 04-30-2006, 11:51 AM   #7
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

Thanks *~~~~* Mega Man X *~~~~*
The Program is working know properly...

Thanks Again...

 
Old 04-30-2006, 02:37 PM   #8
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Awesome. Always a pleasure to give a hand to another Java enthusiast ^_^.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/etc/fstab LABEL ziox Linux - Software 3 12-24-2004 06:14 PM
Java applet error: "Applet Failed" nro Programming 1 08-28-2004 05:52 PM
label printer imorrison Linux - Newbie 1 07-17-2004 10:55 PM
removing "Java Applet window" label blahJake Programming 3 06-14-2004 01:22 PM
Clicking on a label in QT Blaktyger Programming 6 02-10-2004 12:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration