遇到JLabels和Jframe的问题

时间:2012-03-13 13:45:51

标签: java swing jframe jlabel

因此,我必须制作一个程序,用户可以输入他们为早餐,午餐和晚餐订购的每餐的数量。然后我必须显示他们每顿饭订购的金额以及每餐的总费用,税金等等。所以我在JFrame中制作了一个网格来显示结果但是我的Jlab标签一直出现错误,他们的数字是; 8,9,10,13,14,15,18,19,35,40,45。除了JLabel之外我还能用什么来显示结果吗?感谢

import java.awt.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;

public class MealCost extends JFrame
{

// Constants

    static Scanner console = new Scanner (System.in);

    static final double Sales_Tax = .10;
    static final double Breakfast = 5.50;
    static final double DiscountB = .10;
    static final double Lunch = 9.50;
    static final double DiscountL = .15;
    static final double Dinner = 16.50;
    static final double DiscountD = .08;

    public static void main(String[] args)
    {


// Variables and their known values

    int breakfast = 0;
    int lunch = 0;
    int dinner = 0;

    double tax = 0;
    double BreakfastCost = 0;
    double Breakfast_D = 0;
    double LunchCost = 0;
    double Lunch_D = 0;
    double DinnerCost = 0;
    double Dinner_D = 0;
    double total_b = 0;
    double total_l = 0;
    double total_d = 0;
    double total = 0;
    double total_due = 0;

    char Input; 
    String InputStr;
    String outputMsg;


    // This program will display the total cost and discount for each meal of the day.

    // Output Message
    outputMsg = "This program will prompt you for the total number of meals ordered for each meal time. \n"
                 + "Breakfast cost $5.50 per meal, but if 10 or more are ordered there is a 10% discount. \n"
                 + "Lunch cost $9.50 per meal, but if 15 or more are ordered there is a 15% discount. \n"
                 + "Dinner cost $16.50 per meal, but if 8 or more are ordered there is a 12% discount. \n";

    JOptionPane.showMessageDialog(null,outputMsg,"My Favorite Catering Service",
                JOptionPane.INFORMATION_MESSAGE);


    // User Input


        // Ask user if any meals where ordered.
    InputStr = JOptionPane.showInputDialog ("Were any meals ordered today (Y)es or (N)o : ");
    while ((InputStr.length() > 1) || (InputStr.length() == 0 ))
    {
        InputStr = JOptionPane.showInputDialog ("Only enter a single characater (Y) or (N) : ");
    }

    Input = InputStr.charAt(0);

    switch (Input)
    {
    case 'Y': 
    case 'y':

            // Meals for breakfast ordered
            InputStr = JOptionPane.showInputDialog ("Enter the number of Breakfast ordered : ");
            breakfast = Integer.parseInt(InputStr);

            // Meals for lunch ordered
            InputStr = JOptionPane.showInputDialog ("Enter the number of Lunch ordered : ");
            lunch = Integer.parseInt(InputStr);

            // Meals for dinner ordered
            InputStr = JOptionPane.showInputDialog ("Enter the number of Dinner ordered : ");
            dinner = Integer.parseInt(InputStr);

            // Breakfast discount
            if (breakfast >= 10) 
                Breakfast_D = Breakfast * breakfast * DiscountB;


            total_b = Breakfast * breakfast - Breakfast_D;

            // Lunch discount
            if (lunch >= 15) 
                Lunch_D = Lunch * lunch * DiscountL;


            total_l = Lunch * lunch - Lunch_D;

            // Dinner discount
            if (dinner >= 8) 
                Dinner_D = Dinner * dinner * DiscountD;


            total_d = Dinner * dinner - Dinner_D;



            break;

    case 'N':
    case 'n':

        // Output Message
        outputMsg = "Thank you for placing your order with us.";
        JOptionPane.showMessageDialog(null,outputMsg ,"My Favorite Catering Service",
                JOptionPane.INFORMATION_MESSAGE);

        System.exit(0);
        break;
    default:

            // Calculations 
            tax = (total_b + total_l + total_d) * Sales_Tax;
            total = total_b + total_l + total_d;
            total_due = tax + total;


        // output message
        outputMsg = Input + " was an invalid response. \n Thank you for ording with us";
        JOptionPane.showMessageDialog(null,outputMsg ,"My Favorite Catering Service",
                JOptionPane.INFORMATION_MESSAGE);

        System.exit(0);

    }

    // Results page


    JFrame frame = new JFrame("My Favorite Catering Service");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,400);
    frame.setLocationRelativeTo(null);
    JPanel panel = new JPanel ();
    panel.setLayout (new GridLayout(9,5,5,10));
    JLabel label1 = new JLabel ("Meal");
    JLabel label2 = new JLabel ("Quantity");
    JLabel label3 = new JLabel ("Cost");
    JLabel label4 = new JLabel ("Discount");
    JLabel label5 = new JLabel ("Cost After Discount");
    JLabel label6 = new JLabel ("Breakfast");
    JLabel label7 = new JLabel ("", breakfast);
    JLabel label8 = new JLabel ("$%.2f", Breakfast * breakfast);
    JLabel label9 = new JLabel ("$%.2f", Breakfast_D);
    JLabel label10 = new JLabel ("$%.2f", total_b);
    JLabel label11 = new JLabel ("Lunch");
    JLabel label12 = new JLabel ("", lunch);
    JLabel label13 = new JLabel ("$%.2f", Lunch * lunch);
    JLabel label14 = new JLabel ("$%.2f", Lunch_D);
    JLabel label15 = new JLabel ("$%.2f", total_l);
    JLabel label16 = new JLabel ("Dinner");
    JLabel label17 = new JLabel ("", dinner);
    JLabel label18 = new JLabel ("$%.2f", Dinner * dinner);
    JLabel label19 = new JLabel ("$%.2f", total_d);
    JLabel label20 = new JLabel ("");
    JLabel label21 = new JLabel ("");
    JLabel label22 = new JLabel ("");
    JLabel label23 = new JLabel ("");
    JLabel label24 = new JLabel ("");
    JLabel label25 = new JLabel ("");
    JLabel label26 = new JLabel ("");
    JLabel label27 = new JLabel ("");
    JLabel label28 = new JLabel ("");
    JLabel label29 = new JLabel ("");
    JLabel label30 = new JLabel ("");
    JLabel label31 = new JLabel ("Total");
    JLabel label32 = new JLabel ("");
    JLabel label33 = new JLabel ("");
    JLabel label34 = new JLabel ("");
    JLabel label35 = new JLabel ("$%.2f", total);
    JLabel label36 = new JLabel ("Sales Tax");
    JLabel label37 = new JLabel ("");
    JLabel label38 = new JLabel ("");
    JLabel label39 = new JLabel ("");
    JLabel label40 = new JLabel ("$%.2f", tax);
    JLabel label41 = new JLabel ("Total Due");
    JLabel label42 = new JLabel ("");
    JLabel label43 = new JLabel ("");
    JLabel label44 = new JLabel ("");
    JLabel label45 = new JLabel ("$%.2f", total_due);
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    panel.add(label1);
    panel.add(label2);
    panel.add(label3);
    panel.add(label4);
    panel.add(label5);
    panel.add(label6);
    panel.add(label7);
    panel.add(label8);
    panel.add(label9);
    panel.add(label10);
    panel.add(label11);
    panel.add(label12);
    panel.add(label13);
    panel.add(label14);
    panel.add(label15);
    panel.add(label16);
    panel.add(label17);
    panel.add(label18);
    panel.add(label19);
    panel.add(label20);
    panel.add(label21);
    panel.add(label22);
    panel.add(label23);
    panel.add(label24);
    panel.add(label25);
    panel.add(label26);
    panel.add(label27);
    panel.add(label28);
    panel.add(label29);
    panel.add(label30);
    panel.add(label31);
    panel.add(label32);
    panel.add(label33);
    panel.add(label34);
    panel.add(label35);
    panel.add(label36);
    panel.add(label37);
    panel.add(label38);
    panel.add(label39);
    panel.add(label40);
    panel.add(label41);
    panel.add(label42);
    panel.add(label43);
    panel.add(label44);
    panel.add(label45);
    frame.add(panel);

}
}

3 个答案:

答案 0 :(得分:6)

使用JTable而非一堆JLabelJTable是Swing中最复杂的JComponent,例如在上一个row/column中您可以创建总和特别是row/column

enter image description here enter image description here

来自scode的

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableProcessing extends JFrame implements TableModelListener {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableProcessing() {
        String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
        Object[][] data = {
            {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
            {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
            {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
            {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                int modelColumn = table.convertColumnIndexToModel(column);
                return (modelColumn == 3) ? false : true;
            }
        }; 
        model.addTableModelListener(this);
        table = new JTable(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    @Override
    public void tableChanged(TableModelEvent e) {
        System.out.println(e.getSource());
        if (e.getType() == TableModelEvent.UPDATE) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            if (column == 1 || column == 2) {
                TableModel model = table.getModel();
                int quantity = ((Integer) model.getValueAt(row, 1)).intValue();
                double price = ((Double) model.getValueAt(row, 2)).doubleValue();
                Double value = new Double(quantity * price);
                model.setValueAt(value, row, 3);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableProcessing frame = new TableProcessing();
            }
        });
    }
}

答案 1 :(得分:0)

JLabel没有任何替代方法,但您可以在一个JLable下使用单个HTML,

String html = "Meal<br />Quantity<br /> .... so on" 
JLabel label = new JLabel(html);

您可以添加单个JLabel。

答案 2 :(得分:0)

试试这个。

double tmp = (Breakfast * breakfast);
JLabel label8 = new JLabel (Double.toString(tmp));