我想在每次单击按钮时创建组合框

时间:2016-07-02 14:10:25

标签: java arrays variables combobox buttonclick

这是我的按钮点击事件。我想在每次单击按钮时创建组合框。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){

 jButton1.addActionListener(new ActionListener(){

     @Override
     public void actionPerformed(ActionEvent evt){

     List<JComboBox> listOfComboBoxes = new ArrayList<JComboBox>();

 for(int i=1;i<4;i++){
            int x = 28;
           int y = 100;
          int a = 145;
          int b = 28;

          listOfComboBoxes.get(i);
         listOfComboBoxes.get(i).addItem("--Select the Teacher--");
         listOfComboBoxes.get(i).setLayout(null);
         listOfComboBoxes.get(i).setLocation(x,y);

           add(listOfComboBoxes.get(i)).setSize(a,b);
       x=x+30;
   a=a+40;

   i++;

  }  
     }

});

1 个答案:

答案 0 :(得分:0)

你正在处理两个问题:一个是添加组合(比如说是JPanel),第二个是组合这些组合。 让我们把它分成两部分,首先理解添加机制:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MakeCombos extends JFrame {

    private static final int Number_OF_COMBOS = 4;
    private JButton jButton1;
    List<JComboBox> listOfComboBoxes;
    private JPanel panel;
    int counter = 0;

    MakeCombos(){

        super("Test frame");
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400,300));
        setLocationRelativeTo(null);
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        //create a panel
        panel = new JPanel();
        getContentPane().add(panel);

        //add button to it
        jButton1 = new JButton("Click Me");
        //add action listener to the panel
        jButton1.addActionListener((ActionEvent e)-> {
            jButton1ActionPerformed();
        });
        panel.add(jButton1, BorderLayout.CENTER);

        validate();
        pack();
        setVisible(true);

        listOfComboBoxes = makeCombos();
    }

    /**
     *@return
     */
    private List<JComboBox> makeCombos() {

        List combos = new ArrayList<JComboBox>();

        for(int i = 0;  i < Number_OF_COMBOS; i++) {

            JComboBox<String> combo = new JComboBox<>(new String[] {});
            combos.add(combo);
        }
        return combos;
    }

    private void jButton1ActionPerformed() {

        if(counter >= listOfComboBoxes.size()) {
            return;
        }


        listOfComboBoxes.get(counter);
        listOfComboBoxes.get(counter).addItem("--Select the Teacher--");
        getContentPane().add(listOfComboBoxes.get(counter)) ;//.setSize(a,b);

        counter++;

        pack();

    }

    public static void main(String[] args)  {

        new MakeCombos();
    }

}

运行它,如果不清楚,请不要犹豫。
现在让我们更进一步,做一下布局。 请看评论:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MakeCombos extends JFrame {

    private static final int Number_OF_COMBOS = 4;
    private List<JComboBox<String>> listOfComboBoxes;

    private JPanel combosPanel;

    private int xPosition = 28;
    private int yPosition = 100;
    private int width = 145;
    private int height = 28;
    private     int counter = 0;

    MakeCombos(){

        //make width frame for testing
        super("Test frame");
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(500,400));
        setLocationRelativeTo(null);
        //set layout manager to the frame
        getContentPane().setLayout(new BorderLayout());

        //add panel + button to the frame
        JPanel buttonPanel = new JPanel();
        getContentPane().add(buttonPanel, BorderLayout.NORTH); //add the panel to the frame

        //add button to the button panel
        JButton jButton1 = new JButton("Click Me");
        //add action listener to the button
        jButton1.addActionListener((ActionEvent e)-> {
            jButton1ActionPerformed();
        });
        buttonPanel.add(jButton1, BorderLayout.CENTER);

        //--add width panel for the combos
        combosPanel = new JPanel();
        //set layout manager to null so you can layout each combo
        //consider using a layout manager instead 
        combosPanel.setLayout(null); 
        getContentPane().add(combosPanel, BorderLayout.CENTER); //add the panel to the frame

        validate();
        pack();
        setVisible(true);

        //make the combos and add them to width list
        listOfComboBoxes = makeCombos();
    }

    /**
     *
     */
    private List<JComboBox<String>> makeCombos() {

        List<JComboBox<String>> combos = new ArrayList<JComboBox<String>> ();

        for(int i = 0;  i < Number_OF_COMBOS; i++) {

            JComboBox<String> combo = new JComboBox<>(new String[] {});
            combos.add(combo);
        }
        return combos;
    }

    private void jButton1ActionPerformed() {

        if(counter >= listOfComboBoxes.size()) {
            return;
        }

        //////////////////////////////////////////////////////////////////////////////
        //////////////// note : all this block could be moved to MakeCombos() ////////
        // (adding content and setting bounds could have been done when creating ////
        //the combo

        //add content to the combo
        listOfComboBoxes.get(counter).addItem("--Select the Teacher--");

        //set layout bounds to each combo
        listOfComboBoxes.get(counter).setBounds(xPosition, yPosition, width, height);

        //increment position
        xPosition=xPosition+30;
        yPosition=yPosition+40;

        ////////////////////////////////////////////////////////////////////////////
        /////////////////////// end of move-to-makeCombos() block //////////////////
        ////////////////////////////////////////////////////////////////////////////

        //add the combo to the combos panel
        combosPanel.add(listOfComboBoxes.get(counter)) ;

        //increment position and counter
        counter++;

        repaint();
        pack();

    }

    public static void main(String[] args)  {

        new MakeCombos();
    }
}