使用 JLabel 在 JFrame 中打印的问题

时间:2021-06-07 08:08:48

标签: java swing jframe jlabel

I want to print a String in a JLabel 

我为此使用了此代码,但它没有打印,请问哪里出错了 代码是通过JLabel在JFrame中显示日期 首先我为一天制作了一个字符串然后我得到了日历并用它来获得 DAY_OF_WEEK 并通过 usind 如果我不打印日期但它没有打印为什么我不知道请更正它

package com.lalankumar.widget;

import javax.swing.*;

import java.awt.Color;
import java.io.*;
import java.awt.Font;
import java.util.*;

    public class RWidget extends JFrame{
    
        String wday;
        int d;
        public RWidget() {
            
             Calendar c = Calendar.getInstance();
            d= c.get(Calendar.DAY_OF_WEEK);
          Font dayFont = new Font("Courier", Font.BOLD, 25);        
           Font routine = new Font("Courier", Font.ITALIC, 15);                
            setUndecorated(true);
            setLocation(1168,0);
            setSize(200,200);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
             setVisible(true);
            setBackground(new Color(1.0f,1.0f,1.0f,0.1f));
                
                if(d==2) {
                    wday="Monday";                       
                } else if(d==3) {
                     wday="Tuesday";                         
                } else if(d==4) {
                    wday="Wednesday";                       
                } else if(d==5) {
                    wday="Thursday";                        
                } else if(d==6) {
                    wday="Friday";                      
                } else if(d==7) {
                    wday="Saturday";                        
                } else {
                    wday="Sunday";                      
                }
                JLabel dayl= new JLabel(wday);                  
                dayl.setFont(dayFont);                  
                
        }           
        
        public static void main(String[] args) {
            new RWidget();   
        }
          }
            

请快点帮忙

上面给出了完整的代码 请检查哪里出错或为什么它不工作。

1 个答案:

答案 0 :(得分:1)

您正在创建一个新的 JLabel,您正在为其设置文本,但您没有将其添加到您的 JFrame。

我将逐步简化您的代码,并对更改的作用添加一些注释:

package com.lalankumar.widget;

import javax.swing.*;

import java.awt.Color;
import java.io.*;
import java.awt.Font;
import java.util.*;

    public class RWidget extends JFrame{
    
        // String wday; we don't need this variable
        // int d; this can be a local variable
        public RWidget() {
            
            Calendar c = Calendar.getInstance();
            int d= c.get(Calendar.DAY_OF_WEEK);
            Font dayFont = new Font("Courier", Font.BOLD, 25);        
            Font routine = new Font("Courier", Font.ITALIC, 15);
            JLabel text = new JLabel(""); // create a new JLabel, and set the font
            text.setFont(dayFont);
            this.add(text); // add the new Label to the JFrame
            // if you add a String to the constructor, you'll see that immediately on the JFrame
            
            setUndecorated(true);
            setLocation(1168,0);
            setSize(200,200);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setVisible(true);
            // comment this, to make the jFrame easier to see
            // setBackground(new Color(1.0f,1.0f,1.0f,0.1f));
                // replace the conditions by a switch to improve readability
                switch(d) {
                   case 2: text.setText("Monday"); break;
                   case 3: text.setText("Tuesday"); break;
                   case 4: text.setText("Wednesday"); break;
                   case 5: text.setText("Thursday"); break;
                   case 6: text.setText("Friday"); break;
                   case 7: text.setText("Saturday"); break;
                   default: text.setText("Sunday");
                }
                // don't create a new JLabel
                // JLabel dayl= new JLabel(wday);                  
                // dayl.setFont(dayFont);                  
                
        }           
        
        public static void main(String[] args) {
            new RWidget();   
        }
          }

你还可以做什么,而不是开关:

String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
text.setText(days[d-1]);