Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface disaster! - Please troubleshoot 2

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
I have literally put hours and hours and hours into trying to place these few interface components in a pleasant layout, but to no avail. Things have grown worse, not better. Now I cannot even get anything to appear on screen. Would somebody please fix this piece of junk? Or please just fix enough of it to give me a pattern to follow.

All I want is a pleasant layout like the following:

Title Label (centered)

Label & Textbox

Label & Textbox

Textarea

Here is my code:


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

public class SaveLongMoney extends Applet implements ActionListener
{
private Button btnCalc; private Label lblTitle;
private Label lblDayMin;
private Label lblEveMin;
private Label lblResults;

private TextField txtDayMin;
private TextField txtEveMin;
private TextArea txaResults;

private String strDayMin;
private String strEveMin;
private int intDayMin;
private int intEveMin;


public void init()
{
setLayout(null);
Font fontTitle = new Font("Sans", Font.BOLD, 24);
Font fontIface = new Font("Sans", Font.BOLD, 12);

lblTitle = new Label("Save Long Money!");
lblTitle.setSize(400, 50);
//lblTitle.setBounds(0, 0, 500, 50);
lblTitle.setAlignment(Label.CENTER);
lblTitle.setFont(fontTitle);
add(lblTitle);

lblDayMin = new Label("Avg. Monthly Daytime Minutes:");
lblDayMin.setSize(300, 30);
lblDayMin.setLocation(60, 0);
lblDayMin.setFont(fontIface);
add(lblDayMin);

txtDayMin = new TextField(4);
//txtDayMin.setFont(fontIface);
add(txtDayMin);

lblEveMin = new Label("Avg. Monthly Evening/Weekend Minutes:");
add(lblEveMin);

txtEveMin = new TextField(4);
add(txtEveMin);

lblResults = new Label("Results:");
add(lblResults);

Font fontTXA = new Font("Courier", Font.PLAIN, 12);
txaResults = new TextArea("", 14, 75, 0);
txaResults.setEditable(false);
txaResults.setFont(fontTXA);
//txaResults.setLocation(20,200);
add(txaResults);

btnCalc = new Button("Calculate");
btnCalc.addActionListener(this);
add(btnCalc);
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnCalc)
{
strDayMin = txtDayMin.getText();
strEveMin = txtEveMin.getText();
txaResults.setText("Number of daytime minutes: " + strDayMin + "\n");
txaResults.append("Number of evening/weekend minutes: " + strEveMin + "\n");
txaResults.append("======================================================================\n");
txaResults.append("\n");
txaResults.append("Plan Name\t\tFee\tDaytime\t\tEvening\t\tTotal\n");
txaResults.append("______________________________________________________________________\n");
txaResults.append("Free Eve/Weekend\t$10.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("Normal\t\t\t $0.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("High Volume\t\t $5.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("======================================================================\n");
txaResults.append("\n");
}
}

public static void main (String args[])
{

}
}
 
hello,

for some reason, I could remember that you should use the 'setLocation' and 'setBounds' methods in the 'paint' method.

so you have just to redefine this method :

public void paint(Graphics g) {

// put your code here
}

manu0
 
Hi,

Here is your codes:-

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

public class SaveLongMoney extends Applet implements ActionListener
{
private Button btnCalc;
private Label lblTitle;
private Label lblDayMin;
private Label lblEveMin;
private Label lblResults;

private TextField txtDayMin;
private TextField txtEveMin;
private TextArea txaResults;

private String strDayMin;
private String strEveMin;
private int intDayMin;
private int intEveMin;


public void init()
{
setLayout(null);
setSize(400,400);
Font fontTitle = new Font("Sans", Font.BOLD, 24);
Font fontIface = new Font("Sans", Font.BOLD, 12);

lblTitle = new Label("Save Long Money!");
lblTitle.setBounds(new Rectangle(60,0,250,24));
lblTitle.setAlignment(Label.CENTER);
lblTitle.setFont(fontTitle);
add(lblTitle,null);

lblDayMin = new Label("Avg. Monthly Daytime Minutes:");
lblDayMin.setBounds(new Rectangle(10,50,170,20));
lblDayMin.setFont(fontIface);
add(lblDayMin,null);

txtDayMin = new TextField(4);
txtDayMin.setBounds(new Rectangle(190,50,170,20));
add(txtDayMin,null);

lblEveMin = new Label("Avg. Monthly Evening/Weekend Minutes:");
lblEveMin.setBounds(new Rectangle(10,80,170,20));
add(lblEveMin,null);

txtEveMin = new TextField(4);
txtEveMin.setBounds(new Rectangle(190,80,170,20));
add(txtEveMin,null);

lblResults = new Label("Results:");
lblResults.setBounds(new Rectangle(10,110,170,20));
add(lblResults,null);

Font fontTXA = new Font("Courier", Font.PLAIN, 12);
txaResults = new TextArea("", 14, 75, 0);
txaResults.setEditable(false);
txaResults.setFont(fontTXA);
txaResults.setBounds(new Rectangle(190,110,100,100));
add(txaResults,null);

btnCalc = new Button("Calculate");
btnCalc.addActionListener(this);
btnCalc.setBounds(new Rectangle(10,220,100,20));
add(btnCalc,null);
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnCalc)
{
strDayMin = txtDayMin.getText();
strEveMin = txtEveMin.getText();
txaResults.setText("Number of daytime minutes: " + strDayMin + "\n");
txaResults.append("Number of evening/weekend minutes: " + strEveMin + "\n");
txaResults.append("======================================================================\n");
txaResults.append("\n");
txaResults.append("Plan Name\t\tFee\tDaytime\t\tEvening\t\tTotal\n");
txaResults.append("______________________________________________________________________\n");
txaResults.append("Free Eve/Weekend\t$10.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("Normal\t\t\t $0.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("High Volume\t\t $5.00\t$1.75\t\t$0.00\t\tTotal\n");
txaResults.append("======================================================================\n");
txaResults.append("\n");
}
}
}

If you want me to explain everything, just reply to the thread :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Thanks Leon! You are awesome! I'm having a hard time understanding how to implement things in java. I read the dicumentation , but they never tell you how to do anything. I cannot ever find good examples. So, I never seem to be able to make a method work right.

I have another question, if you don't mind. In the setBounds method, instead of hard-coding the x-coord, I would like to get the Applets' width, then subtract the labels' width, and then divide by 2. I've tried the getWIdth method, but I can't figure out how to do it right. How do I refer to the applet? Can you show me?

Here's the label's code (taken from above):

lblTitle = new Label("Save Long Money!");
lblTitle.setBounds(new Rectangle(60,0,250,24));
lblTitle.setAlignment(Label.CENTER);
lblTitle.setFont(fontTitle);
add(lblTitle,null);

Thanks again Leon,

Greg

 
Hi Greg,

Sure, no problem :)

You can get the width of the applet through the method getWidth(). This method returns an int value, which is the width value of the applet.

Wheras for the label, I don't think you will be able to get the width of it until it is painted onto the panel. So since you set the bounds of the label to the dimension you like, this is what you can do.

public void init()
{
// set the size of the applet
setSize(400,400);

// get the width of the applet
int appletWidth = getWidth();

...

// initialise and set the location for the label
lblTitle = new Label("Save Long Money!");
lblTitle.setBounds(new Rectangle(0,0,250,24));
lblTitle.setAlignment(Label.CENTER);
lblTitle.setFont(fontTitle);
lblTitle.setBounds(new Rectangle((appletWidth-lblTitle.getWidth())/2,0,250,24));
add(lblTitle,null);

...
}

If you realised, I used the method setBounds for lblTitle twice. Reason being the first time is just to get the width of the label. The second setBounds will then set the label to the apropriate location.

In case you don't understand how setBounds works:-
lblTitle.setBounds(new Rectangle(int a, int b, int c, intd));
a - x coordinate on the panel
b - y coordinate on the panel
c - width (in this case, width of the label)
d - height (in this case, height of the label)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Thanks again Leon. Yes, I understand setBounds. I had tried it myself, but it didn't work. I didn't know about the "new Rectangle" part. But I do understand the parameters.

My big problem now is trying to call one class from another class. Once again, everything I read seems to contradict each other. I have 3 classes now, instead of one, and I'm trying to make them communicate with each other.
 
Take your time. Try to understand why the sources you read from "contradicts" each other. I am sure they don't actually contradict but there might be another meaning to it instead.

If you have any problems, just post onto the forum. There will be plenty of people here besides me who will be able to help you solve them :)

Best regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top