如何在java中添加多个单选按钮以及如何连接两个表单?

时间:2012-06-29 04:46:07

标签: java swing layout-manager jradiobutton buttongroup

我对JAVA全新。我想创建一个带有两个单选按钮的表单,一个用于学生,另一个用于教师。当用户点击学生时,另一个与学生相关的表格应该打开,当用户点击老师时,教师表格应该打开然后要求用户输入相关数据....这里是我的第一个代码....

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);



    }

}

它只将教师单选按钮添加到不是学生的表单中,任何人都可以帮助加上如何在选择相应的单选按钮时让程序转到教师或学生表单。

提前致谢

4 个答案:

答案 0 :(得分:2)

那是因为你正在使用BorderLayout而没有指定把按钮放在哪里。要测试它,请注释contentPane.setLayout(new BorderLayout(0,0));你会发现它们都是并排出现的。

要了解有关布局的更多信息,您应该尝试通过此链接"A Visual Guide to Layout Managers"

了解它们

答案 1 :(得分:2)

有关布局A Visual Guide to Layout Managers的更多信息 请参阅以下代码..

 /**
 * 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);
}

答案 2 :(得分:2)

使用 ButtonGroup ...

ButtonGroup group = new ButtonGroup();
JRadioButton mbutt1 = new JRadioButton("Yes");
JRadioButton mbutt2 = new JRadioButton("No");
group.add(mbutt1);
group.add(mbutt2);

答案 3 :(得分:2)

你显然需要进行大量的学习和学习。以下是解决此问题的方法之一。

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();
    }
}