繁体   English   中英

使用Actionlistener使用JRadioButtons创建新框架

[英]Using Actionlistener to create new frame with JRadioButtons

嗨,我在创建一个动作监听器时遇到了麻烦,该动作监听器使用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();





}

鉴于两个JFrames的大小和尺寸相同,使用CardLayout另一个想法(在代码中)是正确的。 当前,您的第二个框架显示在第一个框架之上,为您管理第二个框架提供了开销,因为它仍然可以访问。 如果使用CardLayout您将可以轻松地在多个flashcard面板之间导航,同时为用户维护单个交互区域。

看到:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM