Java JComponents don';直到鼠标越过或我最小化窗口和JTextField问题时才会出现

Java JComponents don';直到鼠标越过或我最小化窗口和JTextField问题时才会出现,java,swing,user-interface,bluej,Java,Swing,User Interface,Bluej,我是来自菲律宾的10年级学生,现在是关于Java和GUI的。只有基本的。我们的项目是制作一个POS系统。我已经做了一个,但是有一些小问题: JComponents不会出现,除非我最小化窗口,然后重新打开它(所有内容都会显示),或者我用鼠标(一个接一个)悬停在它们上面 对于“已付金额”(screen2)文本字段,我使用键盘输入字符串,它首先在第一次输入之前显示“null”,因为该文本字段首先设置为显示“0”,然后接受来自键盘的输入。但是当我第一次在程序刚刚运行时输入东西时,它首先显示“null”。

我是来自菲律宾的10年级学生,现在是关于Java和GUI的。只有基本的。我们的项目是制作一个POS系统。我已经做了一个,但是有一些小问题:

  • JComponents不会出现,除非我最小化窗口,然后重新打开它(所有内容都会显示),或者我用鼠标(一个接一个)悬停在它们上面

  • 对于“已付金额”(screen2)文本字段,我使用键盘输入字符串,它首先在第一次输入之前显示“null”,因为该文本字段首先设置为显示“0”,然后接受来自键盘的输入。但是当我第一次在程序刚刚运行时输入东西时,它首先显示“null”。它可以被清除,但可能非常混乱

  • 请原谅我是一个业余爱好者,因为我只是一个学生,这只是一个项目。还有,这是我第一次来这里,如果我做了什么奇怪的事,我很抱歉!我使用BlueJ顺便说一句。请,我请求您的帮助。谢谢大家!

    以下是我的源代码:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BalikBukid extends JFrame{
    public static final int WIDTH = 1500;
    public static final int HEIGHT = 750;
    
    private JLabel BalikBukidFarmKitchen, PriceToPay, AmountPaid;
    private JButton CaesarSalad, FarmSalad, Karekare, BeefTaPao, Adobo, MushroomChicken, KaningAsul, KaningDilaw, IceCreamBasket, TsokolateIsland;
    private JButton Starters, Beef, Chicken, Rice, Dessert;
    private JButton one, two, three, four, five, six, seven, eight, nine, zero, clear;
    private JButton order, endorders;
    private JTextField screen1, screen2;
    
    private static int OrderPrice = 0, Cash, caesars = 0, farmsalds = 0, kare = 0, tapao = 0, adobs = 0, mushroom = 0, asul = 0, dilaw = 0, icecream = 0, islands = 0;;
    private static String PayingScreen;
    
    private OrderHandler ordhandler = new OrderHandler();
    private MoneyHandler monhandler = new MoneyHandler();
    private TransactionHandler transhandler = new TransactionHandler();
    private TotalOrdersHandler tordhandler = new TotalOrdersHandler();
    public BalikBukid(){
        Container pane = getContentPane();
        pane.setLayout(null);
        setTitle("Balik Bukid POS System");        
        setSize(WIDTH, HEIGHT);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pane.setBackground(Color.green);
    
        //Instantiate the labels and buttons
        BalikBukidFarmKitchen = new JLabel("Balik Bukid Farm & Kitchen");
        Starters = new JButton("STARTERS");
        Beef = new JButton("BEEF");
        Chicken = new JButton("CHICKEN");
        Rice = new JButton("RICE");
        Dessert = new JButton("DESSERT");
        PriceToPay  = new JLabel("Price to pay: ");
        AmountPaid = new JLabel("Price paid: ");
        CaesarSalad = new JButton("<html><center><b>Classic Caesar<br>Salad</b><br>Php 160</center></html>");
        FarmSalad = new JButton("<html><center><b>Balik Bukid<br>Farm Salad</b><br>Php 160</center></html>");
        Karekare = new JButton("<html><center><b>Ang Kare-kare</b><br>Php 210</center></html>");
        BeefTaPao = new JButton("<html><center><b>Garlic Beef<br>Ta Pao</b><br>Php 180</center></html>");
        Adobo = new JButton("<html><center><b>Binalot na<br>Adobong Dilaw</b><br>Php 145</center></html>");
        MushroomChicken = new JButton("<html><center><b>Mushroom Chicken</b><br>Php 160</center></html>");
        KaningAsul = new JButton("<html><center><b>Kaning Asul</b><br>Php 35</center></html>");
        KaningDilaw = new JButton("<html><center><b>Kaning Dilaw</b><br>Php 35</center></html>");
        IceCreamBasket = new JButton("<html><center><b>Ice Cream<br>Basket</b><br>Php 110</center></html>");
        TsokolateIsland = new JButton("<html><center><b>Tsokolate Islands</b><br>Php 180<</center></html>");
        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");
        zero = new JButton("0");
        order = new JButton("TRANSACT");
        endorders = new JButton("CHECK OUT");
        screen1 = new JTextField();
        screen2 = new JTextField();
        clear = new JButton("CLEAR");
    
        //Set the bounds of each button and label
        BalikBukidFarmKitchen.setBounds(570, 10, 300, 50);
        CaesarSalad.setBounds(250, 100, 100, 70);
        FarmSalad.setBounds(355, 100, 100, 70);
        Starters.setBounds(90, 100, 150, 70);
        Karekare.setBounds(250, 180, 100, 70);
        BeefTaPao.setBounds(355, 180, 100, 70);
        Beef.setBounds(90, 180, 150, 70);
        Adobo.setBounds(250, 260, 100, 70);
        MushroomChicken.setBounds(355, 260, 100, 70);
        Chicken.setBounds(90, 260, 150, 70);
        KaningAsul.setBounds(250, 340, 100, 70);
        KaningDilaw.setBounds(355, 340, 100, 70);
        Rice.setBounds(90, 340, 150, 70);
        IceCreamBasket.setBounds(250, 420, 100, 70);
        TsokolateIsland.setBounds(355, 420, 100, 70);
        Dessert.setBounds(90, 420, 150, 70);
        one.setBounds(950, 150, 80, 60);
        two.setBounds(1030, 150, 80, 60);
        three.setBounds(1110, 150, 80, 60);
        four.setBounds(950, 210, 80, 60);
        five.setBounds(1030, 210, 80, 60);
        six.setBounds(1110, 210, 80, 60);
        seven.setBounds(950, 270, 80, 60);
        eight.setBounds(1030, 270, 80, 60);
        nine.setBounds(1110, 270, 80, 60);
        zero.setBounds(950, 330, 80, 60);
        clear.setBounds(1030, 330, 160, 60);
        order.setBounds(570, 500, 120, 60);
        endorders.setBounds(720,500, 120, 60);
        screen1.setBounds(600, 150, 200, 45);
        screen2.setBounds(600, 300, 200, 45);
        PriceToPay.setBounds(600, 130, 200, 20);
        AmountPaid.setBounds(600, 280, 200, 20);
    
        //Formatting
        BalikBukidFarmKitchen.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        BalikBukidFarmKitchen.setForeground(Color.BLUE);
        PriceToPay.setFont(new Font("Trebuchet MS", Font.ITALIC, 15));
        PriceToPay.setForeground(Color.BLUE);
        AmountPaid.setFont(new Font("Trebuchet MS", Font.ITALIC, 15));
        AmountPaid.setForeground(Color.BLUE);
        Starters.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        Starters.setForeground(Color.RED);
        Starters.setBackground(Color.WHITE);
        Beef.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        Beef.setForeground(Color.RED);
        Beef.setBackground(Color.ORANGE);
        Chicken.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        Chicken.setForeground(Color.RED);
        Chicken.setBackground(Color.PINK);
        Rice.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        Rice.setForeground(Color.RED);
        Rice.setBackground(Color.YELLOW);
        Dessert.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
        Dessert.setForeground(Color.RED);
        Dessert.setBackground(Color.MAGENTA);
        screen1.setText("0");
        screen2.setText("0");
        screen1.setFont(new Font("Trebuchet MS", Font.BOLD, 19));
        screen2.setFont(new Font("Trebuchet MS", Font.BOLD, 19));
        screen1.setHorizontalAlignment(JTextField.CENTER);
        screen2.setHorizontalAlignment(JTextField.CENTER);
        screen1.setEditable(false);
        screen2.setEditable(false);
    
        //Add Action Listener
        CaesarSalad.addActionListener(ordhandler);
        FarmSalad.addActionListener(ordhandler);
        Karekare.addActionListener(ordhandler);
        BeefTaPao.addActionListener(ordhandler);
        Adobo.addActionListener(ordhandler);
        MushroomChicken.addActionListener(ordhandler);
        KaningAsul.addActionListener(ordhandler);
        KaningDilaw.addActionListener(ordhandler);
        IceCreamBasket.addActionListener(ordhandler);
        TsokolateIsland.addActionListener(ordhandler);
        one.addActionListener(monhandler);
        two.addActionListener(monhandler);
        three.addActionListener(monhandler);
        four.addActionListener(monhandler);
        five.addActionListener(monhandler);
        six.addActionListener(monhandler);
        seven.addActionListener(monhandler);
        eight.addActionListener(monhandler);
        nine.addActionListener(monhandler);
        zero.addActionListener(monhandler);
        clear.addActionListener(monhandler);
        order.addActionListener(transhandler);
        endorders.addActionListener(tordhandler);
    
        //Add to the container
        pane.add(BalikBukidFarmKitchen);
        pane.add(PriceToPay);
        pane.add(AmountPaid);
        pane.add(CaesarSalad);
        pane.add(FarmSalad);
        pane.add(Karekare);
        pane.add(BeefTaPao);
        pane.add(Adobo);
        pane.add(MushroomChicken);
        pane.add(KaningAsul);
        pane.add(KaningDilaw);
        pane.add(IceCreamBasket);
        pane.add(TsokolateIsland);
        pane.add(screen1);
        pane.add(screen2);
        pane.add(one);
        pane.add(two);
        pane.add(three);
        pane.add(four);
        pane.add(five);
        pane.add(six);
        pane.add(seven);
        pane.add(eight);
        pane.add(nine);
        pane.add(zero);
        pane.add(clear);
        pane.add(order);
        pane.add(endorders);
        pane.add(Starters);
        pane.add(Beef);
        pane.add(Chicken);
        pane.add(Rice);
        pane.add(Dessert);
    }
    public class OrderHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String StringOrderPrice;
            if(e.getActionCommand().equals("<html><center><b>Classic Caesar<br>Salad</b><br>Php 160</center></html>")){
                OrderPrice += 160;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                caesars++;
            }else if(e.getActionCommand().equals("<html><center><b>Balik Bukid<br>Farm Salad</b><br>Php 160</center></html>")){
                OrderPrice += 160;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                farmsalds++;
            }else if(e.getActionCommand().equals("<html><center><b>Ang Kare-kare</b><br>Php 210</center></html>")){
                OrderPrice += 210;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                kare++;
            }else if(e.getActionCommand().equals("<html><center><b>Garlic Beef<br>Ta Pao</b><br>Php 180</center></html>")){
                OrderPrice += 180;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                tapao++;
            }else if(e.getActionCommand().equals("<html><center><b>Binalot na<br>Adobong Dilaw</b><br>Php 145</center></html>")){
                OrderPrice += 145;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                adobs++;
            }else if(e.getActionCommand().equals("<html><center><b>Mushroom Chicken</b><br>Php 160</center></html>")){
                OrderPrice += 160;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                mushroom++;
            }else if(e.getActionCommand().equals("<html><center><b>Kaning Asul</b><br>Php 35</center></html>")){
                OrderPrice += 35;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                asul++;
            }else if(e.getActionCommand().equals("<html><center><b>Kaning Dilaw</b><br>Php 35</center></html>")){
                OrderPrice += 35;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                dilaw++;
            }else if(e.getActionCommand().equals("<html><center><b>Ice Cream<br>Basket</b><br>Php 110</center></html>")){
                OrderPrice += 110;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                icecream++;
            }else if(e.getActionCommand().equals("<html><center><b>Tsokolate Islands</b><br>Php 180<</center></html>")){
                OrderPrice += 180;
                StringOrderPrice = Integer.toString(OrderPrice);
                screen1.setText(StringOrderPrice);
                islands++;
            }
        }
    }
    public class MoneyHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equals("1")){
                PayingScreen += "1";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("2")){
                PayingScreen += "2";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("3")){
                PayingScreen += "3";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("4")){
                PayingScreen += "4";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("5")){
                PayingScreen += "5";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("6")){
                PayingScreen += "6";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("7")){
                PayingScreen += "7";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("8")){
                PayingScreen += "8";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("9")){
                PayingScreen += "9";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("0")){
                PayingScreen += "0";
                screen2.setText(PayingScreen);
            }else if(e.getActionCommand().equals("CLEAR")){
                PayingScreen = "";
                screen2.setText(PayingScreen);
            }  
        }
    }
    public class TransactionHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int balance, cashinput, IntAmtPaid, change;  
            String Strcashinput;
    
            IntAmtPaid = Integer.parseInt(PayingScreen);
            if(IntAmtPaid < OrderPrice){
                balance = OrderPrice - IntAmtPaid;
                do{
                    Strcashinput = JOptionPane.showInputDialog(null, "Balance: " + balance +"", "Balance", JOptionPane.INFORMATION_MESSAGE);
                    cashinput = Integer.parseInt(Strcashinput);
                    balance = balance - cashinput;
                }while(balance != 0);
                JOptionPane.showMessageDialog(null, "Balance: 0. Total balance paid. New transaction.", "Total Balance Paid", JOptionPane.INFORMATION_MESSAGE);
                PayingScreen = "";
                OrderPrice = 0;
                screen1.setText("0");
                screen2.setText("0");
            }else if(IntAmtPaid == OrderPrice){
                JOptionPane.showMessageDialog(null, "Balance: 0. Exact amount paid. New transaction.", "Next transaction", JOptionPane.INFORMATION_MESSAGE);
                PayingScreen = "";
                OrderPrice = 0;
                screen1.setText("0");
                screen2.setText("0");
            }else{
                change = IntAmtPaid - OrderPrice;
                JOptionPane.showMessageDialog(null, "Change: " + change + ". New transaction.", "Change", JOptionPane.INFORMATION_MESSAGE);
                PayingScreen = "";
                OrderPrice = 0;
                screen1.setText("0");
                screen2.setText("0");
            }
        }
    }
    public class TotalOrdersHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null, "TOTAL ORDERS TODAY:\nClassic Caesars Salad: " + caesars + " order/s\nFarm Salad: " + farmsalds + " order/s\nKare-kare: " + kare + " order/s\nBeef Ta Pao: " + tapao + " order/s\nAdobo: " + adobs + " order/s\nMushroom Chicken: " + mushroom + " order/s\nKaning Asul: " + asul + " order/s\nKaning Dilaw: " + dilaw + " order/s\nIce Cream Basket: " + icecream + " order/s\nTsokolate Islands: " + islands + "orders.\nHave a good day!", "Total Orders", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
    }
    public static void main (String[]args){
        BalikBukid body = new BalikBukid();
    }
    }
    
    import javax.swing.*;
    导入java.awt.*;
    导入java.awt.event.*;
    公共类BalikBukid扩展JFrame{
    公共静态最终整数宽度=1500;
    公共静态最终内部高度=750;
    私人JLabel Balikbukid农场厨房,价格支付,已支付金额;
    私人吉布顿凯撒色拉、农场色拉、卡雷卡、比夫塔帕、阿多波、蘑菇鸡、卡宁加苏尔、卡宁迪劳、冰淇淋篮、索科拉岛;
    私人吉布顿开胃菜、牛肉、鸡肉、米饭、甜点;
    一号,二号,三号,四号,五号,六号,七号,八号,九号,零号,清除;
    私人JButton订单,背书人;
    私有JTextField屏幕1、屏幕2;
    私有静态int OrderPrice=0,现金,caesars=0,farmsalds=0,kare=0,tapao=0,adobs=0,Muslium=0,asul=0,dilaw=0,icecream=0,islands=0;;
    私有静态字符串支付屏幕;
    私有OrderHandler ordhandler=新OrderHandler();
    private MoneyHandler=new MoneyHandler();
    私有TransactionHandler transhandler=新TransactionHandler();
    私有TotalOrdersHandler tordhandler=新的TotalOrdersHandler();
    公共巴利布基德(){
    容器窗格=getContentPane();
    pane.setLayout(空);
    setTitle(“Balik Bukid POS系统”);
    设置尺寸(宽度、高度);
    setVisible(真);
    可设置大小(假);
    setDefaultCloseOperation(关闭时退出);
    窗格.背景(颜色.绿色);
    //实例化标签和按钮
    Balik Bukid农场厨房=新JLabel(“Balik Bukid农场和厨房”);
    启动器=新的JButton(“启动器”);
    牛肉=新纽扣(“牛肉”);
    鸡肉=新纽扣(“鸡肉”);
    赖斯=新纽扣(“赖斯”);
    甜点=新纽扣(“甜点”);
    PriceToPay=新的JLabel(“支付价格:”);
    已付金额=新JLabel(“已付价格:”);
    凯撒沙拉=新的JButton(“经典凯撒沙拉
    PHP160”); 农场沙拉=新的JButton(“Balik Bukid
    农场沙拉
    PHP160”); Karekare=新JButton(“Ang-Kare-Kare
    Php 210”); BeefTaPao=新的JButton(“蒜味牛肉
    Tapao
    PHP180”); Adobo=新JButton(“Binalot na
    Adobong Dilaw
    Php 145”); 蘑菇鸡=新的JButton(“蘑菇鸡
    PHP160”); KaningAsul=新JButton(“KaningAsul
    Php 35”); KaningDilaw=新JButton(“KaningDilaw
    Php 35”); 冰淇淋篮=新的JButton(“冰淇淋

    PHP110”);
    TsokolateIsland=new JButton(“tsokolateislands
    php180组件应在框架可见之前添加到框架中

    因此,将以下语句移到构造函数的末尾:

    setVisible(true);
    
    实际上,为什么不将所有语句移动到构造函数的末尾来更改框架的属性,以便语句位于一个位置:

    此外:

  • 变量名不应该以大写字符开头。一些变量名是正确的,另一些是不正确的。保持一致并遵循Java约定

  • 不要使用空布局。Swing设计用于布局管理器。因此,请去掉所有setBounds(…)语句,并使用适当的布局管理器


  • Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。相反,使用布局管理器,或者与布局填充和边框一起使用

    现在,该建议是解决问题的核心。实施将遵循以下原则:

  • 使用布局(边框和布局填充)来相对地排列组件,并使用布局约束以逻辑方式调整它们的大小
  • (如果在面板中布置组件)将面板添加到顶层容器中
  • pack()
    顶级容器,以触发GUI的实际验证,并使TLC成为显示内容所需的最小大小
  • (此时调用
    frame.setMinimumSize(frame.getSize());
    是一种不错的但可选的触摸方式。)
  • 调用
    frame.setVisible(true);//显示有组织、大小合适的GUI!

  • 非常感谢!我不知道如何去做这些…很抱歉,这些没有在课堂上讨论过,很难理解。谢谢!现在它确实显示了。很抱歉,我也不太擅长编程,但我会尝试修复变量。关于空值问题,我应该如何修复?我应该简单地使用setSize和setLocation吗?