问题似乎不适用于JAVA

问题描述 投票:0回答:1

我有一个问题,我无法克服...我正在使用Swing用J​​ava编写程序。该程序将用于从显示的日历中选择一天,并放置一个小时的工作时间(例如8:00-16:00),然后该程序将计算您每月工作的小时数并计算您的薪水。

我已经编写了一些代码,因此在启动程序时,您会看到当月的表示形式。我想在按钮上添加ActionListener,以重新排列上个月的日历外观。我想使用生成当前月份的相同方法,但是发送不同的参数(上个月的日期)。

为了对其进行测试,我在ActionListener上使用了该方法(因此,当我启动它时,我看到空白表格,按下该按钮后,它将向我显示当前方法),问题是什么都没有发生...当我将其放入类的构造函数中时,该方法可以正常工作,但是当将其用作执行的操作且不知道为什么时,该方法将不起作用。

我希望您能帮助我弄清楚它,也许告诉我我在哪里犯了错误,以及我可以采取什么措施。这对我来说是一种业余爱好,我在编程方面没有任何专业经验。

我的代码:

package zadanie;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;


class Panel extends JPanel {

    private JButton[] buttonArray = new JButton[42];
    private JButton nextButton, previousButton;
    private JLabel monthYear;
    private Color buttonColor = new Color(116, 185, 255);
    private Color buttonColorInactive = new Color(255,255,255);
    private Color sundey = new Color(0, 184, 148);
    private Color saturday = new Color(85, 239, 196);
    private Color labelColor = new Color(255, 211, 42);
    private LocalDate dateNow = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());


    Panel(){
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(getMonthLabel());
        add(getWeekDaysPanel());
        add(Box.createRigidArea(new Dimension(0,5)));
        add(getMonthPanel());
        calendarGenerator();
        getWeekDaysPanel().setAlignmentX(Component.CENTER_ALIGNMENT);
        getMonthPanel().setAlignmentX(Component.CENTER_ALIGNMENT);
    }

    private JComponent getMonthPanel(){
        JPanel monthPanel = new JPanel();
        monthPanel.setLayout(new GridLayout(6,7));
        monthPanel.setMaximumSize(new Dimension(710,460));
        monthPanel.setPreferredSize(new Dimension(710,460));
        monthPanel.setMaximumSize(new Dimension(710,460));

        //Loop that in every iteration creates a "b" button set it properties and to a "p" panel and a buttonArray.
        for (int i=0; i<42; i++){
            JButton b = new JButton();
                b.setMaximumSize(new Dimension(95,70));
                b.setPreferredSize(new Dimension(95,70));
                b.setMaximumSize(new Dimension(95,70));
                b.setBorderPainted(false);
                b.setRolloverEnabled(false);
                b.setVisible(true);
            JPanel p = new JPanel();
            p.add(b);
            buttonArray[i] = b;
            monthPanel.add(p);
        }
        return monthPanel;
    }
    // Similar to getMonthPanel method - it adds a 7 labels with the names of the days
    private JComponent getWeekDaysPanel(){
        JPanel daysPanel = new JPanel();
            daysPanel.setBackground(labelColor);
            daysPanel.setMinimumSize(new Dimension(700,35));
            daysPanel.setPreferredSize(new Dimension(700,35));
            daysPanel.setMaximumSize(new Dimension(700,35));
        String[] daysList = {"pn.", "wt.", "śr.", "czw.", "pt.", "sob.", "niedz."};

        for (int i = 0; i < 7; i++){
            JLabel e = new JLabel("", JLabel.CENTER);
                e.setMinimumSize(new Dimension(95,25));
                e.setPreferredSize(new Dimension(95,25));
                e.setMaximumSize(new Dimension(95,25));
                e.setLayout(new GridLayout(1,7));
                e.setText(daysList[i]);
                daysPanel.add(e);
        }
        return daysPanel;
    }
    // a method that adds a two buttons (to switch to previous and next month) and a label that displays the displayed month and year
    private JComponent getMonthLabel(){
        JPanel monthLabel = new JPanel();
            monthLabel.setMinimumSize(new Dimension(700,45));
            monthLabel.setPreferredSize(new Dimension(700,45));
            monthLabel.setMaximumSize(new Dimension(700,45));
            monthLabel.setBackground(buttonColorInactive);
            monthLabel.revalidate();
        nextButton = new JButton();
            ImageIcon nIcon = new ImageIcon("n.png");
            nextButton.setMinimumSize(new Dimension(25,25));
            nextButton.setPreferredSize(new Dimension(25,25));
            nextButton.setMaximumSize(new Dimension(25,25));
            nextButton.setIcon(nIcon);
            nextButton.setBorderPainted(false);
            nextButton.setBackground(new Color(255,255,255));
//            nextButton.addActionListener();
        previousButton = new JButton();
            ImageIcon pIcon = new ImageIcon("p.png");
            previousButton.setMinimumSize(new Dimension(25,25));
            previousButton.setPreferredSize(new Dimension(25,25));
            previousButton.setMaximumSize(new Dimension(25,25));
            previousButton.setIcon(pIcon);
            previousButton.setBorderPainted(false);
            previousButton.setBackground(new Color(255,255,255));

        monthYear = new JLabel("MIESIĄC_ROK", JLabel.CENTER);
            monthYear.setMinimumSize(new Dimension(620,25));
            monthYear.setPreferredSize(new Dimension(620,25));
            monthYear.setMaximumSize(new Dimension(620,25));

        monthLabel.add(previousButton);
        monthLabel.add(monthYear);
        monthLabel.add(nextButton);

        return monthLabel;
    }
    // A method that change the appearance of the buttons in the "buttonArray" so the whole thing looks like calendar of the month
    private void calendarGenerator(){
        int noOfDays = dateNow.lengthOfMonth(); /// getting number of days in a month
        int firstDayIndex = (dateNow.getDayOfWeek().getValue() - 1); // gettin the value (number) of the first day of month (it is decreased because getValue starts with 1 and buttonArray with 0)
        int dayNo = 1; // variable that is used to set number of day in the setText() method of button
        int month = (dateNow.getMonth().getValue() - 1); // variable that has a number of the previous month, that is why I decreased it by 1
        int year = dateNow.getYear(); // getting current year

            if (month == 0){ // safety - when the month variable hits 0 it is set for December (no 12) and year is decreased by 1
                month = 12;
                year --;
            }

        LocalDate previousMonthDate = LocalDate.of(year, month, 1); // a new variable for the previous month
        int dayNo2 = previousMonthDate.lengthOfMonth() - (firstDayIndex - 1);  // getting number of days of the previous mont (similar to dayNo but it responsible for the previous month during displaying


        for (int i = 0; i < firstDayIndex; i++){ // loop that fill days in buttons that represent previous month
            buttonArray[i].setText(""+dayNo2);
            buttonArray[i].setVisible(true);
            buttonArray[i].setEnabled(false);
            buttonArray[i].setBackground(buttonColorInactive);
            dayNo2++;
        }


        for (int i = firstDayIndex; i < noOfDays + firstDayIndex; i++){ // loop that fill days in buttons that represent current month
            buttonArray[i].setText(""+dayNo);
            buttonArray[i].setVisible(true);

            if (i == 6 || i == 13 || i == 20 || i == 27 || i == 34 || i == 41){

                buttonArray[i].setBackground(sundey);
            }
            else if (i == 5 || i == 12 || i == 19 || i == 26 || i == 33 || i == 40){
                buttonArray[i].setBackground(saturday);
            }
            else{
                buttonArray[i].setBackground(buttonColor);
            }
            monthYear.setText(""+translate(dateNow.getMonth().getValue())+" "+year); // "translate()" method is used for translating month names from English to my native language
            dayNo++;
        }

        dayNo = 1; // setting dayNo 1 because next month always starts with 1

        for (int i = (noOfDays + firstDayIndex); i < 42; i++){ // loop that fills the rest, empty buttons that represent next month
            buttonArray[i].setText(""+ dayNo);
            buttonArray[i].setVisible(true);
            buttonArray[i].setEnabled(false);
            buttonArray[i].setBackground(buttonColorInactive);
            dayNo++;
        }
    }

    // Method for translating English names to my native Language
    private String translate(int a){
        String monthInPolish = "";
        switch (dateNow.getMonth()){
            case JANUARY: monthInPolish = "Styczeń"; break;
            case FEBRUARY: monthInPolish = "Luty"; break;
            case MARCH: monthInPolish = "Marzec"; break;
            case APRIL: monthInPolish = "Kwiecień"; break;
            case MAY: monthInPolish = "Maj"; break;
            case JUNE: monthInPolish = "Czerwiec"; break;
            case JULY: monthInPolish = "Lipiec"; break;
            case AUGUST: monthInPolish = "Sierpień"; break;
            case SEPTEMBER: monthInPolish = "Wrzesień"; break;
            case OCTOBER: monthInPolish = "Październik"; break;
            case NOVEMBER: monthInPolish = "Listopad"; break;
            case DECEMBER: monthInPolish = "Grudzień"; break;
        }
        return monthInPolish;
    }
}

我正在谈论的方法称为calendarGenerator()感谢您的努力!

java swing for-loop actionlistener
1个回答
0
投票

更改calendarGenerator,因此它接受一个参数,该参数是要生成的月份中的任意日期:

private void calendarGenerator(LocalDate dateInMonth){
    int noOfDays = dateInMonth.lengthOfMonth(); /// getting number of days in a month
    /* the rest of the method remains the same)
}

要生成当前月份,请按calendarGenerator(dateNow);进行调用要生成下个月:calendarGenerator(dateNow.plusMonths(1));

© www.soinside.com 2019 - 2024. All rights reserved.