简体   繁体   English

这不是将JLabel附加到JPanel

[英]This isn't appending the JLabel to JPanel

Why isn't the JLabel timeStamp being added too the JPanel rs_pnl? 为什么也没有在JPanel rs_pnl中添加JLabel timeStamp? I want the user to make a search, which is sent to this function. 我希望用户进行搜索,然后发送给该功能。 If the user types what time is it, then the function should create a JLabel with the time and add it to rs_pnl. 如果用户键入现在的时间,则该函数应使用该时间创建一个JLabel,并将其添加到rs_pnl。 It then returns the panel so it can be used by other classes. 然后,它返回面板,以便其他类可以使用它。

    public class Engine {
        public static JPanel getResults(String input){
            JPanel rs_pnl = new JPanel();
            rs_pnl.setLayout(new BorderLayout());
            rs_pnl.setSize(500, 600);
            String feed = input.toLowerCase();
            String timeLabel = new SimpleDateFormat("hh:mm MM/dd/yy").format(Calendar.getInstance().getTime());
            if(feed.contains("define")){

            }else if(feed.contains("start")){
                //START INDICATED PROGRAM
            }else if(feed.equals("what are the options") | feed.equals("show the menu") | feed.equals("show menu") | feed.equals("options") | feed.equals("menu")){
                JPanel menu = new Menu().show_menu();
                rs_pnl.add(menu, BorderLayout.CENTER);
                System.out.println("Menu Launched");
            }else if(feed.equals("shutdown") | feed.equals("exit")){
                System.exit(0);
            }else if(feed.equals("what time is it") | feed.equals("whats todays date") | feed.equals("what's todays date") | feed.equals("whats the time") | feed.equals("time")){
                JLabel timeStamp  = new JLabel(timeLabel);
                timeStamp.setFont(new Font("Arial", Font.PLAIN, 5));
                timeStamp.setForeground(new Color(69,69,69));
                rs_pnl.add(timeStamp, BorderLayout.CENTER);
            }else{
                //INCLUDE CLASS TO DO ONLINE SEARCH
            }
            return rs_pnl;
        }
    }

By default Swing component have a default size of (0, 0) so there is nothing to paint. 默认情况下,Swing组件的默认大小为(0,0),因此无需绘制任何内容。

The basic logic for dynamically add components to a visible GUI is: 将组件动态添加到可见GUI的基本逻辑是:

panel.add(..._);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to paint all the components

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

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