728x90
반응형
GUI기반 덧셈, 곱셈 문제 생성기 프로그램 만들기 (난수 이용)
프로그램 GUI
전체 코드
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Swt33 extends JFrame implements ActionListener{
JLabel l1,l2, l3;
JTextField tf;
JButton b1,b2;
JComboBox<String> jb;
JRadioButton r1,r2;
int result;
Swt33(){
super("구몬 수학");
l1=new JLabel("문제");
l3=new JLabel(" = ");
r1=new JRadioButton("덧셈", true);
r2=new JRadioButton("곱셈");
ButtonGroup p=new ButtonGroup();
p.add(r1); p.add(r2);
b1=new JButton("Start");
b1.addActionListener(this);
jb=new JComboBox<String>();
jb=new JComboBox(); jb.addItem("1학년"); jb.addItem("3학년"); jb.addItem("5학년");
tf=new JTextField(10);
b2=new JButton("정답");
b2.addActionListener(new Re());
l2=new JLabel("맞았습니다(틀렸습니다)");
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());
p1.add(r1); p1.add(r2); p1.add(b1);
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jb);
JPanel p3=new JPanel();
p3.setLayout(new FlowLayout());
p3.add(l1); p3.add(tf);
JPanel p4=new JPanel();
p4.setLayout(new FlowLayout());
p4.add(b2);
JPanel p5=new JPanel();
p5.setLayout(new BorderLayout());
p5.add("North", p2); p5.add("Center", p3); p5.add("South", p4);
JPanel p6=new JPanel();
p6.setLayout(new FlowLayout());
p6.add("Center",l2);
this.setLayout(new BorderLayout());
this.add("North",p1); this.add("Center",p5);
this.add("South",p6);
this.setDefaultCloseOperation(3); //JFrame.EXIT_ON_CLOSE
this.pack();
this.setVisible(true);
}
class Re implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String s=tf.getText();
int i=Integer.parseInt(s);
if(result==i)
l2.setText("정답입니다");
else
l2.setText("다시 하세요");
}
}
public static void main(String[] args) {
new Swt33();
}
@Override
public void actionPerformed(ActionEvent e) {
String s=(String)jb.getSelectedItem();
int a=10; //1학년
if(s.equals("3학년")) a=50;
else if(s.equals("5학년")) a=100;
int n1=(int)(Math.random()*a+1);
int n2=(int)(Math.random()*a+1);
if(r1.isSelected()) {
l1.setText(n1+"+"+n2);
result=n1+n2;
} else {
l1.setText(n1+"*"+n2);
result=n1*n2;
}
}//this action
}
728x90
반응형