1

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



    }

}

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.

提前致谢

4

4 回答 4

2

那是因为您使用的是 BorderLayout 而没有指定按钮的放置位置。要测试它,请注释 contentPane.setLayout(new BorderLayout(0, 0)); 你会看到它们并排出现。

要了解有关布局的更多信息,您应该尝试从此链接“布局管理器的视觉指南”中学习它们

于 2012-06-29T05:02:09.763 回答
2

有关布局的更多信息布局管理器的可视指南
请参阅以下代码..

 /**
 * 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);
}
于 2012-06-29T05:17:05.513 回答
2

使用按钮组...

ButtonGroup group = new ButtonGroup();
JRadioButton mbutt1 = new JRadioButton("Yes");
JRadioButton mbutt2 = new JRadioButton("No");
group.add(mbutt1);
group.add(mbutt2);
于 2012-06-29T05:25:50.757 回答
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();
    }
}
于 2012-06-29T06:03:04.870 回答