2013-11-05 48 views
0

我正在創建訂購比薩餅的Java應用程序,並且有工作代碼,但不喜歡其中的文本格式。現在,項目被列出並排彼此像這樣:在JFrame中格式化文本

額外的奶酪辣香菇洋蔥總價

我想它是在一個垂直列,而不是並排。我如何去改變框架內的格式?

此外,成本約12.0美元,但我需要得到它作爲12.00美元出來。我該如何解決這個問題?

import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.ItemEvent; 
    import java.awt.event.ItemListener; 
    import java.awt.FlowLayout; 
    import java.awt.Font; 
    import javax.swing.*; 

    public class JPizzeria extends JFrame implements ActionListener, ItemListener { 
     private double Price; 
     double base = 7; 
     double toppings; 

     Font headlineFont = new Font("Courier", Font.BOLD, 50);   
     Font infoFont = new Font("Courier", Font.BOLD, 25);   
     Font info2Font = new Font("Courier", Font.BOLD, 15);   
     Font totalPriceFont = new Font("Courier", Font.BOLD, 20);   
     JLabel title = new JLabel("Hector's Pizzeria");   
     JLabel info = new JLabel("Please select the size of pizza?");   
     JLabel info2 = new JLabel("What would you like on your pizza?");   
     JCheckBox ExtraCheeseBox = new JCheckBox("Extra Cheese", false);   
     JCheckBox PepperoniBox = new JCheckBox("Pepperoni", false);   
     JCheckBox SausageBox = new JCheckBox("Sausage", false);   
     JCheckBox GroundBeefBox = new JCheckBox("Ground Beef", false);   
     JCheckBox OnionBox = new JCheckBox("Onions", false);   
     JCheckBox MushroomBox = new JCheckBox("Mushrooms", false);   
     JCheckBox BlackOlivesBox = new JCheckBox("Black Olives", false);   
     JCheckBox GreenPeppersBox = new JCheckBox("Green Peppers", false);   
     JLabel totalPrice = new JLabel("Total Price");   
     JTextField totPrice = new JTextField(10); 

     public JPizzeria() { 

      super("Hector's Pizzeria");   
      String[] pizzaSize = { "Small-$7.00", "Medium-$9.00", "Large-$11.00", 
        "Extra-Large-$14.00" };   
      JComboBox decide = new JComboBox(pizzaSize);   
      decide.addActionListener(this);   
      totPrice.addActionListener(this);   
      add(title); 
      add(info); 
      add(decide); 
      add(info2); 
     add(ExtraCheeseBox); 
     add(PepperoniBox); 
     add(SausageBox); 
     add(GroundBeefBox); 
     add(OnionBox); 
     add(MushroomBox); 
     add(BlackOlivesBox); 
     add(GreenPeppersBox); 
     add(totalPrice); 
     add(totPrice); 

     totPrice.setText("$"); 
     ExtraCheeseBox.addItemListener(this); 
     PepperoniBox.addItemListener(this); 
     SausageBox.addItemListener(this); 
     GroundBeefBox.addItemListener(this); 
     OnionBox.addItemListener(this); 
     MushroomBox.addItemListener(this); 
     BlackOlivesBox.addItemListener(this); 
     GreenPeppersBox.addItemListener(this); 

     title.setFont(headlineFont); 
     info.setFont(infoFont); 
     info2.setFont(info2Font); 
     totalPrice.setFont(totalPriceFont); 
     setLayout(new FlowLayout(FlowLayout.CENTER)); 
     decide.setSelectedIndex(3); 
    } 

    public static void main(String[] args) { 
     JPizzeria Pizza = new JPizzeria(); 
     final int WIDTH = 850; 
     final int HEIGHT = 650; 
     Pizza.setSize(WIDTH, HEIGHT); 
     Pizza.setVisible(true); 
     Pizza.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public void actionPerformed(ActionEvent e) { 
     JComboBox decide = (JComboBox) e.getSource(); 
     String pizzaSize = (String) decide.getSelectedItem();  
     System.out.println(pizzaSize);  
     if (pizzaSize.equals("Small-$7.00")) 
      Price = base;  
     else if (pizzaSize.equals("Medium-$9.00")) 
      Price = base + 2;  
     else if (pizzaSize.equals("Large-$11.00")) 
      Price = base + 4;  
     else 
      Price = base + 7;  
     System.out.println(totalPrice); 
     totPrice.setText("$" + (Price + toppings)); 
    } 

    public void itemStateChanged(ItemEvent event) { 
     Object source = event.getSource(); 
     int select = event.getStateChange();  
     if (source == ExtraCheeseBox) { 
      if (select == ItemEvent.SELECTED) { 
       toppings += 1; 
      } else 
       toppings -= 1; 
     } else if (source == PepperoniBox) { 
      if (select == ItemEvent.SELECTED) 
       toppings += 1.00; 
      else 
       toppings -= 1.00; 
     } else if (source == SausageBox) { 
      if (select == ItemEvent.SELECTED) 
       toppings += 1.00; 
      else 
       toppings -= 1.00; 
     } else if (source == GroundBeefBox) { 
      if (select == ItemEvent.SELECTED) 
       toppings += 1.00; 
      else 
       toppings -= 1.00; 
     } else if (source == OnionBox) { 
      if (select == ItemEvent.SELECTED) 
       toppings += 1.00; 
      else 
       toppings -= 1.00; 
     } else if (source == MushroomBox) { 
      if (select == ItemEvent.SELECTED) 
       toppings += 1.00; 
      else 
       toppings -= 1.00; 
     } else if (select == ItemEvent.SELECTED) 
      toppings += 1.00; 
     else 
      toppings -= 1.00; 
     totPrice.setText("$ " + toppings); 
    } 
} 
+0

您可以將佈局管理器更改爲類似網格佈局或GridBagLayout中或使用HTML格式字符串的單個組件/佈局 – MadProgrammer

回答

1

您可以使用小數點後的事項獲取總額爲12.00美元。請對您的代碼進行以下更改。

//Declare decimal formatter 
DecimalFormat fmt = new DecimalFormat("0.00"); 

//Format total amount before showing it in rotal 
totPrice.setText("$" + (fmt.format(Price + toppings))); 
totPrice.setText("$ " +fmt.format(toppings)); 
+0

謝謝!我現在有小數點格式。 :) –

+0

@HectorRamos歡迎 –

+0

我仍然試圖找出方式來做頂部選擇的格式,但可能不得不忍受它。 –