Question

WRITE a program in JAVA using GRAPHICAL USER INTERFACE to make fruit and vegetable display panel with a toggle switch.(m...

WRITE a program in JAVA using GRAPHICAL USER INTERFACE to make fruit and vegetable display panel with a toggle switch.(make two displays panel 1 for fruit and the other for vegetables , if we press the toggle switch then the 1st panel will show and if we again press the switch then the 1st will hide and other will show) and vice versa.

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

//pls save it as PanelShowHide.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class PanelShowHide extends JFrame implements ItemListener {

JToggleButton button = new JToggleButton("ON"); //create toggle button
JPanel pFruits = new JPanel();       //create fruit panel
JPanel pVegitables = new JPanel();   //create vegitable panel

PanelShowHide() {
setLayout(new FlowLayout()); //set frame layout as flowlayout
setTitle("Fruit Vegitables"); //set frame title

//pFruits.setLayout(new GridLayout(3, 1)); //set panel's layout as grid if you want labels vertically added
//add 3 labels to fruits panel
       pFruits.add(new JLabel("Apple"));
       pFruits.add(new JLabel("Banana"));
       pFruits.add(new JLabel("Cherry"));

//pVegitables.setLayout(new GridLayout(3, 1)); //set panel's layout as grid if you want labels vertically added
//add 3 labels to vegitables panel
       pVegitables.add(new JLabel("Potato"));
       pVegitables.add(new JLabel("Tomato"));
       pVegitables.add(new JLabel("Onion"));

pFruits.setVisible(true);         //show fruits panel
pVegitables.setVisible(false);   //hide vegitables panel
  
add(pFruits);    //add panel to frame
add(pVegitables); //add panel to frame
add(button);   //add toggle button to frame
  
button.addItemListener(this);   //add event listener to button to handle button event
setSize(300, 200);    //set frame's size
setVisible(true);   //show frame
}

public void itemStateChanged(ItemEvent eve) { //when button is clicked
if (button.isSelected()) { //if button is selected/pressed down show vegitables panel
pFruits.setVisible(false);
pVegitables.setVisible(true);
button.setText("OFF");
} else {                           //otherwised show fruits panel
pFruits.setVisible(true);
pVegitables.setVisible(false);
button.setText("ON");
}
}

public static void main(String args[]) {
      
       //create our frame
PanelShowHide f = new PanelShowHide();
}
}

Fruit Vegitables Apple Banana Cherry ON

匕Fruit Vegtables Potato Tomato Onion

Add a comment
Know the answer?
Add Answer to:
WRITE a program in JAVA using GRAPHICAL USER INTERFACE to make fruit and vegetable display panel with a toggle switch.(m...
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
  • VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert...

    VII JAVA ASSIGNMENT. Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a label to show the result. Display a warning if the user does not...

  • Display the following box using graphical user interface in Java. v the following message box, using...

    Display the following box using graphical user interface in Java. v the following message box, using gråaph Message Box Help Animation Insert File Edit Open Save Save as Cut Copy Paste Delete Reset Send Enter Message

  • In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student...

    In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student information system. The application should allow: 1. Collect student information and store it in a binary data file. Each student is identified by an unique ID. The user should be able to view/edit an existing student. Do not allow student to edit ID. 2. Collect Course information and store it in a separate data file. Each course is identified by an unique...

  • Please make a JAVA program for the following using switch structures Write a program that simulates...

    Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...

  • Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table...

    Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table shown below. The program will then display the cost per pound for that vegetable, again using the table provided. Your program must use a switch statement to process the input value and display the desired output. Be sure to provide a default case in your switch statement which handles unrecognized input. Use named constants for all constant strings (e.g. prompts and vegetable names) and...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

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