簡體   English   中英

使用 JLabel 在 JFrame 中打印時出現問題

[英]Problem in Printing in JFrame with JLabel

I want to print a String in a JLabel 

我為此使用了此代碼,但它沒有打印,請問哪里錯了代碼是通過 JLabel 在 JFrame 中顯示日期首先我為一天創建了一個字符串,然后我得到了日歷並使用它來獲取 DAY_OF_WEEK 並使用它來獲取 DAY_OF_WEEK打印日期但它沒有打印為什么我不知道請更正它

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

請快速幫助

上面給出了完整的代碼請檢查哪里出錯或為什么它不工作。

您正在創建一個新的 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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM