Oi gente,
Desculpem aí a pergunta, mas é que sou iniciante e estou tendo dificuldades para colocar um botão para funcionar.
O JApplet se trata de um calendário onde, os botões para adicionar ou diminuir mês e ano ainda não estão funcionando.
Deêm uma olhada no código pra ver. Já pesquisei, mas não sei aonde estou errando.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
public class Agenda extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JPanel jPanel1 = null;
private JTable jTable = null;
private JLabel jLabel3 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private int mes;
private int ano;
private int diaAtual;
private int diaSemana;
private int diaMes;
/**
* This is the xxx default constructor
*/
public Agenda() {
super();
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
Date hoje = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(hoje);
mes = cal.get(GregorianCalendar.MONTH);
ano = cal.get(GregorianCalendar.YEAR);
diaSemana = cal.get( GregorianCalendar.DAY_OF_WEEK );
diaMes = cal.getActualMaximum( GregorianCalendar.DAY_OF_MONTH );
this.setSize(661, 382);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setText("");
jLabel.setIcon(new ImageIcon("C:/Documents and Settings/Handytech/Meus documentos/Minhas imagens/topo_02.JPG"));
jLabel.setFont(new Font("Dialog", Font.BOLD, 12));
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(6);
borderLayout.setVgap(16);
jContentPane = new JPanel();
jContentPane.setLayout(borderLayout);
jContentPane.add(getJPanel(), BorderLayout.WEST);
jContentPane.add(jLabel, BorderLayout.NORTH);
jContentPane.add(getJPanel1(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
gridBagConstraints21.gridy = 1;
gridBagConstraints21.weighty = 40.0;
gridBagConstraints21.anchor = GridBagConstraints.NORTH;
gridBagConstraints21.gridx = 0;
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.gridy = 0;
gridBagConstraints11.weighty = 0.5;
gridBagConstraints11.anchor = GridBagConstraints.NORTH;
gridBagConstraints11.insets = new Insets(5, 0, 0, 0);
gridBagConstraints11.gridx = 0;
jLabel2 = new JLabel();
jLabel2.setText("Compromisso");
jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
jLabel2.setVerticalAlignment(SwingConstants.TOP);
jLabel2.setVerticalTextPosition(SwingConstants.TOP);
jLabel2.setHorizontalTextPosition(SwingConstants.LEFT);
jLabel1 = new JLabel();
jLabel1.setText("Usuario");
jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
jLabel1.setVerticalAlignment(SwingConstants.TOP);
jLabel1.setVerticalTextPosition(SwingConstants.TOP);
jLabel1.setHorizontalTextPosition(SwingConstants.LEFT);
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setPreferredSize(new Dimension(100, 0));
jPanel.setBackground(new Color(137, 166, 241));
jPanel.add(jLabel1, gridBagConstraints11);
jPanel.add(jLabel2, gridBagConstraints21);
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
gridBagConstraints8.gridx = 5;
gridBagConstraints8.gridwidth = 1;
gridBagConstraints8.gridy = 0;
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.gridx = 4;
gridBagConstraints7.gridwidth = 1;
gridBagConstraints7.gridy = 0;
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.gridx = 0;
gridBagConstraints6.gridwidth = 1;
gridBagConstraints6.gridheight = 1;
gridBagConstraints6.gridy = 0;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridx = 1;
gridBagConstraints5.gridheight = 1;
gridBagConstraints5.gridwidth = 1;
gridBagConstraints5.insets = new Insets(0, 0, 0, 0);
gridBagConstraints5.gridy = 0;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.ipadx = 6;
gridBagConstraints.ipady = 4;
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
gridBagConstraints.weightx = 0.0;
gridBagConstraints.weighty = 0.1;
gridBagConstraints.gridy = 0;
jLabel3 = new JLabel();
jLabel3.setText("Calendário");
jLabel3.setFont(new Font("Dialog", Font.BOLD, 14));
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridy = 1;
gridBagConstraints2.ipadx = 262;
gridBagConstraints2.ipady = 0;
gridBagConstraints2.fill = GridBagConstraints.BOTH;
gridBagConstraints2.weighty = 0.0;
gridBagConstraints2.insets = new Insets(0, 0, 10, 0);
gridBagConstraints2.gridheight = 1;
gridBagConstraints2.gridwidth = 1;
gridBagConstraints2.anchor = GridBagConstraints.WEST;
gridBagConstraints2.gridx = 3;
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.add(getJTable(), gridBagConstraints2);
jPanel1.add(jLabel3, gridBagConstraints);
jPanel1.add(getJButton(), gridBagConstraints5);
jPanel1.add(getJButton1(), gridBagConstraints6);
jPanel1.add(getJButton2(), gridBagConstraints7);
jPanel1.add(getJButton3(), gridBagConstraints8);
}
return jPanel1;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable(7, 7);
jTable.setBackground(new Color(238, 238, 238));
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable.setBounds(100, 100, 100, 100); // posicao horizontal , posicao vertical , comprimento, altura
jTable.setRowHeight(30);
jTable.setValueAt("Dom", 0, 0);
jTable.setValueAt("Seg", 0, 1);
jTable.setValueAt("Ter", 0, 2);
jTable.setValueAt("Qua", 0, 3);
jTable.setValueAt("Qui", 0, 4);
jTable.setValueAt("Sex", 0, 5);
jTable.setValueAt("Sab", 0, 6);
jTable.setEnabled(false); // editar dados da tabela
switch(mes + 1){
case 1: jLabel3.setText("Janeiro - " + ano);
break;
case 2: jLabel3.setText("Fevereiro - " + ano);
break;
case 3: jLabel3.setText("Março - " + ano);
break;
case 4: jLabel3.setText("Abril - " + ano);
break;
case 5: jLabel3.setText("Maio - " + ano);
break;
case 6: jLabel3.setText("Junho - " + ano);
break;
case 7: jLabel3.setText("Julho - " + ano);
break;
case 8: jLabel3.setText("Agosto - " + ano);
break;
case 9: jLabel3.setText("Setembro - " + ano);
break;
case 10: jLabel3.setText("Outubro - " + ano);
break;
case 11: jLabel3.setText("Novembro - " + ano);
break;
case 12: jLabel3.setText("Dezembro - " + ano);
break;
}
diaAtual =1;
boolean comecou = false;
for (int i=1; i<7; i++){
for (int j=0; j<7; j++){
if((comecou || j==diaSemana) && (diaAtual <= diaMes)){
comecou = true;
Integer d = new Integer(diaAtual);
jTable.setValueAt(d, i, j);
diaAtual++;
}
}
}
}
return jTable;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("<");
jButton.setActionCommand("<");
jButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("<"))
mes--;
}
} );
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("<<");
jButton1.setActionCommand("<<");
jButton1.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("<<"))
ano--;
}
} );
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText(">");
jButton2.setActionCommand(">");
jButton2.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("<"))
mes++;
}
} );
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setText(">>");
jButton3.setActionCommand(">>");
jButton3.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("<"))
ano++;
}
} );
}
return jButton3;
}
} // @jve:decl-index=0:visual-constraint="10,10"
Obrigada.
Milena.