Question

In: Computer Science

Write the code in Java: 1. Create a method that displays your name in the console....

Write the code in Java:

1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press.

2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure to trim() the text.

Solutions

Expert Solution


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ButtonClick {  
static    JFrame frame = new JFrame("JFrame Example");  
        public static void main(String s[]) {  
        JPanel panel = new JPanel();  
        panel.setLayout(null);  
        
        JButton convert = new JButton();  
        convert.setText("Click");  
        convert.setBounds(200, 150, 100, 20);
        convert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                printNameToConsole();
            }

                        private void printNameToConsole() {
                                System.out.println("Uday");
                        }
        });
        panel.add(convert);
        
        frame.add(panel);
        frame.setSize(600, 300);  
        frame.setLocationRelativeTo(null);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  
    }  
}  

Answer 2:




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ReverseStringUI {  
static    JFrame frame = new JFrame("JFrame Example");  
static JTextField str= new JTextField(10);

        public static void main(String s[]) {  
        JPanel panel = new JPanel();  
        panel.setLayout(null);  
        str.setBounds(80, 50, 100, 30);
        JButton convert = new JButton();  
        convert.setText("Print");  
        convert.setBounds(80, 150, 100, 20);

        convert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                printName(str.getText());
            }

                        private void printName(String aText) {
                                System.out.println(aText);
                        }
        });
        panel.add(str);
        panel.add(convert);
        frame.add(panel);
        frame.setSize(300, 300);  
        frame.setLocationRelativeTo(null);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  
    }  
}  

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

with java code write The method prints a meaningful title with your name as the author....
with java code write The method prints a meaningful title with your name as the author. The method then generates THREE random numbers 0 - 4 and reports how many of the numbers are the same. 2-Use a loop to repeat 10 times. the output should be like this Title title title by Your Name 4, 3, 4 two are the same 4, 0, 0 two are the same . . . 2, 0, 4 none are the same 4,...
Write a java code that allows the user to input any word/name from the console (10...
Write a java code that allows the user to input any word/name from the console (10 words/names only) and then sorts and alphabetizes them into an array without using predefined methods (.compareTo(), or any sorter functions) and then print out the results. you must have three methods that can: Compare multiple strings Swap elements Sort and alphabetize string ***REMEMBER DO NOT USE ANY PRE-DEFINED METHODS*** Example of how the code should work: Enter names/words (10 only): "james, john, alex, aaron,...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Write a recursive method that displays an integer value reversely on the console using the following...
Write a recursive method that displays an integer value reversely on the console using the following header: public static void reverseDisplay(int value) For example, reverseDisplay(12345) displays 54321. Write a test program that prompts the user to enter an integer, invokes the method above, and displays its reversal.
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name and print it out. 2) Create an uninitialized char array and copy into it      the char array containing your full name and print it out. 3) Create a Character array and copy into it the char array containing      your full name and print it out. 4) Into the Character array, use toUpperCase() to copy the char array containing     your full name...
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
Create a program in JAVA that displays a design or picture for someone in your quarantine...
Create a program in JAVA that displays a design or picture for someone in your quarantine household/group: a pet, a parent, a sibling, a friend. Make them a picture using the tools in TurtleGraphics and run your program to share it with them! Use a pen object, as well as any of the shape class objects to help you create your design. You must use and draw at least 5 shape objects. - You must use a minimum of 4...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT