gpt4 book ai didi

java - 单选按钮未出现在 JFrame 中

转载 作者:行者123 更新时间:2023-12-01 21:55:32 26 4
gpt4 key购买 nike

我的JRadioButton 未出现在JFrame 中。

它应该出现在付款方式下方。如果用户选择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

最佳答案

您没有将 buttons[0]buttons[1] 添加到面板。尝试在该行 add(methodLabel); 之后添加它们并检查结果。

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

关于java - 单选按钮未出现在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com