将外部类(JTable)插入Jpanel

时间:2013-12-12 01:19:30

标签: java swing object calendar jtable

我目前正在开展一个项目,我必须将Jtable(这是一个日历)放入Jpanel。 问题是,我无法找到一种方法使其在面板中工作(它甚至没有出现)。 部分是因为我没有填充表格的对象,这就是我需要帮助的地方,因为我无法找到一种方法使其有效。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class Calendar{
int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
JLabel l = new JLabel("", JLabel.CENTER);
String day = "";
JTable d;
JButton[] button = new JButton[49];

public Calendar() {
        String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };

        d = new JTable(header, 0);
        DefaultTableModel calModel = new DefaultTableModel();    

        d.setBounds(20, 200, 500, 350);


        for (int x = 0; x < button.length; x++) {
                final int selection = x;
                button[x] = new JButton();
                button[x].setFocusPainted(false);
                button[x].setBackground(Color.white);
                if (x > 6)
                        button[x].addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent ae) {
                                        day = button[selection].getActionCommand();

                                }
                        });
                if (x < 7) {
                        button[x].setText(header[x]);
                        button[x].setForeground(Color.red);
                }
                d.add(button[x]);
        }

        JButton previous = new JButton("<< Previous");
        previous.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                        month--;
                        displayDate();
                }
        });

        JButton next = new JButton("Next >>");
        next.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                        month++;
                        displayDate();
                }
        });



        displayDate();

}
public void displayDate() {
        for (int x = 7; x < button.length; x++)
                button[x].setText("");
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "MMMM yyyy");
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(year, month, 1);
        int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
        int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
        for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
                button[x].setText("" + day);
        l.setText(sdf.format(cal.getTime()));

}

}

有人可以帮帮我吗?

0 个答案:

没有答案