Question

Please design a Java GUI application with two JTextField for user to enter the first name...

Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in this program, another class file nameFrame$actionhandler.class was generated when you compile the .java file, you need both files in the same directory in order to run) to see the sample output. You need to design your program with a similar look and have your name as the author in a JLabel.

nameFrame.class:

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class nameFrame
  extends JFrame
{
  private JTextField txtFirstName;
  private JTextField txtLastName;
  private JTextField txtFullName;
  private JLabel lblFirstName;
  private JLabel lblLastName;
  private JLabel lblFullName;
  private JButton btnName;
  private JButton btnClear;
  private JLabel lblAuthor;
  
  public nameFrame()
  {
    super("Enter names");
    setLayout(new FlowLayout());
    
    lblFirstName = new JLabel("First Name:");
    add(lblFirstName);
    txtFirstName = new JTextField(10);
    add(txtFirstName);
    
    lblLastName = new JLabel("Last Name:");
    add(lblLastName);
    txtLastName = new JTextField(10);
    add(txtLastName);
    
    txtFullName = new JTextField(20);
    add(txtFullName);
    txtFullName.setBackground(Color.WHITE);
    txtFullName.setEditable(false);
    


    btnName = new JButton("Name");
    add(btnName);
    btnClear = new JButton("Clear");
    add(btnClear);
    
    lblAuthor = new JLabel("Author: James Gao");
    add(lblAuthor);
    lblAuthor.setBackground(Color.gray);
    
    nameFrame.actionhandler localActionhandler = new nameFrame.actionhandler(this);
    btnName.addActionListener(localActionhandler);
    btnClear.addActionListener(localActionhandler);
  }
  














  public static void main(String[] paramArrayOfString)
  {
    nameFrame localNameFrame = new nameFrame();
    localNameFrame.setDefaultCloseOperation(3);
    localNameFrame.setSize(400, 150);
    localNameFrame.setVisible(true);
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java Code

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class nameFrame
        extends JFrame
{
    private JTextField txtFirstName;
    private JTextField txtLastName;
    private JTextField txtFullName;
    private JLabel lblFirstName;
    private JLabel lblLastName;
    private JLabel lblFullName;
    private JButton btnName;
    private JButton btnClear;
    private JLabel lblAuthor;

    public nameFrame()
    {
        super("Enter names");
        setLayout(new FlowLayout());

        lblFirstName = new JLabel("First Name:");
        add(lblFirstName);
        txtFirstName = new JTextField(10);
        add(txtFirstName);

        lblLastName = new JLabel("Last Name:");
        add(lblLastName);
        txtLastName = new JTextField(10);
        add(txtLastName);

        txtFullName = new JTextField(20);
        add(txtFullName);
        txtFullName.setBackground(Color.WHITE);
        txtFullName.setEditable(false);



        btnName = new JButton("Name");
        add(btnName);
        btnClear = new JButton("Clear");
        add(btnClear);

        lblAuthor = new JLabel("Author: James Gao");
        add(lblAuthor);
        lblAuthor.setBackground(Color.gray);

        nameFrame.actionhandler localActionhandler = new nameFrame.actionhandler(this);
        btnName.addActionListener(localActionhandler);
        btnClear.addActionListener(localActionhandler);
    }


    public static void main(String[] paramArrayOfString)
    {
        nameFrame localNameFrame = new nameFrame();
        localNameFrame.setDefaultCloseOperation(3);
        localNameFrame.setSize(400, 150);
        localNameFrame.setVisible(true);
    }

    private class actionhandler implements ActionListener {
        public actionhandler(nameFrame nameFrame) {
            
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource().equals(btnClear))
            {
                txtFirstName.setText("");
                txtLastName.setText("");
                txtFullName.setText("");
            }
            if(e.getSource().equals(btnName))
            {
                String fname = txtFirstName.getText();
                String lname = txtLastName.getText();
                txtFullName.setText(fname+" "+lname);
            }
        }
    }
}

//Output

0 X Enter names First Name: John - Last Name: Wick Name John Wick Clear Author: James Gao

//If you need any help regarding this solution ...........please leave a comment...... thanks

Add a comment
Know the answer?
Add Answer to:
Please design a Java GUI application with two JTextField for user to enter the first name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • Write a Java application that displays the following ClickMe When the user clicks on "Click Me"...

    Write a Java application that displays the following ClickMe When the user clicks on "Click Me" button, the text, "l am clicked" is displayed. ClickMe am clicked import java.awt.*; import java.awt.event.*; public class OneButton extends JFrame implements ActionListener private JButton button; private JTextField field; public static void main(String args)( OneButton myCoolButton new OneButton0;

  • 22. Fill in the codes that creates a Java GUI application that contains a label and...

    22. Fill in the codes that creates a Java GUI application that contains a label and a button. The label reads "Click the button to change the background color." When the button is clicked, the background is changed to a red color. The program produces the following output. Point 7 22.1 package javaapplication 179; import javax.swing": import java.ant": import java.awt.exst. public class ColorChange extends JFrame / not allowed to change JLabel label - new JLabel("Click the button to change the...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • So I am having probelms with figuring out how to do this program the part in...

    So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to do. This is in JAVA. What I have so far is shown below the question, this is right upto what I have to do next. Choose Your Operation Write a program that uses a WidgetViewer object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT