I am totally new to JAVA..i want to create a form with two Radio buttons on it,one for student and other for teacher. when the user clicks on student another form related to student should open and when user clicks on teacher,teachers form should open and then ask the user to enter related data....here is my code for first for....

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

public class Frm1 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frm1 frame = new Frm1();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Frm1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JRadioButton rb1=new JRadioButton("STUDENT");
        add(rb1);
        JRadioButton rb2=new JRadioButton("TEACHER"); 
        add(rb2);



    }

}

it adds only teacher radio button to the form not the student,can any one help plus how will i make the program to go to teacher or student form when respective radio button is selected.

thanks in advance

有帮助吗?

解决方案

You clearly have lots of study and learning ahead. Here is one (of many) ways to approach this problem.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Test().setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private JPanel cards;
    private CardLayout cardLayout;
    private static final String studentTag = "student";
    private static final String teacherTag = "teacher";

    public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel(new GridLayout(2,1));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        add(contentPane);

        JPanel radioPanel = new JPanel(new GridLayout(0,1));
        JRadioButton studentButton = new JRadioButton("Student");
        studentButton.setActionCommand(studentTag);
        studentButton.setSelected(true);
        JRadioButton teacherButton = new JRadioButton("Teacher");
        teacherButton.setActionCommand(teacherTag);
        ButtonGroup group = new ButtonGroup();
        group.add(studentButton);
        group.add(teacherButton);
        radioPanel.add(studentButton);
        radioPanel.add(teacherButton);
        contentPane.add(radioPanel);

        cardLayout = new CardLayout();
        cards = new JPanel(cardLayout);
        JPanel studentCard = new JPanel(new BorderLayout());
        studentCard.add(new Label("Student card"), BorderLayout.CENTER);
        JPanel teacherCard = new JPanel(new BorderLayout());
        teacherCard.add(new Label("Teacher card"), BorderLayout.CENTER);
        cards.add(studentCard, studentTag);
        cards.add(teacherCard, teacherTag);
        contentPane.add(cards);
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(cards, e.getActionCommand());
            }
        };
        studentButton.addActionListener(listener);
        teacherButton.addActionListener(listener);
        pack();
    }
}

其他提示

Well that's because you're using BorderLayout and not specifying where to put your buttons at. To test it out, comment the contentPane.setLayout(new BorderLayout(0, 0)); and you would see that both of them appear side by side.

To know more about layout, you should try learning them from this link "A Visual Guide to Layout Managers"

For more about Layout A Visual Guide to Layout Managers
See following code..

 /**
 * Create the frame.
 */
public Frm1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    //contentPane.setLayout(new BorderLayout(0, 0));   //this is your problem 
    setContentPane(contentPane);

    JRadioButton rb1=new JRadioButton("STUDENT");
    contentPane.add(rb1);
    JRadioButton rb2=new JRadioButton("TEACHER"); 
    contentPane.add(rb2);
    //Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(rb1);
    group.add(rb2);
}

Use ButtonGroup...

ButtonGroup group = new ButtonGroup();
JRadioButton mbutt1 = new JRadioButton("Yes");
JRadioButton mbutt2 = new JRadioButton("No");
group.add(mbutt1);
group.add(mbutt2);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top