单选按钮不会出现在JFrame中

时间:2015-12-24 10:04:57

标签: java swing awt jradiobutton

我的JRadioButton未显示在JFrame中。

它应显示在Method of payment下方。如果用户选择creditRadioButton,则会显示creditCardLabelcreditTextField

代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class FinishTransaction extends JFrame implements ActionListener{
    JLabel totalLabel;
    JLabel nameLabel;
    JLabel addressLabel;
    JLabel contactLabel;
    JLabel custPaymentLabel;
    JLabel changeLabel;
    JLabel methodLabel;
    JLabel creditCardLabel;
    JTextField totalTextField;
    JTextField nameTextField;
    JTextField addressTextField;
    JTextField contactTextField;
    JTextField custPaymentTextField;
    JTextField changeTextField;
    JTextField creditCardTextField;
    final ButtonGroup bGroup = new ButtonGroup();
    final JRadioButton[] buttons = new JRadioButton[2];
    JButton checkoutButton;

    static FinishTransaction fin = new FinishTransaction();

    public FinishTransaction(){
        super("Finish Transaction-Print Receipt");
        setLayout(null);
        totalLabel = new JLabel("Total Payment: ");
        nameLabel = new JLabel("Name: ");
        addressLabel = new JLabel("Address: ");
        contactLabel = new JLabel("Contact: ");
        custPaymentLabel = new JLabel("Payment: ");
        changeLabel = new JLabel("Change: ");
        totalTextField = new JTextField("");
        nameTextField = new JTextField("");
        addressTextField = new JTextField("");
        contactTextField = new JTextField("");
        custPaymentTextField = new JTextField("");
        changeTextField = new JTextField("");
        methodLabel = new JLabel("Method of payment: ");
        creditCardLabel = new JLabel("Credit Card number: ");
        creditCardTextField = new JTextField("");
        checkoutButton = new JButton("Checkout");
        buttons[0] = new JRadioButton("Cash");
        buttons[1] = new JRadioButton("Credit Card");

        totalTextField.setEditable(false);
        changeTextField.setEditable(false);
        creditCardLabel.setVisible(false);
        creditCardTextField.setVisible(false);

        nameLabel.setBounds(50,60,100,20);     nameTextField.setBounds(130,60,200,20);
        addressLabel.setBounds(50,80,100,20);     addressTextField.setBounds(130,80,200,20);
        contactLabel.setBounds(50,100,100,20);     contactTextField.setBounds(130,100,200,20);
        methodLabel.setBounds(50,130,200,20);    
        buttons[0].setBounds(50,150,100,20); 
        creditCardLabel.setBounds(90,170,150,20);     creditCardTextField.setBounds(210,170,200,20);
        buttons[1].setBounds(50,190,100,20);   
        totalLabel.setBounds(50,230,100,20);     totalTextField.setBounds(140,230,200,20);
        custPaymentLabel.setBounds(50,250,100,20);     custPaymentTextField.setBounds(130,250,200,20);
        changeLabel.setBounds(50,270,100,20);     changeTextField.setBounds(130,270,200,20);
                                                    checkoutButton.setBounds(130,310,200,20);

        add(nameLabel);
        add(addressLabel);
        add(contactLabel);
        add(methodLabel);
        bGroup.add(buttons[0]);
        bGroup.add(buttons[1]);
        add(creditCardLabel);
        add(totalLabel);
        add(custPaymentLabel);
        add(changeLabel);
        add(nameTextField);
        add(addressTextField);
        add(contactTextField);
        add(creditCardTextField);
        add(totalTextField);
        add(custPaymentTextField);
        add(changeTextField);
        add(checkoutButton);

        buttons[0].setSelected(true);

        buttons[0].addActionListener(this);
        buttons[1].addActionListener(this);
        checkoutButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(checkoutButton.getName().equals(((Component)e.getSource()).getName())){
        }
        if(buttons[0].isSelected()){
            creditCardLabel.setVisible(true);
            creditCardTextField.setVisible(true);
        }
    }
    public static void main(String args[]){
        fin.setVisible(true);
        fin.setResizable(false);
        fin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fin.setSize(500,400);
    }
}

输出:
 enter image description here

1 个答案:

答案 0 :(得分:3)

您未向面板添加buttons[0]buttons[1]。尝试在此行add(methodLabel);之后添加它们并检查结果。

add(buttons[0]);
add(buttons[1]);