gpt4 book ai didi

java - 如何在复选框中输入值

转载 作者:行者123 更新时间:2023-12-01 13:19:54 25 4
gpt4 key购买 nike

这是我的代码,我只想制作一张收据,当您选中复选框时,其中会有值,并且它会连续打印带有该值的标签

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GUI extends JFrame implements ActionListener

{

private JLabel label;
private JCheckBox checkbox,checkbox2,checkbox3,checkbox4,checkbox5,
checkbox6,checkbox7,checkbox8,checkbox9,
checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,
checkbox16,checkbox17,checkbox18,checkbox19,checkbox20;
private JButton button;


public GUI()
{
Container pane= getContentPane();
pane.setLayout(new GridLayout(15,1));

JPanel jp= new JPanel();

label= new JLabel("McDonald's");
checkbox = new JCheckBox("Cheese Burger");
checkbox2 = new JCheckBox("Big Mac");
checkbox3 = new JCheckBox("Big n' Tasty");
checkbox4 = new JCheckBox("McSpicy");
checkbox5 = new JCheckBox("Quarter Pounder with Cheese");
checkbox6 = new JCheckBox("Double Cheeseburger");
checkbox7 = new JCheckBox("McChicken Sandwich");
checkbox8 = new JCheckBox("Filet-O-Fish");
checkbox9 = new JCheckBox("Cheeseburger Deluxe");
checkbox10 = new JCheckBox("Burger Mcdo");
checkbox11 = new JCheckBox("Chicken Filet with Drinks");
checkbox12 = new JCheckBox("Spaghetti with Drinks");
checkbox13 = new JCheckBox("Hot Fudge Sundae ");
checkbox14 = new JCheckBox("Caramel Sundae");
checkbox15 = new JCheckBox("Large Mcdo French Fries");
checkbox16 = new JCheckBox("Chicken Nuggets with coke");
checkbox17 = new JCheckBox("Coke Float");
checkbox18 = new JCheckBox("Green Apple Float");
checkbox19 = new JCheckBox("Crispy Chicken with rice");
checkbox20 = new JCheckBox("Oreo Sundae");
button = new JButton("Order Now");

add(label);

pane.add(checkbox);
pane.add(checkbox2);
pane.add(checkbox3);
pane.add(checkbox4);
pane.add(checkbox5);
pane.add(checkbox6);
pane.add(checkbox7);
pane.add(checkbox8);
pane.add(checkbox9);
pane.add(checkbox10);
pane.add(checkbox11);
pane.add(checkbox12);
pane.add(checkbox13);
pane.add(checkbox14);
pane.add(checkbox15);
pane.add(checkbox16);
pane.add(checkbox17);
pane.add(checkbox18);
pane.add(checkbox19);
pane.add(checkbox20);

add(button);
add(jp, BorderLayout.SOUTH);
label.setHorizontalAlignment(JLabel.CENTER);

button.setSize(10,10);
button.addActionListener(this);

setSize(500,550);
setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
String gulp = e.getActionCommand();
{
if ( gulp.equals("Order Now"))
{
new Receipt();
setVisible(true);
dispose();
}
}
}
public static void main(String[] args)
{
GUI r= new GUI();
}
}

这是我的第二个代码,这是我想要实现的收据格式

    import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Receipt extends JFrame
{

private JLabel label,label2,label3,label4,label5,
label6,label7,label8,label9,label10;;


public Receipt()
{
Container pane= getContentPane();
pane.setLayout(new GridLayout(13,1));

label= new JLabel("MC DONALD'S");
label2= new JLabel("McDonald's Tandang Sora");
label3= new JLabel("#22 Tandang Sora Corner, Commonwealth Avenue, Quezon City");
label4= new JLabel("Telephone# (02)86236");
label5= new JLabel("MACHINE SERIAL NUMBER: D123HJ01");


label6= new JLabel("Card Issuer: Sharina Tortoles");
label7= new JLabel("Account Number# 337163990");
label8= new JLabel("February 22, 2014 12:45");
label9= new JLabel("Thank You for choosing Mcdonald's");
label10= new JLabel("Please come again");


label.setHorizontalAlignment(JLabel.CENTER);
label2.setHorizontalAlignment(JLabel.CENTER);
label3.setHorizontalAlignment(JLabel.CENTER);
label4.setHorizontalAlignment(JLabel.CENTER);
label5.setHorizontalAlignment(JLabel.CENTER);
label6.setHorizontalAlignment(JLabel.CENTER);
label7.setHorizontalAlignment(JLabel.CENTER);
label8.setHorizontalAlignment(JLabel.CENTER);
label9.setHorizontalAlignment(JLabel.CENTER);
label10.setHorizontalAlignment(JLabel.CENTER);

pane.add(label, BorderLayout.NORTH);
pane.add(label2);
pane.add(label3);
pane.add(label4);
pane.add(label5);
pane.add(label6);
pane.add(label7);
pane.add(label8);
pane.add(label9);
pane.add(label10);


setSize(500,500);
setVisible(true);
}

public static void main(String[] args)
{
Receipt g= new Receipt();

}
}

希望您清楚我希望通过代码实现什么

最佳答案

我猜 EnumSet 就是您正在寻找的:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

enum Receipt {
CheeseBurger("Cheese Burger"),
BigMac("Big Mac"),
BigNTasty("Big n' Tasty"),
McSpicy("McSpicy"),
QuarterPounderWithCheese("Quarter Pounder with Cheese"),
DoubleCheeseburger("Double Cheeseburger"),
McChickenSandwich("McChicken Sandwich");
//JCheckBox("Filet-O-Fish"),
//JCheckBox("Cheeseburger Deluxe"),
//JCheckBox("Burger Mcdo"),
//JCheckBox("Chicken Filet with Drinks"),
//JCheckBox("Spaghetti with Drinks"),
//JCheckBox("Hot Fudge Sundae "),
//JCheckBox("Caramel Sundae"),
//JCheckBox("Large Mcdo French Fries"),
//JCheckBox("Chicken Nuggets with coke"),
//JCheckBox("Coke Float"),
//JCheckBox("Green Apple Float"),
//JCheckBox("Crispy Chicken with rice"),
//JCheckBox("Oreo Sundae");
private final String str;
private Receipt(String str) {
this.str = str;
}
@Override public String toString() {
return str;
}
}

public class GUI2 {
private JButton button = new JButton(new AbstractAction("Order Now") {
@Override public void actionPerformed(ActionEvent e) {
EnumSet<Receipt> r = EnumSet.noneOf(Receipt.class);
for (ReceiptCheckBox c: list) {
if (c.isSelected()) {
r.add(c.getReceipt());
}
}
JOptionPane.showMessageDialog((JComponent) e.getSource(), r);
}

});
private List<ReceiptCheckBox> list = new ArrayList<>(Receipt.values().length);

public JComponent makeUI() {
JPanel p = new JPanel();
for (Receipt r: Receipt.values()) {
ReceiptCheckBox c = new ReceiptCheckBox(r);
list.add(c);
p.add(c);
}
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("McDonald's"), BorderLayout.NORTH);
panel.add(button, BorderLayout.SOUTH);
panel.add(p);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new GUI2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class ReceiptCheckBox extends JCheckBox {
private final Receipt receipt;
public ReceiptCheckBox(Receipt receipt) {
super(receipt.toString());
this.receipt = receipt;
}
public Receipt getReceipt() {
return receipt;
}
}

关于java - 如何在复选框中输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129517/

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