Coding newbie, please help
I need help creating an applet that adds two integers. I have it layed out how I want to, but I dont know where to put the method or how to call it. I know I add some int x= and int y= somewhere, but not sure exactly.
Code:
/* document segment
filename: ActionApplet
author: Walker
date: october.2010
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class addNumbers extends JApplet implements ActionListener
{
/* ----------------------------- declarations */
// color objects
Color white = new Color(255, 255, 255);
Color black = new Color(0, 0, 0);
// components
JLabel inputLabelJLabel;
JTextField inputLabelJTextField;
JLabel outputLabelJLabel;
JTextField outputLabelJTextField;
JLabel finalLabelJLabel;
JTextField finalLabelTextField;
JButton enterJButton;
JButton clearJButton;
// variables
String inputName;
String outputName;
public void init()
{
setLayout(null);
setSize(400, 400);
/* ------------------- initialization */
inputLabelJLabel = new JLabel();
inputLabelJLabel.setBounds(100, 50, 150, 20);
inputLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12));
inputLabelJLabel.setText("Enter First Integer");
inputLabelJLabel.setForeground(black);
inputLabelJLabel.setHorizontalAlignment(JLabel.LEFT);
add(inputLabelJLabel);
inputLabelJTextField = new JTextField();
inputLabelJTextField.setBounds(230, 50, 150, 20);
inputLabelJTextField.setFont(new Font("Default", Font.PLAIN, 12));
inputLabelJTextField.setHorizontalAlignment(JTextField.CENTER);
inputLabelJTextField.setForeground(black);
inputLabelJTextField.setBackground(white);
inputLabelJTextField.setEditable(true);
add(inputLabelJTextField);
outputLabelJLabel = new JLabel();
outputLabelJLabel.setBounds(100, 80, 150, 20);
outputLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12));
outputLabelJLabel.setText("Enter Second Interger");
outputLabelJLabel.setForeground(black);
outputLabelJLabel.setHorizontalAlignment(JLabel.LEFT);
add(outputLabelJLabel);
outputLabelJTextField = new JTextField();
outputLabelJTextField.setBounds(230, 80, 150, 20);
outputLabelJTextField.setFont(new Font("Default", Font.PLAIN, 12));
outputLabelJTextField.setHorizontalAlignment(JTextField.CENTER);
outputLabelJTextField.setForeground(black);
outputLabelJTextField.setBackground(white);
outputLabelJTextField.setEditable(true);
add(outputLabelJTextField);
finalLabelJLabel = new JLabel();
finalLabelJLabel.setBounds(100, 110, 150, 20);
finalLabelJLabel.setFont(new Font("Default", Font.PLAIN, 12));
finalLabelJLabel.setText("Addition Results");
finalLabelJLabel.setForeground(black);
finalLabelJLabel.setHorizontalAlignment(JLabel.LEFT);
add(finalLabelJLabel);
finalLabelTextField = new JTextField();
finalLabelTextField.setBounds(230, 110, 150, 20);
finalLabelTextField.setFont(new Font("Default", Font.PLAIN, 12));
finalLabelTextField.setHorizontalAlignment(JTextField.CENTER);
finalLabelTextField.setForeground(black);
finalLabelTextField.setBackground(white);
finalLabelTextField.setEditable(false);
add(finalLabelTextField);
enterJButton = new JButton();
enterJButton.setBounds(100, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
add(enterJButton);
enterJButton.addActionListener(this);
clearJButton = new JButton();
clearJButton.setBounds(210, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
add(clearJButton);
clearJButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
/* the following lines of code are commented out,
remove the // to un-comment after the components
are initialized */
if(obj == enterJButton)
{
getFirstMethod();
}
else if(obj == clearJButton)
{
clearAll();
}
}
public void getFirstMethod()
{
}
public void getOutput()
{
outputLabelJTextField.setText("" + inputName);
}
public void clearAll()
{
inputLabelJTextField.setText("");
inputLabelJTextField.requestFocusInWindow();
outputLabelJTextField.setText("");
}
}
Re: Coding newbie, please help
Quote:
I have it layed out how I want to
Have you written all of this code or is this the code your teacher gave to you to complete?
Quote:
but I dont know where to put the method or how to call it
What method? You might know what you are trying to do but we don't.
Please explain exactly what this method is supposed to do so we can help you.