Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java, I created a grid layout jframe with buttons. how can I get them to work when clicked? I know I need an action

in java, I created a grid layout jframe with buttons. how can I get them to work when clicked? I know I need an action listener. I'm trying to create an organized/sorted list when a button is clicked. organized by type: dairy, meats, fruits, vegetables, bakery, breakfast , snacks, desserts. this the code for my grid:

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ButtonGrid { public final static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT); } pane.setLayout(new GridLayout(0,5)); pane.add(new JButton("Chicken")); pane.add(new JButton("Ground Beef")); pane.add(new JButton("Steak")); pane.add(new JButton("Turkey")); pane.add(new JButton("Lamb")); pane.add(new JButton("Milk")); pane.add(new JButton("Eggs")); pane.add(new JButton("Butter")); pane.add(new JButton("Yogurt")); pane.add(new JButton("Cheese")); pane.add(new JButton("Tomato")); pane.add(new JButton("Apple")); pane.add(new JButton("Eorange")); pane.add(new JButton("Grapes")); pane.add(new JButton("Banana")); pane.add(new JButton("Broccoli")); pane.add(new JButton("Lettuce")); pane.add(new JButton("Carrot")); pane.add(new JButton("Celery")); pane.add(new JButton("Corn")); pane.add(new JButton("Pancakes")); pane.add(new JButton("Oatmeal")); pane.add(new JButton("Cereal")); pane.add(new JButton("Waffle")); pane.add(new JButton("Breakfast Sandwiches")); pane.add(new JButton("Chips")); pane.add(new JButton("Dip")); pane.add(new JButton("Cookies")); pane.add(new JButton("Granola Bar")); pane.add(new JButton("Nuts")); pane.add(new JButton("Bread")); pane.add(new JButton("Cinnamon Buns")); pane.add(new JButton("Doughnuts")); pane.add(new JButton("Cakes")); pane.add(new JButton("Bagels")); pane.add(new JButton("Ice Cream")); pane.add(new JButton("Apple Pie")); pane.add(new JButton("Cherry Pie")); pane.add(new JButton("Chocolate Pie")); pane.add(new JButton("Popsicle")); } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Choose Items to Create Your Grocery List:"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); frame.setSize(1000, 1000); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago