Java GUI Help!
Hi, I have a Java GUI exercise that I need to complete and was wondering if I could have a little bit of help... you can see it below.
Create a simple graphical application that will enable a user to perform push, pop and peek operations on a stack and display the resulting stack (using toString) in a text area.
Any help would be much appreciated! Thank you.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: This program uses Java’s built in Stack class. If you are looking for any custom implementation of the stack, I can provide that, just drop a comment.
// StackGUI.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class StackGUI extends JFrame implements ActionListener {
// declaring UI components and the Stack
private JTextField input;
private JButton push, pop, peek;
private JTextArea result;
private Stack<String> stk;
public StackGUI() {
// initializing stack
stk = new Stack<String>();
// using grid layout of 1 column as main layout
setLayout(new GridLayout(0, 1));
// initializing the GUI
// creating a panel for input label and textfield
JPanel inpPanel = new JPanel();
inpPanel.add(new JLabel("Input: "));
input = new JTextField(10);
inpPanel.add(input);
// creating a panel for buttons
JPanel buttonsPanel = new JPanel();
push = new JButton("PUSH");
pop = new JButton("POP");
peek = new JButton("PEEK");
buttonsPanel.add(push);
buttonsPanel.add(pop);
buttonsPanel.add(peek);
// adding action listener to the buttons
push.addActionListener(this);
pop.addActionListener(this);
peek.addActionListener(this);
// initializing text area, making it not editable, setting current value
// of stack as its text
result = new JTextArea();
result.setEditable(false);
result.setText("Stack: " + stk);
// adding all to main frame.
add(inpPanel);
add(buttonsPanel);
add(result);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true); // making window visible
}
public static void main(String[] args) {
new StackGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// resetting text area
result.setText("");
// finding the source of action
if (e.getSource().equals(push)) {
// getting text from input, pushing to stack if it is not empty
String value = input.getText();
if (value.length() > 0) {
stk.push(value);
result.setText("Pushed value: " + value);
// resetting input
input.setText("");
}
} else if (e.getSource().equals(pop)) {
// popping a value from stack and displaying if not empty
if (stk.isEmpty()) {
result.setText("Error! Stack is empty: ");
} else {
result.setText("Popped value: " + stk.pop());
}
} else {
// peeking a value from stack and displaying if not empty
if (stk.isEmpty()) {
result.setText("Error! Stack is empty: ");
} else {
result.setText("Value on top: " + stk.peek());
}
}
// finally appending the contents of stack to result text area
result.append("\nStack: " + stk);
}
}
/*OUTPUT*/
Java GUI Help! Hi, I have a Java GUI exercise that I need to complete and...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...
Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...
In Java please, thank you! Design a GUI application as shown below and perform the following tasks: The text field is for data file name, the file extension must be.txt, otherwise, prompt user to provide a valid file name. Load the content of the data file and display on the textarea. 1. 2. Loading Files File Name: data.tt Load File
(In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...
I already created a doubly linked list class. I need help doing this and adding the doubly linked list class to this. I need this for Java language if someone can please help me Step 2 Stack and Queue Using the linked list class you created in Step 1 create stack and queue classes. Iwill leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to...
Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...
This project is designed to help you practice Graphical User Interfaces (GUI) using Java. Design and write a class needed to simulate a pizza shop ordering system GUI. Your ordering system should allow the user to select whether the order is to be delivered or picked up, the items ordered from the menu and the name of the person making the order. When the user presses the place order button, the display should show the delivery status, the items ordered,...
I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...
JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...