[英]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.