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.

Recommended Answers

All 2 Replies

Java programming question?
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.

Im using net beans if that helps and am not to familiar with the program
7 hours ago - 4 days left to answer.
Additional Details
This is what I have so far
charlottesform.java

package charlottespackage;

/**
*
* @author charlotte
*/
public class charlottesform extends javax.swing.JFrame {

/** Creates new form charlottesform */
public charlottesform() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
Senior = new javax.swing.JButton();
Junior = new javax.swing.JButton();
sophmore = new javax.swing.JButton();
freshman = new javax.swing.JButton();
Result = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOp
4 minutes ago

and this is my main class
main.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package charlottesfinalproject;

/**
*
* @author charlotte
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}

}

3 minutes ago

I want the person to be able to press the button and in the JTextField output the result "for instance when you click Senior "the user should see the result senior in the JtextField.
59 seconds ago

I just cant figure out how to program it and thats what I need help with.

Use a GridLayout. You can change which Layout Manager netbeans is using by right clicking on the form, I think. Your GridLayout should use 1 row and 1 column, that way the buttons will be displayed vertically. Then just use the palette to add 4 buttons. Netbeans will automatically arrange them vertically if you use GridLayout like I just mentioned. To pop up a message with JOptionPane you can do

JOptionPane.showMessageDialog(null, "Message!");

To detect when one of your buttons was clicked, look at this tutorial. It is literally a matter of right clicking the button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.