使用Actionlistener使用JRadioButtons创建新框架

时间:2012-12-07 17:59:43

标签: java swing jframe frame actionlistener

您好我在创建一个actionlistener时遇到了一些麻烦,该动作创建了一个用JRadioButtons作为选择的新框架。

最终它将成为我正在创造的闪存卡测验计划。

这就是我被困的地方:

    import javax.swing.*;

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


    public class FlashCard extends JFrame {
    private ImageIcon myIcon = new ImageIcon("src/Resources/DNA.png");



    public FlashCard(){
    //Consider using CardLayout format for flashcards.

    setLayout(new GridLayout(1, 4, 5, 5));

    JButton startButton = new JButton("Begin");

    add(startButton);

    startButton.addActionListener(new ActionListener(){


        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

            //Execute when button is pressed THIS IS THE PART WHERE I AM STUCK
            JFrame frameAction = new JFrame();
            frameAction.setTitle("Questions");
            frameAction.setSize(350, 150);
            frameAction.setLocationRelativeTo(null);
            frameAction.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frameAction.setVisible(true);

            frameAction.

    //              JRadioButton jrb1 = new JRadioButton("Student", true);
    //              jrb1.setForeground(Color.RED);
    //              jrb1.setBackground(Color.WHITE);
    //              jrb1.setMnemonic('S');
    //          
    //              ButtonGroup group = new ButtonGroup();
    //              add(jrb1);
        }
    });

}

这是我的主要方法:

    public static void main(String[] args){

    //Create a frame and set its properties.
    JFrame frame = new FlashCard();
        frame.setTitle("Genetics FlashCard Quiz");
        frame.setSize(350,150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    //Create a second frame for when the user
    // clicks begin.

    JFrame question = new JFrame();





}

1 个答案:

答案 0 :(得分:0)

鉴于您JFrames的尺寸和尺寸相同维度,您使用CardLayout的其他想法(在代码中)是正确的。目前您的第二帧出现在第一帧上,为您提供管理后者的开销,因为它仍然可以访问。如果您使用CardLayout,则可以在多个flashcard面板之间轻松导航,同时为用户维护单个交互区域。

见:

相关问题