Swing问题 点击按钮后不出现内容 20C

设置日历时,点击上一月下一月没反应,需要用鼠标改变大小才能显示。请问各位大神为什么呢?? 急急急
下面这是代码 求大神帮解决
package com.edgedo.txt;
import java.util.Calendar;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.SwingConstants;
import java.awt.Color;

public class RiLi extends JFrame {

private JPanel contentPane;
Calendar cal =Calendar.getInstance();
JPanel panel_3 = new JPanel();


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RiLi frame = new RiLi();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public RiLi() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.NORTH);

    //JButton btnNewButton_1 = new JButton("\u4E0A\u4E00\u6708");


     JButton button = new JButton("\u4E0A\u4E00\u6708");

    panel.add(button);

    JButton btnNewButton = new JButton("\u4E0B\u4E00\u6708");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            cal.add(Calendar.MONTH,1);
            DayX();
        }
    });

    JLabel label = new JLabel("");
    panel.add(label);
    panel.add(btnNewButton);

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1, BorderLayout.CENTER);
    panel_1.setLayout(new BorderLayout(0, 0));

    JPanel panel_2 = new JPanel();
    panel_1.add(panel_2, BorderLayout.NORTH);
    panel_2.setLayout(new GridLayout(0, 7, 0, 0));

    JLabel lblNewLabel_7 = new JLabel("\u65E5");
    lblNewLabel_7.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_7);

    JLabel lblNewLabel_6 = new JLabel("\u4E00");
    lblNewLabel_6.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_6);

    JLabel lblNewLabel_5 = new JLabel("\u4E8C");
    lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_5);

    JLabel lblNewLabel_4 = new JLabel("\u4E09");
    lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_4);

    JLabel lblNewLabel_3 = new JLabel("\u56DB");
    lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_3);

    JLabel lblNewLabel_2 = new JLabel("\u4E94");
    lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_2);

    JLabel lblNewLabel_1 = new JLabel("\u516D");
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
    panel_2.add(lblNewLabel_1);


    panel_1.add(panel_3, BorderLayout.CENTER);
    panel_3.setLayout(new GridLayout(0, 7, 0, 0));
    DayX();

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cal.add(Calendar.MONTH, -1);

            DayX();
        }
    });}
    public void DayX(){
        panel_3.removeAll();
        //找到本月一共有几天

        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        int day1=cal.get(Calendar.DAY_OF_MONTH);
        //找到这个月的一号是周几
        cal.set(Calendar.DAY_OF_MONTH,1);
        int weekNum=cal.get(Calendar.DAY_OF_WEEK);
        int j=1;
        while(j<weekNum){
            JPanel panel_4 = new JPanel();
            panel_3.add(panel_4);

            JLabel lblNewLabel_8 = new JLabel("");
            lblNewLabel_8.setBackground(Color.WHITE);
            panel_4.add(lblNewLabel_8);
            j++;
        }
        int i=1;
        while(i<=day1){

        JPanel panel_4 = new JPanel();
        panel_3.add(panel_4);

        JLabel lblNewLabel_8 = new JLabel(""+i);
        lblNewLabel_8.setBackground(Color.WHITE);
        panel_4.add(lblNewLabel_8);
        i++;
    }
        //doLayout();
        //repaint();

        panel_3.doLayout();
        panel_3.repaint();
    }

}

3个回答

看了你的代码,,告诉你个 好方法,,万能刷新。

SwingUtilities.updateComponentTreeUI(panel_3);
把这一句 加在 DayX ( )最后。
//doLayout();
//repaint();

// panel_3.doLayout();
// panel_3.repaint();

    SwingUtilities.updateComponentTreeUI(panel_3);

            这些玩意都可以注释掉了。。。

SwingUtilities.updateComponentTreeUI(panel_3);
把这一句 加在 DayX ( )最后。
//doLayout();
//repaint();
// panel_3.doLayout();
// panel_3.repaint();

将panel_3.repaint()改为 panel_3.revalidate()或者 panel_3.updateUI(); 或者 panel_3.validate();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!