как сделать более короткий жестко закодированный Jframe? - PullRequest
0 голосов
/ 03 сентября 2018

Я только начинающий в коде. и мы используем Net-бобы. вот пример выходных данных, которые он ожидает или хочет, чтобы мы закодировали.

образец вывода

Наш проф. хочет, чтобы мы создали программу, не используя какой-либо ярлык или другой класс (1 класс и 1 основной метод). Вот мои коды, и я хочу сделать это коротким. есть ли возможный путь?

 package chuajframe;
 import javax.swing.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;

 public class ChuaJFrame {
    static double price;
    static double subTotal = 0;
    static double subTotal1 = 0;
    static int qty;
    static double total = 0;
    static double change;
    static double bill;

    public static void main(String s[]) {

        JFrame z = new JFrame("Chua JFrame"); 

        JLabel label = new JLabel("Welcome");       
    label.setBounds(160, 0, 100, 20);

        JLabel a = new JLabel("Descrition                 Price                    
     Quantity");   a.setBounds(10, 10, 400, 50);
        //JLabel a1 = new JLabel("Price");       a1.setBounds(120,10,100, 50);
    //JLabel a2 = new JLabel("Quantity");    a2.setBounds(210,10,100, 50);

        JTextField b = new JTextField();    b.setBounds(10,50, 70, 20);
        JTextField b1= new JTextField();    b1.setBounds(120,50, 70, 20);
        JTextField b2= new JTextField();    b2.setBounds(210,50, 70, 20);
        JButton b3 = new JButton("Add");    b3.setBounds(300,50,70, 20);

        JTextField c = new JTextField();    c.setBounds(10,80, 70, 20);
        JTextField c1= new JTextField();    c1.setBounds(120,80, 70, 20);
        JTextField c2= new JTextField();    c2.setBounds(210,80, 70, 20);
        JButton c3 = new JButton("Add");    c3.setBounds(300,80,70, 20);

        JLabel label1 = new JLabel("Your about to purchase the following");label1.setBounds(90, 100, 300,30);
        JLabel label2 = new JLabel("Subtotal");  label2.setBounds(280, 120, 300,30);

        JLabel d = new JLabel();    d.setBounds(10, 150, 100, 20);
        JLabel d1 = new JLabel();  d1.setBounds(120,150,100, 20);
    JLabel d2 = new JLabel();  d2.setBounds(210,150,100, 20);
        JLabel d3 = new JLabel();  d3.setBounds(300,150,100, 20);

        JLabel e = new JLabel();   e.setBounds(10, 180, 100, 20);
        JLabel e1 = new JLabel();  e1.setBounds(120,180,100, 20);
    JLabel e2 = new JLabel();  e2.setBounds(210,180,100, 20);
        JLabel e3 = new JLabel();  e3.setBounds(300,180,100, 20);

        JLabel f = new JLabel("Total"); f.setBounds(210, 210, 100, 20);
        JLabel f1 = new JLabel();    f1.setBounds(300, 210, 100, 20);

        JTextField g = new JTextField(); g.setBounds(250,240,100,20);
        JButton g1 = new JButton("Payment");    g1.setBounds(140,240,100, 20);

        JLabel h = new JLabel();  h.setBounds(140,270,200, 20);
        JLabel h1 = new JLabel();  h1.setBounds(300,270,80, 20);
        JLabel i = new JLabel();  i.setBounds(140,285,200, 20);

        b3.addActionListener(new ActionListener(){  
            @Override
            public void actionPerformed(ActionEvent e){
            d.setText(b.getText());
            d1.setText(b1.getText());
            d2.setText(b2.getText());
            price = Double.parseDouble(d1.getText());
            qty = Integer.parseInt(d2.getText());
            subTotal = price * qty;
            d3.setText(Double.toString(subTotal));
            total = subTotal + subTotal1;
            f1.setText(Double.toString(total));
            b.setText(" ");
            b1.setText(" ");
            b2.setText(" ");
            }});

        c3.addActionListener(new ActionListener(){  
            @Override
            public void actionPerformed(ActionEvent ae){
            e.setText(c.getText());
            e1.setText(c1.getText());
            e2.setText(c2.getText());
            price = Double.parseDouble(e1.getText());
            qty = Integer.parseInt(e2.getText());
            subTotal1 = price * qty;
            e3.setText(Double.toString(subTotal1));
            total = subTotal + subTotal1;
            f1.setText(Double.toString(total));
            c.setText(" ");
            c1.setText(" ");
            c2.setText(" ");
            }});

        g1.addActionListener(new ActionListener(){  
            @Override
            public void actionPerformed(ActionEvent e){
            bill = Double.parseDouble(g.getText());

                if(bill>total)
                {
                    change = bill - total;
                    h.setText("Change");
                    h1.setText(Double.toString(change));
                    i.setText("Thank you for purchasing.");
                }
                else{
                    h.setText("Sorry insufficient payment.");
                    i.setText("Please enter your payment again!!");
                }

            }});

    z.add(label);

        z.add(a);   //z.add(a1);  z.add(a2);

        z.add(b);   z.add(b1);  z.add(b2); z.add(b3); 

        z.add(c);   z.add(c1);  z.add(c2); z.add(c3);

        z.add(label1);  z.add(label2);

        z.add(d);   z.add(d1);  z.add(d2);  z.add(d3); 

        z.add(e);   z.add(e1);  z.add(e2);  z.add(e3);

        z.add(f);   z.add(f1);

        z.add(g); z.add(g1);

        z.add(h);   z.add(h1);

        z.add(i);

    z.setSize(400,350);    
    z.setLayout(null);    
    z.setVisible(true);  
        z.setLocationRelativeTo(null);
        z.setResizable(false);
    z.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

1 Ответ

0 голосов
/ 03 сентября 2018

Вы можете сделать то, что называется составной реализацией, с большинством ваших полей. Например, включите это:

static double price;
static double subTotal = 0;
static double subTotal1 = 0;
static int qty;
static double total = 0;
static double change;
static double bill;

В это:

static double price = 0, subTotal = 0, subTotal1 = 0, total = 0, change = 0, bill = 0;
static int qty;

Или это:

JTextField b = new JTextField();
JTextField b1= new JTextField();
JTextField b2= new JTextField();
JButton b3 = new JButton("Add");

В это:

JTextField b = new JTextField(), b1= new JTextField(), b2= new JTextField(), b3 = new JButton("Add");

А затем используйте цикл for, чтобы добавить к z все вещи, которые нужно добавить внизу вашего класса

...