Question

Create a swing application to get following gui screen. All gui elements in the JFrame are JButton. You are expected to write

with java programming language

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

//Java Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

public class SwingApplication extends JFrame {
//Create JButtons for GUI
JButton[] buttons = new JButton[12];
//Values for buttons to be updated
//and initialize it with values
private static int[] values = new int[]{0,1,2,3,4,5,6,7};
//ascii values for buttons to be updated
private static char[] asciiValue = new char[]{'a','b','c','d'};
//Constructor
public SwingApplication()
{

//set Frame
setLayout(new GridLayout(0,3,10,10));
setTitle("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
//JPanel
JPanel panel = new JPanel(new FlowLayout());
//Initialize JButtons for numbers
buttons[0] = new JButton("0");
buttons[1] = new JButton("1");
buttons[2] = new JButton("2");
buttons[3] = new JButton("3");
buttons[4] = new JButton("4");
buttons[5] = new JButton("5");
buttons[6] = new JButton("6");
buttons[7] = new JButton("7");

//Initialize JButtons for characters
buttons[8] = new JButton("a");
buttons[9] = new JButton("b");
buttons[10] = new JButton("c");
buttons[11] = new JButton("d");
//add JButtons to JPanels
panel.add(buttons[8]);
panel.add(buttons[9]);
panel.add(buttons[10]);
panel.add(buttons[11]);

//Add to JFrame
getContentPane().add(buttons[0]);
getContentPane().add(buttons[1]);
getContentPane().add(buttons[2]);
getContentPane().add(buttons[3]);
//Add panel to frame
getContentPane().add(panel);
getContentPane().add(buttons[4]);
getContentPane().add(buttons[5]);
getContentPane().add(buttons[6]);
getContentPane().add(buttons[7]);

//Set frame to visible
setVisible(true);
//register listener
buttons[0].addActionListener(new actionHandler());
buttons[1].addActionListener(new actionHandler());
buttons[2].addActionListener(new actionHandler());
buttons[3].addActionListener(new actionHandler());
buttons[4].addActionListener(new actionHandler());
buttons[5].addActionListener(new actionHandler());
buttons[6].addActionListener(new actionHandler());
buttons[7].addActionListener(new actionHandler());
buttons[8].addActionListener(new actionHandler());
buttons[9].addActionListener(new actionHandler());
buttons[10].addActionListener(new actionHandler());
buttons[11].addActionListener(new actionHandler());

}
//Action Listener handler buttons
private class actionHandler implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e) {
//For JButton 0
if(e.getSource().equals(buttons[0]))
{
values[0]+=1;
buttons[0].setText(Integer.toString(values[0]));
}
//For JButton 1
if(e.getSource().equals(buttons[1]))
{
values[1]+=1;
buttons[1].setText(Integer.toString(values[1]));
}
//For JButton 2
if(e.getSource().equals(buttons[2]))
{
values[2]+=1;
buttons[2].setText(Integer.toString(values[2]));
}
//For JButton 3
if(e.getSource().equals(buttons[3]))
{
values[3]+=1;
buttons[3].setText(Integer.toString(values[3]));
}
//For JButton 4
if(e.getSource().equals(buttons[4]))
{
values[4]+=1;
buttons[4].setText(Integer.toString(values[4]));
}
//For JButton 5
if(e.getSource().equals(buttons[5]))
{
values[5]+=1;
buttons[5].setText(Integer.toString(values[5]));
}
//For JButton 6
if(e.getSource().equals(buttons[6]))
{
values[6]+=1;
buttons[6].setText(Integer.toString(values[6]));
}
//For JButton 7
if(e.getSource().equals(buttons[7]))
{
values[7]+=1;
buttons[7].setText(Integer.toString(values[7]));
}
//For JButton a
if(e.getSource().equals(buttons[8]))
{
asciiValue[0]+=1;
buttons[8].setText(Character.toString(asciiValue[0]));
}
//For JButton b
if(e.getSource().equals(buttons[9]))
{
asciiValue[1]+=1;
buttons[9].setText(Character.toString(asciiValue[1]));
}
//For JButton c
if(e.getSource().equals(buttons[10]))
{
asciiValue[2]+=1;
buttons[10].setText(Character.toString(asciiValue[2]));
}
//For JButton d
if(e.getSource().equals(buttons[11]))
{
asciiValue[3]+=1;
buttons[11].setText(Character.toString(asciiValue[3]));
}


}
}
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
new SwingApplication();
}
});
}
}

//Output

- x 2 3 с d 4 5 6 7 8

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

Add a comment
Know the answer?
Add Answer to:
with java programming language Create a swing application to get following gui screen. All gui elements...
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
  • I want to extend this GUI application in java language. Thanks for the help in advance. import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.Action...

    I want to extend this GUI application in java language. Thanks for the help in advance. import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class FormWindow {     private JFrame frame;     /**      * Launch the application.      */     public static void main(String[] args) {         EventQueue.invokeLater(new Runnable() {             public void run() {                 try {                     FormWindow window = new FormWindow();                     window.frame.setVisible(true);                 } catch (Exception e) {                     e.printStackTrace();                 }             }         });     }     /**      * Create the application.      */     public FormWindow() {         initialize();    ...

  • Please code the following Java App using Swing No additional classes are required. With that in...

    Please code the following Java App using Swing No additional classes are required. With that in mind ... Write a program that emulates a calculator. Create a Label with a title of "Super Calculator", a textbox, where the numbers are displayed, a series of buttons labeled as shown, and a “Clear” button. As the user enters the numbers, pressing the number buttons, display the numbers right justified to the textbox. When the user press any one of the operators, +,...

  • JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The...

    JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The interface components will appear centered in the interface, and in two rows. o Row one will have a Label that reads “Please enter a valid integer:” and a Text Field to take in the integer from the user. o Row two will have a Button that when pressed converts the integer to binary  The conversion will be completed using recursion – A separate...

  • 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...

  • 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...

  • 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************************************...

  • Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help me...

    Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help menu includes two menu items: 1. About 2. Visit Homepage. Add a separator between these menu items. 1. Create a menu item which is About. Add a short cut for the menu item. It is ctrl+A. -When a user clicks it (an action event occurs), display a show message dialog box. Display the message shown in the figure. Display information icon. 2. Create a menu...

  • Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event...

    Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event handler inner that extends WidgetViewerActionEvent it has instance variables StringSet sSet JTextField inputStr JLabel numStr JLabel numChar In the constructor, create a WidgetViewer object create sSet create a local variable JLabel prompt initialized to "Enter a String" create inputStr with some number of columns create a local variable JButton pushMe initialized to "Push to include String" create numStr initialized to "Number of Strings: 0"...

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area...

    Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area in a Frame. Also add a pull-down menu with a menu item "Load". Copy the class as the answer to this part. (b) Create another class TestHexEditor with a main() method which creates an object anEditor of the class HexEditor and displays the frame in part (a) using setVisible(true). Copy the class as the answer to this part. (c) Make changes to the class...

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