简体   繁体   English

鼠标悬停或我最小化窗口和JTextField问题后,JComponent才会出现

[英]JComponents don't appear until mouseover or I minimize the window, and JTextField problems

I'm a 10th Grader from the Philippines, and IT right now is about Java with GUI. 我是来自菲律宾的10年级学生,现在的IT是关于Java与GUI的。 Only the basic ones. 只有基本的。 Our project is to make a POS System. 我们的项目是制造POS系统。 I already made one, but there are some minor problems: 我已经写了一个,但是有一些小问题:

  1. JComponents don't appear unless I either minimize the window then reopen it (everything then shows up) or I hover over them with my mouse (one by one) 除非我最小化窗口然后重新打开它(一切随后显示),或者用鼠标将鼠标悬停在它们上面,否则JComponents不会出现

  2. For the "Amount Paid" (screen2) text field, where I use a keypad to input String, it firsts shows "null" before the first input, because this text field is set to show "0" at first then accepts input from the keypad. 对于我使用键盘输入String的“ Amount Paid”(屏幕2)文本字段,它在第一个输入之前首先显示“ null”,因为此文本字段首先设置为显示“ 0”,然后接受来自键盘。 But the first time I input something when the programs has just run, it shows "null" first. 但是,当程序刚运行时,我第一次输入内容时,它首先显示“ null”。 It can be cleared, but can be very confusing. 可以清除它,但可能会造成混乱。

Please forgive me for my being an amateur for I am just a students and this is just a project. 请原谅我作为业余爱好者,因为我只是一个学生,这只是一个项目。 Also, this is my first time here, so sorry if I do anything weird! 另外,这是我第一次来这里,对不起,如果我做任何奇怪的事! I use BlueJ BTW. 我使用BlueJ BTW。 Please, I am asking for your help. 拜托,我正在寻求您的帮助。 Thank you! 谢谢!

Here is my source code: 这是我的源代码:

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();
}
}

Also I may or may not be restricted to the packages I already imported in my program, but it would be better if we discussed about them in class so I can understand them more. 同样,我可能会或可能不会仅限于已经在程序中导入的程序包,但是如果我们在课堂上讨论它们会更好,以便我能更好地理解它们。 And sorry I use comments, haha, I use them so I won't get lost. 抱歉,我使用评论,哈哈,我使用它们是为了避免迷路。

Components should be added to the frame BEFORE the frame is made visible. 在使框架可见之前,应将组件添加到框架。

So move the following statement to the end of your constructor: 因此,将以下语句移到构造函数的末尾:

setVisible(true);

Actually, why don't you move all the statements the change the property of the frame to the end of the constructor so the statements are located in once place: 实际上,为什么不移动所有语句以将框架的属性更改为构造函数的末尾,以使这些语句位于一次位置:

Also: 也:

  1. Variable names should NOT start with an upper case character. 变量名称不应以大写字母开头。 Some variable names are correct, other are not. 一些变量名正确,而其他则不正确。 Be consistent and follow Java conventions!!! 保持一致并遵循Java约定!!!

  2. Don't use a null layout. 不要使用空布局。 Swing was designed to be used with a layout manager. Swing旨在与布局管理器一起使用。 So get rid of all the setBounds(...) statements and use approapriate layout managers. 因此,请摆脱所有setBounds(...)语句,并使用适当的布局管理器。

Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. Java GUI必须在不同的语言环境中使用不同的PLAF在不同的OS,屏幕大小,屏幕分辨率等上工作。 As such, they are not conducive to pixel perfect layout. 因此,它们不利于像素的完美布局。 Instead use layout managers, or combinations of them along with layout padding and borders for white space . 而是使用布局管理器,或它们的组合与布局沿填充和边框为白色的空间

Now, that advice is the core of fixing the problem. 现在,该建议是解决问题的核心。 The implementation would be along the lines of: 该实现将遵循以下原则:

  1. Use layouts (borders and layout padding) to arrange the components relative to one another, and use layout constraints to have them resize in a logical way. 使用布局(边界和布局填充)来相对于彼此布置组件,并使用布局约束以逻辑方式调整它们的大小。
  2. (If laying the components out in a panel) Add the panel to the top level container. (如果将组件放置在面板中)将面板添加到顶层容器中。
  3. pack() the top level container in order to trigger the actual validation of the GUI, and cause the TLC to be the minimum size it needs to be in order to display the content. pack()顶层容器,以便触发GUI的实际验证,并使TLC成为显示内容所需的最小大小。
  4. (Calling frame.setMinimumSize(frame.getSize()); at this point is a nice, but optional, touch.) (调用frame.setMinimumSize(frame.getSize());这时很不错,但是是可选的。)
  5. Call frame.setVisible(true); // show the organized, sized GUI! 调用frame.setVisible(true); // show the organized, sized GUI! frame.setVisible(true); // show the organized, sized GUI!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM