JRadioButton正在选择最后一个

时间:2014-11-12 04:24:40

标签: java sql database swing jradiobutton

我的问题是: -  当我有多个JRadioButtons时,它只选择要放入我的数据库表的最后一个。我已经尝试了很多,我无法弄清楚为什么它只插入最后一个RadioButton。即使我点击一个不同的按钮,无论它总是选择最后一个是无麸质外壳。请帮帮我。这是我的代码中与此问题有关的部分。

JButton btnFinish = new JButton("Finish");
    btnFinish.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try{
                close();
                String query = "insert into Exam2 (FullName, Address, Phonenumber, PizzaSize, CrustType, Toppings, Quantity) values (?,?,?,?,?,?,?)";
                    java.sql.PreparedStatement pst= con.prepareStatement(query);


                    pst.setString(1, txtName.getText());
                    pst.setString(2, txtAddress.getText());
                    pst.setString(3, txtNumber.getText());
                    pst.setString(4, rdbtnSmall.getText());
                    pst.setString(4, rdbtnMedium.getText());
                    pst.setString(4, rdbtnLarge.getText());
                    pst.setString(5, rdbtnHandTossed.getText());
                    pst.setString(5, rdbtnHandmadePan.getText());
                    pst.setString(5, rdbtnBrooklynStyle.getText());
                    pst.setString(5, rdbtnGlutenFreeCrust.getText());
                    pst.setString(6, rdbtnPepperoni.getText());
                    pst.setString(6, rdbtnItalianSausage.getText());
                    pst.setString(6, rdbtnBeef.getText());
                    pst.setString(6, rdbtnHam.getText());
                    pst.setString(6, rdbtnBacon.getText());
                    pst.setString(6, rdbtnOlives.getText());
                    pst.setString(6, rdbtnMushrooms.getText());
                    pst.setString(6, rdbtnOnions.getText());
                    pst.setString(7, textField.getText());



                    pst.execute();
                    close();




                }catch(Exception i) {
                        System.err.println("Exception: " + i.getMessage());
                        JOptionPane.showMessageDialog(null, "There has been an error connecting to the database");
                    }



        }
    });

    JSeparator separator = new JSeparator();

    JLabel lblPizzaSizeAnd = new JLabel("Pizza Size And Crust");
    lblPizzaSizeAnd.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

    rdbtnSmall = new JRadioButton("Small(10\")");

    rdbtnMedium = new JRadioButton("Medium(12\")");

    rdbtnLarge = new JRadioButton("Large(14\")");

    JSeparator separator_1 = new JSeparator();

    JSeparator separator_2 = new JSeparator();
    separator_2.setOrientation(SwingConstants.VERTICAL);

    rdbtnHandmadePan = new JRadioButton("HandMade Pan");
    rdbtnHandmadePan.setToolTipText("Two layers of cheese, toppings to the edge, and a crust that bakes up golden and crispy with a buttery taste.");

    rdbtnHandTossed = new JRadioButton("Hand Tossed");
    rdbtnHandTossed.setToolTipText("Garlic-seasoned crust with a rich, buttery taste.");

    rdbtnBrooklynStyle = new JRadioButton("Brooklyn Style");
    rdbtnBrooklynStyle.setToolTipText("Hand stretched to be big, thin, and perfectly foldable.");

    rdbtnGlutenFreeCrust = new JRadioButton("Gluten Free Crust");
    rdbtnGlutenFreeCrust.setToolTipText("Domino's pizza made with a Gluten Free Crust.");

    group = new ButtonGroup();
    group.add(rdbtnHandmadePan);
    group.add(rdbtnHandTossed);
    group.add(rdbtnBrooklynStyle);
    group.add(rdbtnGlutenFreeCrust);

    JSeparator separator_3 = new JSeparator();

    JLabel lblToopings = new JLabel("Toppings");
    lblToopings.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

    rdbtnPepperoni = new JRadioButton("Pepperoni");

    rdbtnItalianSausage = new JRadioButton("Italian Sausage");

    rdbtnBeef = new JRadioButton("Beef");

    rdbtnHam = new JRadioButton("Ham");

    rdbtnBacon = new JRadioButton("Bacon");

    rdbtnOlives = new JRadioButton("Olives");

    rdbtnMushrooms = new JRadioButton("Mushrooms");

    rdbtnOnions = new JRadioButton("Onions");

    JLabel lblQuantity = new JLabel("Quantity");

    textField = new JTextField();
    textField.setColumns(10);

谢谢!

1 个答案:

答案 0 :(得分:2)

您正在为单个列分配多个值...

pst.setString(4, rdbtnSmall.getText());
pst.setString(4, rdbtnMedium.getText());
pst.setString(4, rdbtnLarge.getText());

这意味着实际上只会使用您设置的最后一个值。您应该检查选择了哪些按钮,只添加所选按钮......

您可以使用ButtonGroup更轻松地从群组中获取所选按钮...

ButtonModel model = bg.getSelection();
String selection = model == null ? "Nothing" : model.getActionCommand();

对于一个可运行的例子......

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ButtonGroup bg;

        public TestPane() {

            JRadioButton red = new JRadioButton("Red");
            red.setActionCommand("Red");
            JRadioButton blue = new JRadioButton("Blue");
            blue.setActionCommand("Blue");
            JRadioButton green = new JRadioButton("Green");
            green.setActionCommand("Green");

            bg = new ButtonGroup();
            bg.add(red);
            bg.add(blue);
            bg.add(green);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;
            add(red, gbc);
            add(blue, gbc);
            add(green, gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            JButton select = new JButton("OK");
            add(select, gbc);

            select.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ButtonModel model = bg.getSelection();
                    String selection = model == null ? "Nothing" : model.getActionCommand();
                    JOptionPane.showMessageDialog(TestPane.this, "You selected " + selection);
                }
            });

        }

    }

}

小型数据库示例

ButtonModel model = sizeButtonGroup.getSelection(); 
String size = model == null ? "Small" : model.getActionCommand();

pst.setString(4, size);

// Repeat for other groups/columns