How can I Define a JFrame subclass that has four vertically positioned buttons.

How can I Define a JFrame subclass that has four vertically positioned buttons.

How can I Define a JFrame subclass that has four vertically positioned buttons. The labels for the four buttons are Senior, Junior, Sophomore, and Freshman. When a button is clicked, display a message that identifies which button is clicked, using JOptionPane.
View Answers

March 22, 2010 at 12:08 PM

Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class DisplayButtons extends JFrame {
int i=0;
static JPanel panel;
public DisplayButtons(){
JButton[] buttons = new JButton[4];
panel=new JPanel(new GridLayout(4,1));
String b[]={"Senior","Junior","Sophomore","Freshman"};
for(i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(b[i]);
buttons[i].setSize(80, 80);
buttons[i].setActionCommand(b[i]);
buttons[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String choice = e.getActionCommand();
JOptionPane.showMessageDialog(null, "You have clicked: "+choice);
}
});
panel.add(buttons[i]);
}
}
public static void main(String[]args){
DisplayButtons frame=new DisplayButtons();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}

For more information, visit the following link:

http://www.roseindia.net/java/example/java/swing/create_multiple_buttons_using_ja.shtml

Thanks









Related Tutorials/Questions & Answers:
How can I Define a JFrame subclass that has four vertically positioned buttons. - Java Beginners
How can I Define a JFrame subclass that has four vertically positioned buttons.  How can I Define a JFrame subclass that has four vertically positioned buttons. The labels for the four buttons are Senior, Junior, Sophomore
how can i define only one method from the interface. - Java Beginners
how can i define only one method from the interface.  If i am having an interface with 3 methods(declaration) . If i want to use only one method... the solution for that. please i want it immediately.  Hi friend
Advertisements
How can combine threads and buttons?
How can combine threads and buttons?  I would like to start an application, stop it and restart it again and stop etc. I tried the following code. It does start and stop, but I get an error when I try to restart. Any
How can I paginate a table which has shown in a div through Ajax in client side without using database
How can I paginate a table which has shown in a div through Ajax in client side without using database   The table is shown by calculation in a PHP script file so The data not store any where so please give me the way to paginate
how to create directed line between two buttons when i clicked there
how to create directed line between two buttons when i clicked there  Hi I have to create a topology between 6 nodes. 6 nodes are created as 6 buttons and when i clicked in first button and then on second button a directed
How to access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
How can I do it? .click();
How can I do it? .click();  I have a very unusual problem. I want...("a"); x.click(); </script> So it's click on an element witch one Id's is "a", but I want that it make mouseup in this element. How can I do it, because if I write
libodbc++ is stable? Can I use it in my production project? Is it has bugfix support?
libodbc++ is stable? Can I use it in my production project? Is it has bugfix...? Can i use it for my project without any risk like memory leak or any other problems in library code. Can I get help if there is any problems? It comes under
How can I learn Java?
How can I learn Java?  Hi, I have just completed a course in HTML and C programming language. I have some programming experience in visual basic... programming. How can I learn Java? in shortest possible time. I mean I just want to begin
how should i can solve
how should i can solve  Web based program - Input - Person's contact details with Passport Number as Unique Key. Save data in to oracle / MySQL. Output - List of Persons saved in the database. Technology to be used - JSP
how can i display a editable result of form?
how can i display a editable result of form?  how can i display a editable result of form? i know how to display form result but the result... show the result but i can not modify the result. how can i display modifyable
how to reset JFrame
how to reset JFrame  how to reset JFrame
Vertically center page
Vertically center page  I know how to center the text of the page Horizontally ..but is it possible to vertically center page in HTML or CSS?   You can set the Height, width and left, right alignment for the text in CSS
how can i display a pdf file in a jtextarea
how can i display a pdf file in a jtextarea  I need to display a pdf file in a jtextfield or in a jtextarea.Atlest i need to displat it in a jframe.I have a button and while clicking on it ,i need to choose the pdf file and need
how can i add hibernate plugin to eclipse?
how can i add hibernate plugin to eclipse?  how can i add hibernate plugin to eclipse
how i can get jqfade.js library
how i can get jqfade.js library  how i can get jqfade.j script library
JFrame
Jframe. How can i do it. Please help me to do it. Or what else can i have...JFrame  Hi, I have JFrames in my application and i'm developing application using NetBeans IDE 6.5. I have many jframes. Whenver i click a menu
how can i remove newline characters and tabs
how can i remove newline characters and tabs   how can i remove newline characters and tabs ? but i wont to using GUI .please help
how can i make monthly register ?
how can i make monthly register ?  how can i make monthly register ? iam using jdeveloper
how can i create a discussion forum?
how can i create a discussion forum?  how can i create a discussion forum for my e- mentoring site for women which can be used by a registered user only. i am using jsp and servlets and i am working with netbeans 6.8.
how can i set an object in focus
how can i set an object in focus  There is a Canvas object in my game... on the Board . Basically i am working on snake game project, and i want is when... i am facing is after clicking button gamescreen appearing on window but snake
how to move label in jframe in java?
how to move label in jframe in java?  please any one can give me code for moving a java label in Jframe
How can I learn Java in 5 minutes
How can I learn Java in 5 minutes  Hi, I am searching for the tutorials to learn Java programming. I don't know how easy or difficult is Java.... It will be helpful for me if someone gives me tutorials to learn Java. How can I
How can I to my database to my application
How can I to my database to my application  How can I to my database to my application   Hi, Please see the JDBC discussion thread.ADS_TO_REPLACE_1 Thanks
How can i modify my account in roseindia
How can i modify my account in roseindia  Presently am not using my gmail id. I have to modify my roseindia account. Please send the answer to following mail id
how to define variable in node js
how to define variable in node js  Hi, I want to know the correct way to define variable in node js. how to define variable in node js? Thanks   Hi, Node js is nothing but server-side JavaScript. You can define
how to define variable in node js
how to define variable in node js  Hi, I want to know the correct way to define variable in node js. how to define variable in node js? Thanks   Hi, Node js is nothing but server-side JavaScript. You can define
How can I protect my database password ?
How can I protect my database password ?   How can I protect my... in as plain text. What can I do to protect my passwords... a database over the internet. I have concerns about the security of the database
How can I master Java in one month?
How can I master Java in one month?  Hi, I wish to learn Java programming in one month. I have little experience in c programming and PHP programming. Is there any way to learn Java quickly. How can I master Java in one month
how can i print the selected content of a frame
how can i print the selected content of a frame  hello sir, I am designing a bill calculate program. I want to print the bill in crystal form. I want to skip all the text fields shapes and all the button from the frame.. but all
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
How can i use Facebook connect button
How can i use Facebook connect button  Please to meet you all guys I wonder how can i use this Connect to facebook for me to post in a particular... http://likekhevy4.blogspot.com/ How can i apply this kind of comment with "Connect
how can i close a frame. - Java Beginners
how can i close a frame.  Hi, My question is how can we close... frames as i go on clicking button...........can we have like when i click on button...............my target is when i click on that button, a new frame is coming and updated table
How can I create sessionfactory in Hibernate?
How can I create sessionfactory in Hibernate?  HELLO, How can I create sessionfactory in Hibernate? If you can explain me with example that would... with the links below: Hibernate 4 Hibernate Session Factory I hope
How can I start big data?
How can I start big data?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can I... learn the topic "How can I start big data?". Also tell me which
How can I get IBM certification for free?
How can I get IBM certification for free?  Hi, I am beginner in Data...: How can I get IBM certification for free? Try to provide me good examples or tutorials links so that I can learn the topic "How can I get IBM
How can I practice big data at home?
How can I practice big data at home?  Hi, I am beginner in Data...: How can I practice big data at home? Try to provide me good examples or tutorials links so that I can learn the topic "How can I practice big data at home
How can I become a data scientist for free?
How can I become a data scientist for free?  Hi, I am beginner... to learn: How can I become a data scientist for free? Try to provide me good examples or tutorials links so that I can learn the topic "How can I become
How can I become a good data scientist?
How can I become a good data scientist?  Hi, I am beginner in Data...: How can I become a good data scientist? Try to provide me good examples or tutorials links so that I can learn the topic "How can I become a good data
How can I get into big data?
How can I get into big data?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can I... can learn the topic "How can I get into big data?". Also tell me
How can I learn Hadoop easily?
How can I learn Hadoop easily?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can... that I can learn the topic "How can I learn Hadoop easily?". Also tell
How can I do data science course?
How can I do data science course?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How... so that I can learn the topic "How can I do data science course?"
How can I become a data scientist?
How can I become a data scientist?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How... links so that I can learn the topic "How can I become a data scientist?"
How can I become a data scientist in India?
How can I become a data scientist in India?  Hi, I am beginner... to learn: How can I become a data scientist in India? Try to provide me good examples or tutorials links so that I can learn the topic "How can I become
How can I go to ISRO after BSc?
How can I go to ISRO after BSc?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can... that I can learn the topic "How can I go to ISRO after BSc?". Also
How quickly can I learn Python?
How quickly can I learn Python?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How... that I can learn the topic "How quickly can I learn Python?". Also
How can I teach myself Python?
How can I teach myself Python?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can... that I can learn the topic "How can I teach myself Python?". Also tell
How can I learn Python fast?
How can I learn Python fast?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can I... can learn the topic "How can I learn Python fast?". Also tell me
How can I learn Python finance?
How can I learn Python finance?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: How can... that I can learn the topic "How can I learn Python finance?". Also

Ads