Question

Lab14 10:30 Write a program similar to the following figure, which allow user to add name and draw the added names in the cenPlease use Java to solve this quesition. Thx.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//DisplayNames.java

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class DisplayNames extends JFrame {

   private JPanel contentPane;
   private JTextField textField;
   static ArrayList<String> names = new ArrayList();
   static int x=60;
static int y=60;
   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                     

                   DisplayNames frame = new DisplayNames();
                  
                   DrawPanel drawPanel = new DrawPanel(names,x,y);
                   drawPanel.setSize(500,500);
                   frame.getContentPane().add(drawPanel);
                   frame.setTitle("Lab 15 of Summer17");
                  
                   frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   public DisplayNames() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 687, 441);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);
      
       JLabel lblInputAName = new JLabel("Input a name:");
       lblInputAName.setFont(new Font("Tahoma", Font.PLAIN, 22));
       lblInputAName.setBounds(40, 312, 146, 41);
       contentPane.add(lblInputAName);
      
       textField = new JTextField();
       textField.setFont(new Font("Tahoma", Font.PLAIN, 22));
       textField.setBounds(185, 314, 229, 41);
       contentPane.add(textField);
       textField.setColumns(10);
      
       JButton btnAddName = new JButton("Add Name");
       btnAddName.setFont(new Font("Tahoma", Font.PLAIN, 22));
       btnAddName.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               names.add(textField.getText());
               repaint();
               textField.setText("");
           }
       });
       btnAddName.setBounds(427, 314, 146, 41);
       contentPane.add(btnAddName);
      
       JPanel panel = new JPanel();
       panel.setBounds(500, 500, 496, 244);
       contentPane.add(panel);

   }
}
class DrawPanel extends JPanel {
  
   ArrayList<String> InputNames=new ArrayList<String>();
   int xPos;
   static int yPos;
  
   public DrawPanel(ArrayList<String> names, int x, int y)
   {
       this.InputNames=names;
       this.xPos=x;
       DrawPanel.yPos=y;
   }
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
  
Font font=new Font("Arial",0, 22);
   g.setFont(font);

for(int i=0;i<InputNames.size();i++)
{       
   g.drawString(InputNames.get(i),xPos,yPos + i*30);
     
}
     
}
}

//Output

X Lab 15 of Summer17 Tom Hanks Frank Jim Mike Smith Input a name: Add Name

Add a comment
Know the answer?
Add Answer to:
Please use Java to solve this quesition. Thx. Lab14 10:30 Write a program similar to the...
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
  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts 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");...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own...

    WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own class in a program To write a constructor with parameters, an method and a toString) method. To use ArrayList To use a for-each loo . p *l . To use text file for output 2 The Context This is a single-class project, the first for which I have not given you code to start from. It is not realistic, or useful, but it gives...

  • USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...

    USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in a number n and then reads n strings. it then reads a string s from the user and gives you the word that was typed in after s. Example input: 4 Joe Steve John Mike Steve Example output: John Example input: 7 Up Down Left Right North South East Right Example output: North 2. Remember to call your class Program Question 5.2 1. Write...

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