2012-06-29 29 views
1

我對JAVA完全陌生。我想創建一個帶有兩個單選按鈕的窗體,一個用於學生,另一個用於教師。當用戶點擊學生時,應打開另一個與學生相關的表單,當用戶點擊教師時,教師表單應打開,然後要求用戶輸入相關數據....這裏是我的第一個代碼....如何添加更多然後一個單選按鈕形式以及如何連接在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); 



    } 

} 

它只爲表單添加老師單選按鈕而不是學生,任何一個幫助以及如何選擇相應的單選按鈕時我將程序轉到老師或學生窗體。

在此先感謝

回答

2

你顯然有很多的研究和l收入前進。以下是解決這個問題的方法之一。

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

嗯,這是因爲你使用的BorderLayout和不指定在哪裏把你的按鈕時。要測試它,請註釋contentPane.setLayout(new BorderLayout(0,0));你會看到他們兩個並排出現。

要了解更多關於佈局,你應該嘗試從這個鏈接"A Visual Guide to Layout Managers"

+0

我可以在表格上找到兩個按鈕。但問題是如果我點擊學生或老師其各自的表格不打開 – user1390517

+0

@ user1390517 - 當您點擊相應的按鈕時,您是否添加了聽衆爲您完成這項工作?你基本上想要處理你的事件。如果您不知道如何使用監聽器,以下是另一個參考資料:http://docs.oracle.com/javase/tutorial/uiswing/events/index.html – Sujay

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

使用的ButtonGroup ...

ButtonGroup group = new ButtonGroup(); 
JRadioButton mbutt1 = new JRadioButton("Yes"); 
JRadioButton mbutt2 = new JRadioButton("No"); 
group.add(mbutt1); 
group.add(mbutt2); 
相關問題