package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class text implements ItemListener, ActionListener {
JFrame f=new JFrame("选择题答题卡");
JRadioButton r1,r2,r3,r4;
JLabel tt=new JLabel("请在这里输入答案:");
JButton b1,b2;
ButtonGroup bg;
int op=0;
int id=0;
int i=0;
String daa;
String daan[];
int xuanze=10;
JPanel p1=new JPanel() ;
JPanel p2=new JPanel();
void xuanze(){
r1=new JRadioButton("A");
r1.setSize(40, 20);
r2=new JRadioButton("B");
r2.setSize(40, 20);
r3=new JRadioButton("C");
r3.setSize(40, 20);
r4=new JRadioButton("D");
r4.setSize(40, 20);
// p1.setLayout(null);
p1.add(r1);
p1.add(r2);
p1.add(r3);
p1.add(r4);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this);
}
public text()
{
for(int i=0;i<=xuanze-1;i++){
p1.setLayout(new GridLayout(i+1,5));
JLabel j=new JLabel();
j.setText(String.valueOf(i+1));
j.setSize(40, 20);
p1.add(j);
xuanze();
}
b1=new JButton("提交");
b2=new JButton("取消");
p2.add(b1);
p2.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
tt.setSize(100, 20);
f.getContentPane().add(tt,"North");
f.getContentPane().add(new JScrollPane(p1));
f.getContentPane().add(p2,"South");
f.setBounds(200,30,250,500);
f.setVisible(true);
}
public static void main(String args[])
{
new text();
}
@Override
public void itemStateChanged(ItemEvent e) {
//
if(e.getSource()==r1) op=1;
if(e.getSource()==r2) op=2;
if(e.getSource()==r3) op=3;
if(e.getSource()==r4) op=4;
switch(op)
{
case 1:daa="A";break;
case 2:daa="B";break;
case 3:daa="C";break;
case 4:daa="D";break;
default:daa=null;
}
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if(cmd.equals("提交"))
{
System.out.print(id+"\t");
System.out.print(daa+"\n");
}
if(cmd.equals("取消"))
{
System.exit(0);
}
}}
我有数组保存会出错,现在只能输出最后一题,请教各位高手
------解决方案--------------------
循环打印不就i输出所有的了么?
------解决方案--------------------
改用集合了,集合比较好用