如何在另一个类中调用静态类

时间:2014-03-09 14:22:47

标签: java swing class user-interface

你好,我的组合帧到一个类和另一个类之间有一个问题,所以我创建了一个有JComboBox的程序然后如果我点击一次这将出现被调用的类但在我的日历中我不能在这里调出它是我在另一个类中调用我的帧的代码

现在这很好但是在我的日历中我有一个异常错误我因为静态而无法调出它?或者我不知道。这是我在日历中的课程。

package Patola;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Calendar
{

    static JLabel lblMonth, lblYear;
    static JButton btnPrev, btnNext;
    static JTable tblCalendar;
    static JComboBox cmbYear;
    static JFrame frmMain;
    static Container pane;
    static DefaultTableModel mtblCalendar;
    static JScrollPane stblCalendar;
    static JPanel pnlCalendar;
    static int realYear, realMonth, realDay, currentYear, currentMonth;

    public static void main (String args[]){

        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch (ClassNotFoundException e) {}
        catch (InstantiationException e) {}
        catch (IllegalAccessException e) {}
        catch (UnsupportedLookAndFeelException e) {}


        frmMain = new JFrame ("Group Cents");
        frmMain.setSize(330, 375); 
        pane = frmMain.getContentPane();
        pane.setLayout(null);
        frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        lblMonth = new JLabel ("January");
        lblYear = new JLabel ("Jump to year :");
        cmbYear = new JComboBox();
        btnPrev = new JButton ("prev");
        btnNext = new JButton ("next");
        mtblCalendar = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return false;}};
        tblCalendar = new JTable(mtblCalendar);
        stblCalendar = new JScrollPane(tblCalendar);
        pnlCalendar = new JPanel(null);


        pnlCalendar.setBorder(BorderFactory.createTitledBorder("Calendar"));


        btnPrev.addActionListener(new btnPrev_Action());
        btnNext.addActionListener(new btnNext_Action());
        cmbYear.addActionListener(new cmbYear_Action());


        pane.add(pnlCalendar);
        pnlCalendar.add(lblMonth);
        pnlCalendar.add(lblYear);
        pnlCalendar.add(cmbYear);
        pnlCalendar.add(btnPrev);
        pnlCalendar.add(btnNext);
        pnlCalendar.add(stblCalendar);

        Font font = new Font("Algerian", Font.PLAIN, 14);
        lblMonth.setFont(font);
        lblMonth.setForeground(Color.BLUE);
        pnlCalendar.setBounds(0, 0, 320, 335);
        lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 100, 25);
        lblYear.setBounds(10, 305, 150, 20);
        cmbYear.setBounds(230, 305, 80, 20);
        btnPrev.setBounds(10, 25, 60, 25);
        btnNext.setBounds(250, 25, 60, 25);
        stblCalendar.setBounds(10, 50, 300, 250);
        pnlCalendar.setBackground(Color.CYAN);
        btnPrev.setForeground(Color.RED);
        btnNext.setForeground(Color.RED);
        frmMain.setResizable(false);
        frmMain.setVisible(true);
        lblYear.setFont(font);
        lblYear.setForeground(Color.RED);

        GregorianCalendar cal = new GregorianCalendar(); 
        realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); 
        realMonth = cal.get(GregorianCalendar.MONTH); 
        realYear = cal.get(GregorianCalendar.YEAR); 
        currentMonth = realMonth;
        currentYear = realYear;


        String[] headers = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

        for (int i=0; i<7; i++)
        {
            mtblCalendar.addColumn(headers[i]);
        }

        tblCalendar.getParent().setBackground(tblCalendar.getBackground());


        tblCalendar.getTableHeader().setResizingAllowed(false);
        tblCalendar.getTableHeader().setReorderingAllowed(false);


        tblCalendar.setColumnSelectionAllowed(true);
        tblCalendar.setRowSelectionAllowed(true);
        tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        tblCalendar.setRowHeight(38);
        mtblCalendar.setColumnCount(7);
        mtblCalendar.setRowCount(6);


        for (int i=realYear-100; i<=realYear+100; i++)
        {
            cmbYear.addItem(String.valueOf(i));
        }


        refreshCalendar (realMonth, realYear);
    }

    public static void refreshCalendar(int month, int year){

        String[] months =  {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        int nod, som; 


        btnPrev.setEnabled(true);
        btnNext.setEnabled(true);
        if (month == 0 && year <= realYear-10){btnPrev.setEnabled(false);} 
        if (month == 11 && year >= realYear+100){btnNext.setEnabled(false);} 
        lblMonth.setText(months[month]); 
        lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 180, 25); 
        cmbYear.setSelectedItem(String.valueOf(year));


        for (int i=0; i<6; i++)
        {
            for (int j=0; j<7; j++)
            {
                mtblCalendar.setValueAt(null, i, j);
            }
        }


        GregorianCalendar cal = new GregorianCalendar(year, month, 1);
        nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
        som = cal.get(GregorianCalendar.DAY_OF_WEEK);


        for (int i=1; i<=nod; i++)
        {
            int row = new Integer((i+som-2)/7);
            int column  =  (i+som-2)%7;
            mtblCalendar.setValueAt(i, row, column);
        }


        tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer());
    }

    static class tblCalendarRenderer extends DefaultTableCellRenderer
    {
        public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column)
        {
            super.getTableCellRendererComponent(table, value, selected, focused, row, column);
            if (column == 0 || column == 6)
            { 
                setBackground(new Color(255, 220, 220));
            }
            else
            {
                setBackground(new Color(255, 255, 255));
            }
            if (value != null)
            {
                if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && currentYear == realYear)
                { 
                    setBackground(new Color(220, 220, 255));
                }
            }
            setBorder(null);
            setForeground(Color.black);
            return this;
        }
    }

    static class btnPrev_Action implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (currentMonth == 0)
            { 
                currentMonth = 11;
                currentYear -= 1;
            }
            else{ 
                currentMonth -= 1;
            }
            refreshCalendar(currentMonth, currentYear);
        }
    }
    static class btnNext_Action implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if (currentMonth == 11)
            { 
                currentMonth = 0;
                currentYear += 1;
            }
            else
            { 
                currentMonth += 1;
            }
            refreshCalendar(currentMonth, currentYear);
        }
    }

            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您正尝试使用Calendar类

的对象访问静态变量
 new Calendar().frmMain.setVisible(true);

frmMainstatic类中定义为Calendar

如果您确定frmMain已在Calendar课程中实例化,则可以使用

进行访问
Calendar.frmMain.setVisible(true)