在JFrame中格式化文本

时间:2013-11-05 02:28:45

标签: java swing decimalformat

我正在创建一个Java应用程序,用于订购比萨饼并拥有有效的代码,但不喜欢其中文本的格式。现在,这些项目按照这样并排列出:

额外的奶酪意大利辣香肠蘑菇洋葱总价

我希望它在垂直列而不是并排。如何更改框架内的格式?

此外,成本大约为12美元,但我需要以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);
    }
}

1 个答案:

答案 0 :(得分: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));