?
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
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33557986
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
ID: 33558312
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
ID: 33558672
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 33559649
>>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
ID: 33560203
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 33560220
Shown where though?
0
 

Author Comment

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

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 33560372
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
ID: 33560873
Absolutely perfect! You are phenomenal. Thank you so much!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33561307
:)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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