Atribuir uma variavel em um JLabel

4 respostas
A

Gente eu nao to conseguindo colocar uma variavel num JLabel.
Eu estou usando NetBeans…

public class TelaPrincipal extends javax.swing.JFrame {

    public void Mostra() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat horaFormat = new SimpleDateFormat("H:mm:ss");
        Date date = new Date();

        String dat = dateFormat.format(date);
        String hor = horaFormat.format(date);
        System.out.println(dat);
        System.out.println(hor);

        txtData.setText(dat);
    }
    /** Creates new form TelaPrincipal */
    public TelaPrincipal() {
        initComponents();
    }

No metodo main eu coloquei assim

Mostra();

Da erro fazer isso.

Eu tenho um JLabel que se chama txtData.
E quando executo nao vai.
Tentei tirar do metodo void mais da erro tambem, e o erro eh sempre o mesmo

4 Respostas

Adelar

Olá,
você pode colocar no main:

new TelaPrincipal();

e no construtor TelaPrincipal você chama o método Mostra.

public TelaPrincipal(){ Mostra(); }

o erro acontece porque o método main é estático e métodos estáticos não podem chamar diretamente métodos que não são.

Att.

A

continua dando erro
veja meu codigo

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPrincipal.java
 *
 * Created on 01/07/2010, 22:17:10
 */

package br.com.sistema;

import javax.swing.UIManager;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author Anderson Luiz
 */
public class TelaPrincipal extends javax.swing.JFrame {

    public void Mostra() {
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat horaFormat = new SimpleDateFormat("H:mm:ss");
        Date date = new Date();

        String dat = dateFormat.format(date);
        String hor = horaFormat.format(date);
        System.out.println(dat);
        System.out.println(hor);

        txtData.setText(dat);
    }
    /** Creates new form TelaPrincipal */
    public TelaPrincipal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jToolBar2 = new javax.swing.JToolBar();
        txtData = new javax.swing.JLabel();
        jToolBar3 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jSeparator7 = new javax.swing.JPopupMenu.Separator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JPopupMenu.Separator();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator9 = new javax.swing.JPopupMenu.Separator();
        jMenuItem8 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator10 = new javax.swing.JPopupMenu.Separator();
        jMenuItem10 = new javax.swing.JMenuItem();
        jSeparator11 = new javax.swing.JPopupMenu.Separator();
        jMenuItem11 = new javax.swing.JMenuItem();
        jSeparator12 = new javax.swing.JPopupMenu.Separator();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu8 = new javax.swing.JMenu();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        jMenu9 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SisCom - Sistema Comercial");

        jToolBar1.setRollover(true);

        jToolBar2.setRollover(true);

        txtData.setText("Data:");
        jToolBar2.add(txtData);

        jToolBar3.setRollover(true);

        jLabel1.setText("Hora:");
        jToolBar3.add(jLabel1);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Desenvolvido por:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(153, 153, 153))); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logotipo_anderson.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("Cadastro");

        jMenu3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_cliente.png"))); // NOI18N
        jMenu3.setText("Clientes");

        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_apagar.png"))); // NOI18N
        jMenuItem1.setText("Apagar Cliente");
        jMenu3.add(jMenuItem1);
        jMenu3.add(jSeparator4);

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_cadastrar.png"))); // NOI18N
        jMenuItem2.setText("Cadastrar Cliente");
        jMenu3.add(jMenuItem2);
        jMenu3.add(jSeparator5);

        jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_consultar.png"))); // NOI18N
        jMenuItem3.setText("Consultar Cliente");
        jMenu3.add(jMenuItem3);
        jMenu3.add(jSeparator6);

        jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_editar.png"))); // NOI18N
        jMenuItem4.setText("Editar Cliente");
        jMenu3.add(jMenuItem4);

        jMenu1.add(jMenu3);
        jMenu1.add(jSeparator1);

        jMenu4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_fornecedor.png"))); // NOI18N
        jMenu4.setText("Fornecedores");

        jMenuItem5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_apagar.png"))); // NOI18N
        jMenuItem5.setText("Apagar Fornecedor");
        jMenu4.add(jMenuItem5);
        jMenu4.add(jSeparator7);

        jMenuItem6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_cadastrar.png"))); // NOI18N
        jMenuItem6.setText("Cadastrar Fornecedor");
        jMenu4.add(jMenuItem6);
        jMenu4.add(jSeparator8);

        jMenuItem7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_consultar.png"))); // NOI18N
        jMenuItem7.setText("Consultar Fornecedor");
        jMenu4.add(jMenuItem7);
        jMenu4.add(jSeparator9);

        jMenuItem8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/menu_editar.png"))); // NOI18N
        jMenuItem8.setText("Editar Fornecedor");
        jMenu4.add(jMenuItem8);

        jMenu1.add(jMenu4);
        jMenu1.add(jSeparator2);

        jMenu5.setText("Funcionarios");

        jMenuItem9.setText("Apagar Funcionario");
        jMenu5.add(jMenuItem9);
        jMenu5.add(jSeparator10);

        jMenuItem10.setText("Cadastrar Funcionario");
        jMenu5.add(jMenuItem10);
        jMenu5.add(jSeparator11);

        jMenuItem11.setText("Consultar Funcionario");
        jMenu5.add(jMenuItem11);
        jMenu5.add(jSeparator12);

        jMenuItem12.setText("EditarFuncionario");
        jMenu5.add(jMenuItem12);

        jMenu1.add(jMenu5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Pedidos");

        jMenu8.setText("Orcamentos");
        jMenu2.add(jMenu8);
        jMenu2.add(jSeparator3);

        jMenu9.setText("Pedido de Vendas");
        jMenu2.add(jMenu9);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar3, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(378, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new TelaPrincipal();
        Mostra();
        
        skin sk = new skin();
        sk.ExibeSkin();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenu jMenu9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator10;
    private javax.swing.JPopupMenu.Separator jSeparator11;
    private javax.swing.JPopupMenu.Separator jSeparator12;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JPopupMenu.Separator jSeparator7;
    private javax.swing.JPopupMenu.Separator jSeparator8;
    private javax.swing.JPopupMenu.Separator jSeparator9;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JLabel txtData;
    // End of variables declaration                   

}
D

n tenho certeza, + axo que o JLabel só aceita valores do tipo String…
intao transforma a data para string e exibe…
vlw

paulomn

Damiao Cunha:
n tenho certeza, + axo que o JLabel só aceita valores do tipo String…
intao transforma a data para string e exibe…
vlw

so reforcando o que o colega falou, é so voce utilizar o .toString() para conversao

Criado 3 de julho de 2010
Ultima resposta 4 de jul. de 2010
Respostas 4
Participantes 4