package test1;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.*;
import java.util.Arrays;public class Test extends JFrame{
/**
 * 在下拉列表choice1中选择整数N,单击“生成”按钮在列表框List1和List2中显示N对的随机数(2~1000),
 * 在列表框List3中显示每对随机数的最大公约数。单击“排序”按钮,List3中的N个最大公约数按从小到大排列。
 * (提示:排序算法采用冒泡法。两数的最大公约数可以采用欧几里得算法:不断用两数中较大数减较小的数,至到有一个数等于0。
 * 另外一个大于0的数就是这两数的最大公数约)。
 */
private JComboBox choice;
private JList list1= new JList(),
              list2= new JList(),
              list3 = new JList();
private JButton button1 = new JButton("生成");
private JButton button2 = new JButton("排序");
private Integer[] numbers1 = null;
private Integer[] numbers2 = null;
private Integer[] numbers3 = null;

public Test(String[] args){
super("数据操作");
this.setSize(500,300);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
list1.setBounds(new Rectangle(50,50,50,50));
list2.setBounds(new Rectangle(105,50,50,50));
list3.setBounds(new Rectangle(205,50,50,50));
this.addList("Data1",list1);
this.addList("Data2",list2);
this.addList("Data3",list3);
choice = new JComboBox(args);
choice.setSize(20,10);
this.add(choice);
button1.addActionListener(new ButtonAction());
button2.addActionListener(new ButtonAction());
this.add(button1);
this.add(button2);
this.setVisible(true);
}

public void addList(String listName,JList list){
// list.setModel(new DefaultListModel());
JLabel label = new JLabel(listName);
// list.setVisible(true);
this.add(label);
this.add(list);
}

public int gcd(int a,int b){
if(a%b!=0)
return gcd(b,a%b);
else return b;
}
public static void main(String[] args) {
String[] n = {"2","3","4","5","6","7","8","9","10"};
Test myTest = new Test(n);
}

private class ButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(button1)){
// System.out.println(choice.getSelectedItem());
int n = Integer.parseInt(choice.getSelectedItem().toString());
numbers1 = new Integer[n];
numbers2 = new Integer[n];
numbers3 = new Integer[n];
for(int i = 0; i<n; i++){
numbers1[i] = new Integer((int)(Math.random()*998+2));
numbers2[i] = new Integer((int)(Math.random()*998+2));
numbers3[i] = new Integer(gcd(numbers1[i].intValue(),numbers2[i].intValue()));
// System.out.println(numbers1[i]);
}
list1.setListData(numbers1);
list2.setListData(numbers2);
list3.setListData(numbers3);
// System.out.println(n);
}else if(e.getSource().equals(button2)){
if(numbers2==null)
;
else{
Arrays.sort(numbers3);
// for(int i = 0; i<numbers3.length; i++)
// System.out.println(numbers3[i]);
list3.setListData(numbers3);
}
}
}
}
}

解决方案 »

  1.   

    可以将list1.setBounds(new   Rectangle(50,50,50,50)); 换成:
    list1.setPreferredSize(new Dimension(50,50));但是这样的JList不会滚动,可修改一下addList方法:
    public void addList(String listName, JList list) {
    // list.setModel(new   DefaultListModel());
    JLabel label = new JLabel(listName);
    // list.setVisible(true);
    this.add(label);
    JScrollPane js = new JScrollPane(list);
    js.setPreferredSize(new Dimension(50,50));
    this.add(js);
    }同时将
    list1.setBounds(new   Rectangle(50,50,50,50));
    list2.setBounds(new   Rectangle(105,50,50,50));
    list3.setBounds(new   Rectangle(205,50,50,50));
     
    删除
      

  2.   

    非常感谢,已经能看到,但是不太明白原来为什么没有显示出来。还有如果想要随数据多少改变JList大小怎么办呢?
      

  3.   

    this.setLayout(new   FlowLayout()); 使用布局后
    list1.setBounds(new       Rectangle(50,50,50,50)); 
    list2.setBounds(new       Rectangle(105,50,50,50)); 
    list3.setBounds(new       Rectangle(205,50,50,50));  这个方法是不起效果的.
    它会根布局策略自动调整位置. 
    如果想要随数据多少改变JList大小.肯定要用布局,具体怎么实现,自己可以在网上查查.