2014-03-02 72 views
0

這是我的代碼,我只是想打個收據,當您選中該複選框在它那裏有值,並將其與值打印標籤連續如何把值複選框

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(); 

    } 
} 

希望它很清楚,你是我想要的代碼發生的格式

+1

_「希望你很清楚我想用代碼來實現什麼」_--實際上並非如此。你還沒有真正問過一個問題。什麼不工作? –

+1

我的水晶球說:對於在'GUI'中選擇的每個複選框,您希望將'JLabel'添加到'Receipt'框架。該標籤應包含與所選複選框的字符串相同的字符串。它是否正確? – Marco13

+0

是的。而且我還希望在收貨框處顯示的複選框的值以及與複選框相同的字符串。 – futureprogrammer

回答

1

我猜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; 
    } 
} 
+0

好電話......... –