简体   繁体   English

在我的日历中按下按钮后,不会重绘其他月份

[英]won't repaint a different Month after pressing button in my calendar

I'm trying to build a Calendar in Java as a little project I thought of, But I can't seem to change the name of the Month every time I click the Next button. 我试图用一个小项目在Java中构建一个Calendar,但是似乎每次单击Next按钮时都不能更改Month的名称。 here's my code! 这是我的代码!

package drawing;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Drawing_something extends JPanel{
    int[] calender_squares = {1, 2, 3, 4, 5, 6, 7};
    String[] Month = {"January", "February", "March", "April","May","June","July",
            "August","September","October","November","December"};
    int i = 0;
    Graphics c;
    @Override
    public void paintComponent(Graphics c){
        super.paintComponent(c);
        this.setBackground(Color.WHITE);
        int WIDTH = 55, HEIGHT = 65;

        for (int in: calender_squares) {
            for (int counter = 0; counter < 7; counter++){
                c.drawRect(50, 50, 100, 100);
                c.drawRect(50, 50, 700, 500);
                c.copyArea(50, 50, 600, 500, 100, 0);
                c.copyArea(50, 50, 600, 400, 0, 100);
            }
        }

        for (int date = 1; date <= 30; date++) {
            String s = String.valueOf(date);
            c.drawString(s, WIDTH, HEIGHT);
            if (date <= 6){
                WIDTH += 100;
            } else if (date == 7){
                WIDTH = 55;
                HEIGHT = 165;
            }else if (date <= 13){
                WIDTH += 100;
            }else if (date == 14){
                WIDTH = 55;
                HEIGHT = 265;
            }else if (date <= 20){
                WIDTH += 100;
            }else if (date == 21){
                WIDTH = 55;
                HEIGHT = 365;
            }else if (date <= 27){
                WIDTH += 100;
            }else if (date == 28){
                WIDTH = 55;
                HEIGHT = 465;
            }else if (date <= 30){
                WIDTH += 100;
            }
        }
        c.setFont(new Font("default", Font.BOLD, 40));
        c.drawString(Month[i], 320, 45);
    }

    public Drawing_something(){
        setLayout(new BorderLayout());
        JButton N = new JButton("NEXT");
        JButton B = new JButton("BACK");
        JPanel P = new JPanel();

        P.add(B);
        P.add(N);

        add(P, BorderLayout.SOUTH);

        B.addActionListener(new HandlerClass());
        N.addActionListener(new NextClass());
    }
    public class HandlerClass implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }
    }
    public class NextClass implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (i == 11){
                i = 0;
            }
            i = i + 1;
            c.drawString(Month[i], 320, 45);
        }
    }

    public static void main(String[] args){
        JFrame mainFrame = new JFrame("Calender");
        mainFrame.add(new Drawing_something());
        mainFrame.setSize(850, 650);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
}

if anyone could help that would be much appreciated!! 如果有人可以帮助,将不胜感激!

Thanks in advance!! 提前致谢!!

Don't maintain a reference to the Graphics context used to paint the component, this is not how custom painting is done. 不要维护对用于绘制组件的Graphics上下文的引用,这不是完成自定义绘制的方式。

Instead, once you update the i value, call repaint 相反,更新i值后,请调用repaint

You shouldn't really be adding components to the Draw_Something component, as these will cover what is been painted. 您实际上不应该在Draw_Something组件中添加组件,因为这些组件将覆盖已绘制的内容。

Instead, add all the components to a separate container and use setters and getters to change the state of the calendar pane 而是将所有组件添加到单独的容器中,并使用setter和getter更改日历窗格的状态。

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

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