要求在确定与添加按钮中间加入一个文本框jtextfield,点击添加把文本框的内容添加到复选框cb中。
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;@SuppressWarnings(value="all")
public class Test4 extends JDialog{
 private String[] str;
 private JCheckBox[] cb;
 
 public Test4(String[] str,JFrame f){
  super(f,"挑选词",true); 
  this.str=str;
  final List<String> cj = new ArrayList<String>();
  JButton btnExit=new JButton("确定");
  JButton btnAdd=new JButton("添加");
  btnExit.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    setVisible(false);
    for(int i=0;i<cb.length;i++){
     if(cb[i].isSelected()){
      System.out.println(cb[i].getText());
     }
    }
   }
  });
  btnAdd.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    
   }
  });
  JPanel pBottom=new JPanel();
  pBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
  pBottom.add(btnExit);
  pBottom.add(btnAdd);  cb=new JCheckBox[str.length];
  
  JPanel p=new JPanel();
  JScrollPane sp=new JScrollPane(p);
  p.setLayout(new GridLayout(str.length/1,3));
  int savei=0;
  for(int i=0;i<str.length;i++){
   cb[i]=new JCheckBox(str[i]);
   cb[i].setSelected(true);
   p.add(cb[i]);
  }
  
  add(sp,BorderLayout.CENTER);
  add(pBottom,BorderLayout.SOUTH);
  Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
  int x=(size.width-500)/2-100;
  int y=(size.height-300)/2-100;
  setBounds(x,y,550,450);
  setResizable(false);
  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  setVisible(true);
 }
 
 public static void main(String[] args) {
  String str[]=new String[]{"1","2","3","4","5","6","7"};
  new Test4(str,null);
 }

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;@SuppressWarnings(value = "all")
    public class Test4 extends JDialog {
    private String[] str;
    private List<JCheckBox> cb; private JTextField field;
    private JPanel p; public Test4(String[] str, JFrame f) {
    super(f, "挑选词", true);
    this.str = str;
    final List<String> cj = new ArrayList<String>();
    JButton btnExit = new JButton("确定");
    JButton btnAdd = new JButton("添加");
    btnExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    setVisible(false);
    // for (int i = 0; i < cb.length; i++) {
    // // if (cb[i].isSelected()) {
    // // System.out.println(cb[i].getText());
    // // }
    // }
    }
    });
    btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String text = field.getText().trim();
    if (text != null && !"".equals(text)) {
    cb.add(new JCheckBox(text));
    }
    installBoxes();
    }
    });
    JPanel pBottom = new JPanel();
    pBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
    field = new JTextField(30);
    pBottom.add(btnExit);
    pBottom.add(field);
    pBottom.add(btnAdd); cb = new ArrayList<JCheckBox>(); p = new JPanel();
    JScrollPane sp = new JScrollPane(p);
    p.setLayout(new GridLayout());
    installBoxes(); add(sp, BorderLayout.CENTER);
    add(pBottom, BorderLayout.SOUTH);
    Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (size.width - 500) / 2 - 100;
    int y = (size.height - 300) / 2 - 100;
    setBounds(x, y, 550, 450);
    setResizable(false);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
    } private void installBoxes() {
    p.removeAll();
    for (int i = 0; i < cb.size(); i++) {
    JCheckBox cbox = cb.get(i);
    p.add(cbox);
    } p.revalidate();
    p.repaint();
    System.out.println("123");
    } public static void main(String[] args) {
    String str[] = new String[] { "1", "2", "3", "4", "5", "6", "7" };
    new Test4(str, null);
    }
    }
    这代码写的,多多磨练吧。
      

  2.   

    我错了
    public class Test
    {
        public static void main(String[] args)
        {
            int[][] a = new int[][]{{1,0,0,0,0,},{1,1,0,0,0},{1,1,1,0,0},{1,1,1,1,0},{1,1,1,1,1,1}};
            int[][] b = new int[][]{{1,0,0,0,0,},{1,1,0,0,0},{1,0,1,0,0},{1,0,1,0,0},{1,1,1,1,1,1}};
            int[][] c = new int[][]{{0,0,1,0,0,0,0,0,0},{0,0,1,1,1,0,0,0,0},{0,0,1,1,1,1,0,0,0},{0,1,1,1,1,1,1,1,0},{1,1,1,1,1,1,1,1,1}};
            int[][] d = new int[][]{{0,0,1,0,0,0,0,0,0},{0,0,1,0,1,0,0,0,0},{0,0,1,0,1,0,0,0,0},{0,1,0,1,0,0,0,0,0},{1,1,1,1,1,1,1,1,1}};
            int[][] e = new int[][]{{1,0,0,0,0,},{1,1,1,0,0},{1,1,1,1,1},{1,1,1,0,0},{1,1,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0}};
            int[][] f = new int[][]{{1,1,1,1,1,},{1,0,1,0,0},{1,0,1,0,0},{1,0,1,0,0},{1,1,1,1,1,1}};
            System.out.println("-----------------------------------------------");
            System.out.println("A图是:");
            Test.printxx(a);
            System.out.println("-----------------------------------------------");
            System.out.println("B图是:");
            Test.printxx(b);
            System.out.println("-----------------------------------------------");
            System.out.println("C图是:");
            Test.printxx(c);
            System.out.println("-----------------------------------------------");
            System.out.println("D图是:");
            Test.printxx(d);
            System.out.println("-----------------------------------------------");
            System.out.println("E图是:");
            Test.printxx(e);
            System.out.println("-----------------------------------------------");
            System.out.println("F图是:");
            Test.printxx(f);
        }
        public static void printxx(int[][] n)
        {
           int[][] x=n;
           int xl=x.length;
           int yl=x[0].length;
             for (int i=0;i<xl;i++)
            {
              for(int j=0;j<yl;j++)
              {
                 if(x[i][j]==1) 
                 {
                   System.out.print("*");
                 }
                 else
                 {
                  System.out.print(" ");
                 }
              }
              System.out.println();
            }   
        }
    }