Solved

Define a JFrame subclass that has four vertically positioned buttons. The labels for the four buttons are Senior, Junior, Sophomore, and Freshman.

Posted on 2010-08-30
10
465 Views
Last Modified: 2012-05-10
I need to 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. I do not know where to begin on this.
0
Comment
Question by:Rowley4
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Use a JPanel with a BoxLayout. Add it to the JOptionPane

http://download.oracle.com/javase/tutorial/uiswing/layout/box.html
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
Use BorderLayout, which is the default for JFrame, check this example http://www.roseindia.net/java/example/java/awt/borderlayout-example-in-java.shtml
0
 

Author Comment

by:Rowley4
Comment Utility
Awesome link, very helpful!

So if this is my code, and I want to add a different content to each button when the button is pushed how would I do that?

import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayoutDemo {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        addAButton("Freshman", pane);
        addAButton("Sophomore", pane);
        addAButton("Junior", pane);
        addAButton("Senior", pane);
    }

    private static void addAButton(String text, Container container) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(button);
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("BoxLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>add a different content to each button when the button is pushed

What sort of thing did you have in mind?
0
 

Author Comment

by:Rowley4
Comment Utility
Something very simple like Freshman: First year of school. Sophomore: Second year of school. Junior: Third year of school. Senior: Forth and final year of school.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Shown where though?
0
 

Author Comment

by:Rowley4
Comment Utility
No specifications. Where/however would be the easiest.  A new window??
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
Try something like
import java.awt.Component;

import java.awt.Container;

import java.awt.event.ActionEvent;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JOptionPane;

import javax.swing.AbstractAction;

import javax.swing.JFrame;



public class BoxLayoutDemo {

    public static void addComponentsToPane(Container pane) {

        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));



        addAButton("Freshman", "A freshman is...", pane);

        addAButton("Sophomore", "A sophomore is...", pane);

        addAButton("Junior", "A junior is...", pane);

        addAButton("Senior", "A senior is...", pane);

    }



    private static void addAButton(String text, String studentInfo, Container container) {

        JButton button = new JButton(new StudentAction(text, studentInfo));

        button.setAlignmentX(Component.CENTER_ALIGNMENT);

        container.add(button);

    }



    private static class StudentAction extends AbstractAction {

	private String studentInfo;



	public StudentAction(String buttonCaption, String studentInfo) {

	    super(buttonCaption);

	    this.studentInfo = studentInfo;

	}



	public void actionPerformed(ActionEvent e) {

	    JOptionPane.showMessageDialog(null, studentInfo);

	}

    }



    private static void createAndShowGUI() {

        //Create and set up the window.

        JFrame frame = new JFrame("BoxLayoutDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        //Set up the content pane.

        addComponentsToPane(frame.getContentPane());



        //Display the window.

        frame.pack();

        frame.setVisible(true);

    }



    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }

}

Open in new window

0
 

Author Comment

by:Rowley4
Comment Utility
Absolutely perfect! You are phenomenal. Thank you so much!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 71
countPairs challenge 7 57
mapAB Challlenge 35 84
recursion example 16 66
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now