我的程序没有在动作处理程序中创建预期的JButton

时间:2016-12-30 05:51:49

标签: java swing jbutton

以下Java程序尝试从数据库中读取数据,但是我没有创建多个按钮,而是希望是while()循环的结果,我只得到一个按钮,其标题是第一个数据库条目。任何人都可以帮我确定原因吗?

 // Submit button's action handler

btnSubmit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) 
            JFrame2 f2 = new JFrame2();
            f2.setVisible(true);
            try {
                String query = "select name from decipline";
                pst = conn.prepareStatement(query);
                rs = pst.executeQuery();
                while(rs.next()) {  
                    String str = rs.getString("name");
                    JButton btn = new JButton(str);
                    System.out.println(str);
                    btn.addActionListener(this);
                    btn.setBounds(154, 112, 89, 23);
                    f2.getContentPane().add(btn);               
                }
            } catch(Exception E) {
                E.printStackTrace();
            } 
        }
    });

2 个答案:

答案 0 :(得分:0)

您确实希望尽可能避免使用null布局和setBounds。查看所有可用的layout managers。根据您的示例,我建议使用GridLayout。此外,您可能希望在JOptionPane中创建一个JRadioButtons的ButtonGroup,而不是JFrame中的提交类型示例中的按钮。

try {
    List<String> names = new ArrayList<String>();
    PreparedStatement ps = conn.prepareStatement("SELECT name FROM discipline");
    ResultSet rs = ps.executeQuery();
    while(rs.next()) {
        names.add(rs.getString("name"));
    }

    JPanel menu = new JPanel(new GridLayout(1, names.size())); // One column, as many rows as names.
    List<JRadioButton> buttons = new ArrayList<JRadioButton>();
    ButtonGroup bg = new ButtonGroup();
    for(String name : names) {
        JRadioButton radio = new JRadioButton(name);
        bg.add(radio);
        buttons.add(radio);
        menu.add(radio);
    }

    int code = JOptionPane.showConfirmDialog(null, menu, "Choose a name.", JOptionPane.OK_CANCEL_OPTION);
    if(code == JOptionPane.OK_OPTION) {
        JRadioButton selected = null;
        for(JRadioButton btn : buttons) {
            if(btn.isSelected()) {
                selected = btn;
            }
        }
        if(selected != null) {
            String name = selected.getText();
            // Do submission with chosen name.
        } else {
            // You didn't choose a name!
        }
    } else {
        // Canceled option.
    }
} catch (Exception e) {
    e.printStackTrace();
}

答案 1 :(得分:-1)

以下是我的建议。

    btnSubmit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) 
                    //frame.dispose();
                    JFrame2 f2=new JFrame2();
                    f2.setVisible(true);
                    f2.setLayout(null);
                try{
                    int x=154, width=89;
                    String query = "select name from decipline";
                    pst=conn.prepareStatement(query);
                    rs=pst.executeQuery();
                    //int i=0;
                while(rs.next())
                {   
                     String str= rs.getString("name");
                     JButton btn =new JButton(str);
                     System.out.println(str);
                     btn.addActionListener(new ActionListner(){
                          @Override
                    public void actionPerformed(ActionEvent e) {
                     .......
                       }
                     });
                     btn.setBounds(x, 112, width, 23);
                     x=x+width+1;
                     f2.getContentPane().add(btn);

                  }
                }
                catch(Exception E){
                    E.printStackTrace();
                } 
            }
        });