d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help W/ Simple Java Project > Please Help.
Add Reply New Topic New Poll
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 21 2016 09:32pm
Okay so the project is simple.

3x lines with editable text frames.

.2 tax rate.

line 1 is income
line 2 is an uneditable string "tax rate" set at 20%
line 3 is net income

This is the code I have so far and it does not even create a window. What am I doing wrong?

/*
program:OutputFormatting
name: msullivan
date: february.2012
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class IncomeTax extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);

DecimalFormat currency;
DecimalFormat percentage;

public IncomeTax()
{
createUserInterface();
}

public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);

/* initialize components section */

setTitle("IncomeTax");
setSize(400, 600);
setVisible(true);
}

JLabel IncomeJLabel;
nameJLabel = IncomeJLabel();
nameJLabel.setBounds(10, 15, 12, 8);
nameJLabel.setFont(new Font("Default", Font.PLAIN, 12));
nameJLabel.setText("");
nameJLabel.setForeground(black);
nameJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(IncomeJLabel);
JTextField IncomeJTextField;
nameJTextField = new JTextField();
nameJTextField.setBounds(0, 0, 0, 0);
nameJTextField.setFont(new Font("Default", Font.PLAIN, 12));
nameJTextField.setHorizontalAlignment(JTextField.CENTER);
nameJTextField.setForeground(black);
nameJTextField.setBackground(white);
nameJTextField.setEditable(false);
contentPane.add(nameJTextField);
JButton nameJButton;
nameJButton = new JButton();
nameJButton.setBounds(0, 0, 0, 0);
nameJButton.setFont(new Font("Default", Font.PLAIN, 12));
nameJButton.setText("");
nameJButton.setForeground(black);
nameJButton.setBackground(white);
contentPane.add(nameJButton);
nameJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
nameJButtonActionPerformed(event);
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 21 2016 09:38pm
Quote
This is the code I have so far and it does not even create a window. What am I doing wrong?


what does your main method look like? you've got some copy/paste issue going on. i see a lot of code that isn't in a method. use some [ code] blocks

This post was edited by carteblanche on Sep 21 2016 09:41pm
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 21 2016 10:04pm
Quote (carteblanche @ Sep 21 2016 10:38pm)
what does your main method look like? you've got some copy/paste issue going on. i see a lot of code that isn't in a method. use some [ code] blocks


Okay so I have the 3 text boxes I need.

I'm having difficulty making the text line up properly.

For example:
//initialize components
firstNumberJLabel = new JLabel();
firstNumberJLabel.setBounds(75, 50, 150, 20);
firstNumberJLabel.setFont(new Font("Default", Font.PLAIN, 12));
firstNumberJLabel.setText("Enter Gross Income");
firstNumberJLabel.setForeground(black);
firstNumberJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(firstNumberJLabel);

firstNumberJTextField = new JTextField();
firstNumberJTextField.setBounds(225, 50, 50, 20);
firstNumberJTextField.setFont(new Font("Default", Font.PLAIN, 12));
firstNumberJTextField.setHorizontalAlignment(JTextField.CENTER);
firstNumberJTextField.setForeground(black);
firstNumberJTextField.setBackground(white);
firstNumberJTextField.setEditable(true);
contentPane.add(firstNumberJTextField);


The text "Enter Gross Income: is starting to the right of all the letters below:

secondNumberJLabel = new JLabel();
secondNumberJLabel.setBounds(75, 80, 100, 20);
secondNumberJLabel.setFont(new Font("Default", Font.PLAIN, 12));
secondNumberJLabel.setText("Amount of Tax");
secondNumberJLabel.setForeground(black);
secondNumberJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(secondNumberJLabel);

secondNumberJTextField = new JTextField();
secondNumberJTextField.setBounds(225, 80, 50, 20);
secondNumberJTextField.setFont(new Font("Default", Font.PLAIN, 12));
secondNumberJTextField.setHorizontalAlignment(JTextField.CENTER);
secondNumberJTextField.setForeground(black);
secondNumberJTextField.setBackground(white);
secondNumberJTextField.setEditable(true);
contentPane.add(secondNumberJTextField);



So essentially

Enter Gross Income(TEXT BOX IS HERE FOR INPUT) This actually is about 2 tabs to the right
Amount of tax(TEXT BOX HERE) About one tab to the right
Tax Rate(TEXT BOX HERE) Aligned properly on the box.

Is how it's appearing inside of the window.

The actual input boxes are aligned properly, however.

How do I make the text line up properly as well?

This post was edited by nikkilina on Sep 21 2016 10:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 21 2016 10:09pm
Quote (nikkilina @ Sep 22 2016 12:04am)
So essentially

Enter Gross Income(TEXT BOX IS HERE FOR INPUT) This actually is about 2 tabs to the right
Amount of tax(TEXT BOX HERE) About one tab to the right
Tax Rate(TEXT BOX HERE) Aligned properly on the box.

Is how it's appearing inside of the window.

The actual input boxes are aligned properly, however.

How do I make the text line up properly as well?


you need to start using [code ] tags when spacing is important. is this what you're talking about?
Code

Enter Gross Income(TEXT BOX IS HERE FOR INPUT)
Amount of tax(TEXT BOX HERE)
Tax Rate(TEXT BOX HERE)


regardless, you should look into swing layouts. i find them kinda annoying to use tbh.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

i'm headin' off now though. use some code tags and i'll look at it tomorrow night.

This post was edited by carteblanche on Sep 21 2016 10:10pm
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 21 2016 10:12pm
Quote (nikkilina @ Sep 21 2016 11:04pm)
Okay so I have the 3 text boxes I need.

I'm having difficulty making the text line up properly.

For example:
//initialize components
firstNumberJLabel = new JLabel();
firstNumberJLabel.setBounds(75, 50, 150, 20);
firstNumberJLabel.setFont(new Font("Default", Font.PLAIN, 12));
firstNumberJLabel.setText("Enter Gross Income");
firstNumberJLabel.setForeground(black);
firstNumberJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(firstNumberJLabel);

firstNumberJTextField = new JTextField();
firstNumberJTextField.setBounds(225, 50, 50, 20);
firstNumberJTextField.setFont(new Font("Default", Font.PLAIN, 12));
firstNumberJTextField.setHorizontalAlignment(JTextField.CENTER);
firstNumberJTextField.setForeground(black);
firstNumberJTextField.setBackground(white);
firstNumberJTextField.setEditable(true);
contentPane.add(firstNumberJTextField);


The text "Enter Gross Income: is starting to the right of all the letters below:

secondNumberJLabel = new JLabel();
secondNumberJLabel.setBounds(75, 80, 100, 20);
secondNumberJLabel.setFont(new Font("Default", Font.PLAIN, 12));
secondNumberJLabel.setText("Amount of Tax");
secondNumberJLabel.setForeground(black);
secondNumberJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(secondNumberJLabel);

secondNumberJTextField = new JTextField();
secondNumberJTextField.setBounds(225, 80, 50, 20);
secondNumberJTextField.setFont(new Font("Default", Font.PLAIN, 12));
secondNumberJTextField.setHorizontalAlignment(JTextField.CENTER);
secondNumberJTextField.setForeground(black);
secondNumberJTextField.setBackground(white);
secondNumberJTextField.setEditable(true);
contentPane.add(secondNumberJTextField);



So essentially

Enter Gross Income(TEXT BOX IS HERE FOR INPUT) This actually is about 2 tabs to the right
Amount of tax(TEXT BOX HERE) About one tab to the right
Tax Rate(TEXT BOX HERE) Aligned properly on the box.

Is how it's appearing inside of the window.

The actual input boxes are aligned properly, however.

How do I make the text line up properly as well?


And the final question:

// main method
public static void main(String[] args)
{
AddNumbers application = new AddNumbers();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void enterJButtonActionPerformed(ActionEvent event)
{
// get input values
firstNumber = Integer.parseInt(firstNumberJTextField.getText());
secondNumber = Integer.parseInt(secondNumberJTextField.getText());

// calculate values
resultNumber = firstNumber + secondNumber;

// display results
resultNumberJTextField.setText("" + resultNumber);
}

public void clearJButtonActionPerformed(ActionEvent event)
{
firstNumberJTextField.setText("");
firstNumberJTextField.requestFocusInWindow();
secondNumberJTextField.setText("");
resultNumberJTextField.setText("");
}

}

This is the code for adding numbers.

What I need this program to do is 3 things:

There is a static string tax rate of .2

Gross Income firstNumber is to be multiplied by static string .2
The second box, secondNumber is the amount of tax. I.E 60,000 would = 12000 in this box.
The third box is Net Income
AKA 48,000 in this example.

So it would be firstNumber*.2 to produce secondNumber; then it would be firstNumber - secondNumber to produce thirdNumber which is net income. What do I need to do? Anyone have resources for this particular subject?

This is what I have so far to calculate the first and 3rd numbers:

// set properties of application’s window
setTitle("Add Numbers"); // set title bar text
setSize(400, 400); // set window size
setVisible(true); // display window
}

// main method
public static void main(String[] args)
{
AddNumbers application = new AddNumbers();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void enterJButtonActionPerformed(ActionEvent event)
{
// get input values
firstNumber = Double.parseDouble(firstNumberJTextField.getText());
secondNumber = Double.parseDouble(secondNumberJTextField.getText());

// calculate values
resultNumber = firstNumber * .2;
// display results
resultNumberJTextField.setText("" + resultNumber);
}

public void clearJButtonActionPerformed(ActionEvent event)
{
firstNumberJTextField.setText("");
firstNumberJTextField.requestFocusInWindow();
secondNumberJTextField.setText("");
resultNumberJTextField.setText("");
}

}

The compiler finds no errors but when I try to run it the program doesnt work

This post was edited by nikkilina on Sep 21 2016 10:35pm
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 21 2016 10:41pm
More info:
// main method
public static void main(String[] args)
{
AddNumbers application = new AddNumbers();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void enterJButtonActionPerformed(ActionEvent event)
{
// get input values
firstNumber = Double.parseDouble(firstNumberJTextField.getText());
secondNumber = Double.parseDouble(secondNumberJTextField.getText());
// calculate values
secondNumber = resultNumber - firstNumber;
resultNumber = firstNumber * .2;
// display results
secondNumberJTextField.setText("" + secondNumber);
resultNumberJTextField.setText("" + resultNumber);
}

public void clearJButtonActionPerformed(ActionEvent event)
{
firstNumberJTextField.setText("");
firstNumberJTextField.requestFocusInWindow();
secondNumberJTextField.setText("");
resultNumberJTextField.setText("");
}

}

I am getting the third and first numbers to work, but the secondNumber which = firstNumber - thirdNumber if not actually written inside of will make the program not work
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll