Adding text to java frame
I am new to java and i am having trouble adding text to frame. can anyone help?
[79 byte] By [
neo22] at [2007-11-11 8:07:51]

# 1 Re: Adding text to java frame
Can you be more specific?
If by "adding text" you mean drawing a String of text on the screen, that can be done by drawing to the current graphics state of the frame. You can get that by using the getGraphics() method. Then, you can draw to that:
Graphics g = getGraphics();
g.drawString("this is a test", 0, 0);
So first, we are passing a reference of the current graphics state to a variable g. Then, we are drawing a String to that reference using the drawString(String, int, int) method from the Graphics class. The first argument is a String, which will be the String that you want drawn on the frame. The next two are the coordinates, the first one being x, the second y.
So the above code simply draws "this is a test" at coordinates (0, 0). Note that the above can be simplified to getGraphics().drawString("this is a test", 0, 0);.
You also might be referring to adding a text box or text area to the field, then setting the text value of that.
Again, please be more specific.
destin at 2007-11-11 22:36:18 >

# 2 Re: Adding text to java frame
Sorry i was not specific...
Ok here i will try to explain again (i willl try this once i get home since i am at work now)
Basically i have loads of conversion calculations ....
Like i have to convert inch to centimeter ... display centimeters ... then convert contimeters to feet and inch .... again display feet then inch...
i have implemented a frame using Frame f = new Frame() and i have set the size and everything ....
now i only know the method to add label but the problem is i am only getting the inch calculation on the screen ...
I was looking at the graphics as well ... should i do it that way?
neo22 at 2007-11-11 22:37:23 >

# 3 Re: Adding text to java frame
Well, setting the text value of a label can be done in it's constructor. Then you just add it to the frame.
Label label = new Label("text");
frame.getContentPane().add(label);
destin at 2007-11-11 22:38:27 >

# 4 Re: Adding text to java frame
Ok but can i keep adding more labels ? will they all display one after another
neo22 at 2007-11-11 22:39:21 >

# 5 Re: Adding text to java frame
This is what i have right now ... please correct me where i am wrong ... i am only a beginner... only the last calculation is being displayed
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class test extends Frame
{
public static void main(String[] args)
{
Frame f = new Frame("Banners Results");
f.addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
f.setSize(500,700);
//f.setLocation(10,10);
f.setVisible(true);
String Test;
int number;
Test = JOptionPane.showInputDialog("Enter no: ");
number = Integer.parseInt(Test);
f.add(new Label(number + " "));
double number1 = number * 500;
f.add(new Label((int)number1+ " "));
double number2 = number1 * 0.3;
f.add(new Label((int)number2+ " ")); //THIS LINE COMES UP ON THE FRAME .... THe lines above this don't display
}
}
neo22 at 2007-11-11 22:40:31 >

# 6 Re: Adding text to java frame
My first question... why are you extending Frame and then creating and displaying a completely new Frame
Anyway, you want to add stuff to the content pane of the frame. Take a look at this code:
import java.awt.*;
public class MyFrame extends Frame {
public MyFrame() {
super("My Frame");
Panel pane = (Panel) getContentPane();
pane.setPreferredSize(new Dimenion(100, 100));
Button b = new Button("Click me!");
b.setSize(115, 30);
b.setLocation(10, 10);
pane.add(b);
Label label = new Label("Click the button!");
label.setSize(150, 25);
label.setLocation(10, 50);
pane.add(label);
pane.setLayout(new BorderLayout());
pack();
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new MyFrame();
}
}
destin at 2007-11-11 22:41:25 >
