queste sn le mie classi se può essere utile
Classe Calendario
codice:import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.GregorianCalendar; import interfaccia.Cella; import interfaccia.Cella_giorno_settimana; import javax.swing.BorderFactory; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Calendario extends JPanel{ private JComboBox combobox; private Cella[]days; private JSpinner spinner; private Cella_giorno_settimana[] week; public Calendario(){ this.setSize(Toolkit.getDefaultToolkit().getScreenSize()); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createLineBorder(Color.black)); //pannello che contiene la combobox e la spinner JPanel combospinnerpanel = new JPanel(); FlowLayout layout = new FlowLayout(FlowLayout.LEFT,50,5); combospinnerpanel.setLayout(layout); //combobox ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(new String[] { "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre","Ottobre", "Novembre", "Dicembre" }); combobox = new JComboBox(); combobox.setModel(jComboBox1Model); combobox.setBounds(14, 14, 98, 28); combospinnerpanel.add(combobox); combobox.addActionListener(new MioListener()); //spinner Calendar calendar = new GregorianCalendar(); int currentYear = calendar.get(Calendar.YEAR); SpinnerModel jSpinner1Model = new SpinnerNumberModel(currentYear,currentYear - 100,currentYear + 100,1); spinner = new JSpinner(); spinner.setModel(jSpinner1Model); spinner.setEditor(new JSpinner.NumberEditor(spinner, "#")); spinner.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false)); spinner.getEditor().setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false)); spinner.getEditor().setEnabled(false); spinner.setBounds(495, 14, 98, 28); combospinnerpanel.add(spinner); spinner.addChangeListener(listener); this.add(combospinnerpanel,BorderLayout.NORTH); //pannello che contiene i giorni del mese (da 1 a 31) JPanel panelgiorni = new JPanel(); GridLayout panelgiornilayout = new GridLayout(6, 7); panelgiornilayout.setHgap(5); panelgiornilayout.setVgap(5); panelgiornilayout.setColumns(7); panelgiornilayout.setRows(6); panelgiorni.setLayout(panelgiornilayout); this.add(panelgiorni,BorderLayout.SOUTH); //bottoni giorni da 1 a 31 days = new Cella[42]; for (int i=0; i<42;i++){ Cella cella = new Cella(); days[i]= cella; } for (int i=0; i<42; i++){ panelgiorni.add(days[i]); days[i].clear(false); } leggistatocombospinner(); //pannello che contiene il nome dei giorni della settimana JPanel settpanel = new JPanel(); GridLayout settpanellayout = new GridLayout(1,0); settpanellayout.setHgap(5); settpanellayout.setVgap(5); settpanellayout.setColumns(0); settpanellayout.setRows(1); settpanel.setLayout(settpanellayout); this.add(settpanel,BorderLayout.CENTER); //label giorni della settimana lun/mar/mer etc.. week = new Cella_giorno_settimana[7]; for (int i=0; i<7;i++){ Cella_giorno_settimana cella = new Cella_giorno_settimana(); week[i]= cella; } for (int i=0; i<7; i++){ settpanel.add(week[i]); } String lun = new String("lun"); week[0].setGiorno(lun); String mar = new String("mar"); week[1].setGiorno(mar); String mer = new String("mer"); week[2].setGiorno(mer); String gio = new String("gio"); week[3].setGiorno(gio); String ven = new String("ven"); week[4].setGiorno(ven); String sab = new String("sab"); week[5].setGiorno(sab); String dom = new String("dom"); week[6].setGiorno(dom); //this.add(combospinnerpanel,BorderLayout.NORTH); //this.add(panelgiorni,BorderLayout.CENTER); } public class MioListener implements ActionListener { public void actionPerformed(ActionEvent ae) { JComboBox jcb = (JComboBox) ae.getSource(); azzeracelle(); //azzero le celle della precedente visualizzazione per non creare sovrascrizioni leggistatocombospinner(); } } ChangeListener listener = new ChangeListener() { public void stateChanged(ChangeEvent e) { JSpinner js = (JSpinner) e.getSource(); azzeracelle(); leggistatocombospinner(); } }; public void azzeracelle(){ for (int i=0; i<42; i++) days[i].clear(false); } public void leggistatocombospinner (){ int mm= combobox.getSelectedIndex(); int yy=((Integer)spinner.getValue()).intValue(); setCalendario(yy,mm); } public void setCalendario (int anno, int mese){ GregorianCalendar c = new GregorianCalendar (anno,mese,1); int lastday = c.getActualMaximum (Calendar.DAY_OF_MONTH); int weekday = c.get (Calendar.DAY_OF_WEEK); int giorno = (weekday + 5) % 7; int maxday = giorno+(lastday-1); for (int i=giorno, y=1; i<=maxday ;i++, y++){ days[i].clear(true); days[i].setGiorno(y); } } public static void main(String args[]) { JFrame frame = new JFrame("Prova Pannello"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Calendario panel = new Calendario(); frame.add(panel); frame.setSize(616, 427); frame.setVisible(true); } }
Classe Cella
praticamente la classe cella contiene un bottone, io vorrei che nn appena si clicchi sul bottone della cella mi restituisca la data nel formato gg/mm/aaaa, il problema è che il mese e l'anno li devo prendere dalla spinner e combobox della classe Calendario..e quindi nn so come fare.codice:import java.awt.BorderLayout; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.BorderFactory; import javax.swing.JButton; import interfaccia.Calendario; import javax.swing.JPanel; public class Cella extends JPanel { private int giorno; private JButton jButton1; //String days[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", //"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", //"23", "24", "25", "26", "27", "28", "29", "30", "31"}; public Cella(){ //private void initGUI() { try { { this.setPreferredSize(new java.awt.Dimension(78, 49)); BorderLayout thisLayout = new BorderLayout(); this.setLayout(thisLayout); { jButton1 = new JButton(); this.add(jButton1, BorderLayout.CENTER); jButton1.setText(""); jButton1.setBounds(0, 0, 105, 49); jButton1.setBorder(BorderFactory.createRaisedBevelBorder()); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { } }); } } } catch (Exception e) { e.printStackTrace(); } //} } public void setGiorno(int giorno){ this.giorno=giorno; jButton1.setText(Integer.toString(giorno)); } public int getGiorno(){ return giorno; } public void clear(boolean flag){ this.setVisible(flag); } }
![]()

Rispondi quotando