Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How can I Define a JFrame subclass that has four vertically positioned buttons.

Have Programming Question? Ask it here!
 
 
 


charlotte
How can I Define a JFrame subclass that has four vertically positioned buttons.
1 Answer(s)      4 years and 4 months ago
Posted in : Java Beginners


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.

Advertisement
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 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
Advertisements
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
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 access Subclass?
How to access Subclass?  How to access subclass in java? please tell me the syntex with an example
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
Tiny Window with subclass
). This means it can do everything a JFrame can, in addition to what we define... by defining a subclass of JFrame. All the work of buiding the JFrame will be put... program and one containing the JFrame subclass. It's perhaps more common
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
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
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 to reset JFrame
how to reset JFrame  how to reset JFrame
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
how can i make monthly register ?
how can i make monthly register ?  how can i make monthly register ? iam using jdeveloper
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 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 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. Thanks
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 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 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 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 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 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 change UIButton title color?
How can I change UIButton title color?  Hi, I have a button in my iPhone/iPad application. I want to change the color of the text when user clicks on it making it selected. Provide me good example code. Thanks   Hi
how can i simplify my java code
how can i simplify my java code  /* Create a program that prompts... c=0;int f=0;int i=0; int counter=0; int starter=0; int cot=1...)(Math.random() * 9 +cot); h = (int)(Math.random() * 9 +counter); i = (int
How can i search and insert within a query
How can i search and insert within a query  if it is possible... semester and degree if i search batch 2008 , semester first and degree bscs then search..., terminal) in another table. i am trying these but not successful, A problem occur
how to add scrollbar to JFrame
how to add scrollbar to JFrame  hello friends i am making a java application in which i have a frame to which i wanted to add scrollbars but when i add my panel to scrollpane and then add that scrollpane to JFrame than
how to add scrollbar to JFrame
how to add scrollbar to JFrame  hello friends i am making a java application in which i have a frame to which i wanted to add scrollbars but when i add my panel to scrollpane and then add that scrollpane to JFrame than
how to add scrollbar to JFrame
how to add scrollbar to JFrame  hello friends i am making a java application in which i have a frame to which i wanted to add scrollbars but when i add my panel to scrollpane and then add that scrollpane to JFrame than
how to add scrollbar to JFrame
how to add scrollbar to JFrame  hello friends i am making a java application in which i have a frame to which i wanted to add scrollbars but when i add my panel to scrollpane and then add that scrollpane to JFrame than
how to add scrollbar to JFrame
how to add scrollbar to JFrame  hello friends i am making a java application in which i have a frame to which i wanted to add scrollbars but when i add my panel to scrollpane and then add that scrollpane to JFrame than
how can i hide and show tables
how can i hide and show tables  hai, i am creating a form which the code is included below i want to hide the tables initially and after i click go i want to display the tables with result.so any help is appreciated much <
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
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 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 close particular frame in swings - Java Beginners
how can i close particular frame in swings  Hi, I want to know, how can we close a particular frame when mulitple frames are appearing as output... in swings and three frames are having their own close buttons and when i click
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 draw a table using applet or swings - Java Beginners
how can i draw a table using applet or swings  Hi RoseIndia, Actually, I was trying for creating a table using applet or swings.......... My task is create a table and enter the integer values during run time......I have
How can i display the distance of a line drawing randomly?
How can i display the distance of a line drawing randomly?  How can i display the distance of a line drawing randomly
Please tell me how can i convert string to timer
Please tell me how can i convert string to timer  Please tell me how can i convert string to timer
how can i do group tagging in portlet factory
how can i do group tagging in portlet factory  how can i do group tagging in portlet factory
Can i use Scriptlets inside javascript,if so how?
Can i use Scriptlets inside javascript,if so how?  Can i use Scriptlets inside javascript,if so how
how can I great two user and password in C++?
how can I great two user and password in C++?  how can I great two user and password in c
how can I great two user and password in C++?
how can I great two user and password in C++?  how can I great two user and password in C
How can I execute a PHP script using command line?
How can I execute a PHP script using command line?  How can I execute a PHP script using command line
How can I initialize the JSONArray and JSON object with data?
How can I initialize the JSONArray and JSON object with data?  How can I initialize the JSONArray and JSONObject with data
Advertisements
 

 

 

DMCA.com