Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:25:44+00:00 2026-06-12T19:25:44+00:00

I’m trying to write a program that will take text in a JTextField and

  • 0

I’m trying to write a program that will take text in a JTextField and put it into variables I’ve declared when I press a JButton. I need to calculate weekly pay for a school project, but that only requires the console, I’m doing the GUI for my own fun. I’m trying to get it so when I hit ‘calc’ it’ll take the imputs from id, rh, oh, hp, etc and calculate weekly pay (wp), which will then be printed on the right column next to the calc button.

//the calculations aren't complete yet until I finish the GUI

public class Weekly_Pay 
{

public static void calculations(String[] args) 
{

Scanner imput = new Scanner(System.in);

System.out.println("ID number: ");
int employeeId = imput.nextInt();

System.out.println("Hourly Wage: ");
Double hourlyWage = imput.nextDouble();

System.out.println("Regular Hours: ");
double regularHours = imput.nextDouble();

System.out.println("Overtime Hours: ");
double overtimeHours = imput.nextDouble();

double overtimePay = round(overtimeHours * (1.5 * hourlyWage));
double regularPay  = round(hourlyWage * regularHours);

double weeklyPay = regularPay + overtimePay;

System.out.println("Employee ID Number:" + employeeId);
System.out.printf("Weekly Pay: " + "$%.2f\n", weeklyPay);

}

public static double round(double num) 
{

// rounding to two decimal places
num *= 100;
int rounded = (int) Math.round(num);
return rounded/100.0;

}


public static void main(String[] args) 
{

JFrame window = new JFrame();
window.setTitle("Weekly Pay");
window.setSize(350, 200);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color lGray = new Color(209, 209, 209);

JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setBackground(lGray);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

JTextField idEntry = new JTextField(); //where the user imputs their ID
JTextField hwEntry = new JTextField(); //where the user imputs their hourly wage
JTextField rhEntry = new JTextField(); //where the user imputs their regular hours
JTextField ohEntry = new JTextField(); //where the user imputs their overtime hours

JLabel id = new JLabel("ID Number");
JLabel hw = new JLabel("Hourly Wage");
JLabel rh = new JLabel("Regular Hours");
JLabel oh = new JLabel("Overtime Hours");
JButton calc = new JButton("Calculate");
JLabel wp = new JLabel(" Weekly Pay: $" + "$%.2f\n", weeklyPay);

GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();    
hGroup.addGroup(layout.createParallelGroup().
           addComponent(id).addComponent(hw).addComponent(rh).addComponent(oh).addComponent(calc));
hGroup.addGroup(layout.createParallelGroup().
  addComponent(idEntry).addComponent(hwEntry).addComponent(rhEntry).addComponent(ohEntry).addComponent(wp));
layout.setHorizontalGroup(hGroup);

GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();    
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(id).addComponent(idEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(hw).addComponent(hwEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(rh).addComponent(rhEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(oh).addComponent(ohEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(calc).addComponent(wp));
layout.setVerticalGroup(vGroup);

window.add(panel);
window.setVisible(true);

}  
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:25:46+00:00Added an answer on June 12, 2026 at 7:25 pm

    for example:

    String input = new String();         
    JButton mbutt = new JButton;
    JTextField jtxt = new JTextField();
    
    mbutt.addActionListener(new ActionListener(){
    
           public void actionPerformed(ActionEvent event){    
                 input = jtxt.getText().toString();
           }
     });
    

    ////////////////////////////////// Edited Part //////////////////////////////

    Now few things before i jump into the code.

    – I just wanted to show the working of ActionListener, and how to extract a data from a field and put it into a variable.

    – Its a bad practice to directly put the component on the JFrame, and thats exactly what i have done here (too bad of me..!!!), so You should always use something like a JPanel over the JFrame, and then place the component over it. In order to keep it Simple i have Deliberately use direct JFrame to hold the components.

    – And yes, Its always a very good practice to have the UI work on the UI thread, and Non-UI work on Non-UI thread.

    – In Swings main() method is Not long lived, after scheduling the construction of GUI in the Event Dispatcher Thread it exits… So now its the responsibility of EDT to handle the GUI, so you should keep the EDT for handling the GUI only, as i have done it in the main() method [EventQueue.invokeLater()].

    Full Code:

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class Tes extends JFrame {
    
        String input;
        JTextField jtxt; 
        JButton mbutt; 
    
    
        public Tes(){
    
     //--ALWAYS USE A JPANEL OVER JFRAME, I DID THIS TO KEEP IT SIMPLE FOR U--//
    
            this.setSize(400,400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            this.setComponent();
            this.setHandler();
        }
    
        public void setComponent(){
    
            jtxt =  new JTextField("Hello");
    
            mbutt = new JButton("Button"); 
    
            this.add(BorderLayout.SOUTH,mbutt);
    
            this.add(BorderLayout.NORTH,jtxt);
    
        }
    
        public void setHandler(){
    
            mbutt.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
    
                    input = jtxt.getText().toString();
    
                    System.out.println("Input Value: "+input);
    
              **//--See your Console Output everytime u press the button--//**
    
                }
            });
    
        }
        public static void main(String[] args){
    
    
             EventQueue.invokeLater(new Runnable(){
    
                @Override
                public void run() {
    
                    Tes t = new Tes();
                    t.setVisible(true);
    
                }
    
    
    
             });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.