如何制作更短的硬编码Jframe?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何制作更短的硬编码Jframe?相关的知识,希望对你有一定的参考价值。

我只是编码的初学者。我们正在使用Net-beans。这是他期望或希望我们编码的示例输出。

sample output

我们的教授。希望我们在不使用任何捷径或其他类(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);

    }

}
答案

您可以使用大多数字段执行称为复合实例化的操作。例如,转此:

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,您需要在类的底部添加

以上是关于如何制作更短的硬编码Jframe?的主要内容,如果未能解决你的问题,请参考以下文章

通过 findById 访问活动布局中的硬编码片段

如何在java中编写更短的双数? [关闭]

如何在六个月或更短的时间内成为DevOps工程师:打包

世界上最短的时钟代码!更短的,有木有?

如何在更短的时间内向初始 DataFrame 添加大量列(5000~1000 列)?

有没有更短的写 x == y OR x == z 的方法? [复制]