2014-05-09 54 views
-1

我試圖創建新的JFrameJButton調用開始測試被點擊這個主程序。如何在點擊JButton時顯示新表單?

這應該是我的程序(photo link)

package javaapplication72; 
import javax.swing.*; 
import java.awt.*; 

public class javaapplication72{ 
    JFrame f1; 
    JPanel panel1,panel2,panel3,panel4; 
    JButton buttton_1,buttton_2; 
    JLabel label_1,label_2; 
    JRadioButton radio_1,radio_2,radio_3,radio_4,radio_5,radio_6,radio_7; 

    javaapplication72(){ 

     JFrame f1 = new JFrame ("MathTest - Main Menu"); 
     f1.setVisible(true); 
     f1.setSize(300,400); 
     f1.setLayout(new GridLayout(0,1)); 
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     panel1 = new JPanel(new GridLayout(0, 2)); 
     panel1.setBorder(BorderFactory.createLineBorder(Color.black, 1)); 
     panel1.add(new JLabel("Select a test type")); 
     panel1.add(new JRadioButton("Addition")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Substraction")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Multiplication")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Division")); 

     panel2 = new JPanel(new GridLayout(0, 2)); 
     panel2.setBorder(BorderFactory.createLineBorder(Color.black, 1)); 
     panel2.add(new JLabel("select a diffculty level")); 
     panel2.add(new JRadioButton("easy ")); 
     panel2.add(new JLabel("")); 
     panel2.add(new JRadioButton("moderate")); 
     panel2.add(new JLabel("")); 
     panel2.add(new JRadioButton("hard")); 

     panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     panel3.add(new JLabel("")); 
     panel3.add(new JButton("start test")); 

     panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     panel4.add(new JButton(" exit ")); 

     f1.add(panel1); 
     f1.add(panel2); 
     f1.add(panel3); 
     f1.add(panel4); 

     f1.add(label_1); 
     f1.add(radio_1); 
     f1.add(radio_2); 
     f1.add(radio_3); 
     f1.add(radio_4); 

     f1.add(label_2); 
     f1.add(radio_5); 
     f1.add(radio_6); 
     f1.add(radio_7); 
     f1.add(buttton_1); 
     f1.add(buttton_2); 

    } 

    public static void main(String[] args) 
    { 
    javaapplication72 xyz =new javaapplication72(); 

    } 
} 

這是我的新的幀我希望它顯示,當我點擊開始測試

package javaapplication71; 
import javax.swing.*; 
import java.awt.*; 

public class javaapplication71{ 
JFrame f1; 
JPanel panel1,panel4; 
JLabel label_1; 
JTextField t1; 

javaapplication71(){ 
f1 = new JFrame ("MathTest - Test Page"); 
f1.setVisible(true); 
f1.setSize(400,150); 
f1.setLayout(new GridLayout(0,1)); 
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

panel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
panel1.add(new JLabel("Question 1  14 - 5 = ")); 
panel1.add(new JTextField(10)); 
panel1.add(new JButton("Submit Answer")); 



panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
panel4.add(new JButton(" Cancel Test ")); 
f1.add(panel1); 
f1.add(panel4); 
f1.add(label_1); 

} 

public static void main(String[] args) 
{ 
    javaapplication71 xyz =new javaapplication71(); 
} 
} 
+1

1)爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可驗證示例)。 2)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 3)請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) –

回答

0

現在運行這段代碼。你沒有添加動作監聽器。但是,您的表單可以採用更好的面向對象方式進行設計。

package temp; 
import javax.swing.*; 
import java.awt.*; 

public class javaapplication71{ 
JFrame f1; 
JPanel panel1,panel4; 
JLabel label_1; 
JTextField t1; 

javaapplication71(){ 
f1 = new JFrame ("MathTest - Test Page"); 
f1.setVisible(true); 
f1.setSize(400,150); 
f1.setLayout(new GridLayout(0,1)); 
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

panel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
panel1.add(new JLabel("Question 1  14 - 5 = ")); 
panel1.add(new JTextField(10)); 
panel1.add(new JButton("Submit Answer")); 



panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
panel4.add(new JButton(" Cancel Test ")); 
f1.add(panel1); 
f1.add(panel4); 
f1.add(label_1); 

} 

} 

等班

package temp; 
import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class javaapplication72{ 
    JFrame f1; 
    JPanel panel1,panel2,panel3,panel4; 
    JButton buttton_1,buttton_2; 
    JLabel label_1,label_2; 
    JRadioButton radio_1,radio_2,radio_3,radio_4,radio_5,radio_6,radio_7; 

    javaapplication72(){ 

     JFrame f1 = new JFrame ("MathTest - Main Menu"); 
     f1.setVisible(true); 
     f1.setSize(300,400); 
     f1.setLayout(new GridLayout(0,1)); 
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     panel1 = new JPanel(new GridLayout(0, 2)); 
     panel1.setBorder(BorderFactory.createLineBorder(Color.black, 1)); 
     panel1.add(new JLabel("Select a test type")); 
     panel1.add(new JRadioButton("Addition")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Substraction")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Multiplication")); 
     panel1.add(new JLabel("")); 
     panel1.add(new JRadioButton("Division")); 

     panel2 = new JPanel(new GridLayout(0, 2)); 
     panel2.setBorder(BorderFactory.createLineBorder(Color.black, 1)); 
     panel2.add(new JLabel("select a diffculty level")); 
     panel2.add(new JRadioButton("easy ")); 
     panel2.add(new JLabel("")); 
     panel2.add(new JRadioButton("moderate")); 
     panel2.add(new JLabel("")); 
     panel2.add(new JRadioButton("hard")); 

     panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     panel3.add(new JLabel("")); 

     JButton startButton = new JButton("Start test"); 
     panel3.add(startButton); 

     startButton.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      new javaapplication71(); 

     } 
    }); 

     panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     panel4.add(new JButton(" exit ")); 

     f1.add(panel1); 
     f1.add(panel2); 
     f1.add(panel3); 
     f1.add(panel4); 
//  label_1 = new JLabel(); 
//  radio_1 = new JRadioButton(); 
//  radio_2 = new JRadioButton(); 
//  radio_3 = new JRadioButton(); 
//  radio_4 = new JRadioButton(); 
     f1.add(label_1); 
     f1.add(radio_1); 
     f1.add(radio_2); 
     f1.add(radio_3); 
     f1.add(radio_4); 
// 
//  label_2 = new JLabel(); 
//  radio_5 = new JRadioButton(); 
//  radio_6 = new JRadioButton(); 
//  radio_7 = new JRadioButton(); 

     f1.add(label_2); 
     f1.add(radio_5); 
     f1.add(radio_6); 
     f1.add(radio_7); 
     buttton_1 = new JButton(); 
     buttton_2 = new JButton(); 
     f1.add(buttton_1); 
     f1.add(buttton_2); 



    } 

    public static void main(String[] args) 
    { 
    javaapplication72 xyz =new javaapplication72(); 

    } 
} 

而且有註釋代碼那是因爲你使用了兩個標籤label_1label_2同樣radio_1 .. radio_6這些都只是參考。您應取消註釋我添加的代碼以刪除控制檯錯誤。

爲了產生表格我已經添加以下代碼到72類

JButton startButton = new JButton("Start test"); 
     panel3.add(startButton); 

     startButton.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      new javaapplication71(); 

     } 
    }); 

我已經添加了匿名類ActionListner實現這一目標。