Aqua Costa Posted April 19, 2009 at 01:11 PM Report #257609 Posted April 19, 2009 at 01:11 PM package jcalculator; import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author Tiago José Vieira Pires Costa */ public class Calculator{ public Calculator() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("JCalculator"); frame.setVisible(true); frame.setBounds(0, 0, 400, 400); frame.setResizable(false); calc = frame.getContentPane(); calc.setLayout(new BorderLayout()); teclado = new JPanel(); teclado.setLayout(new GridLayout(4,4)); calc.add(teclado, BorderLayout.CENTER); mostrador = new JTextField(); mostrador.setEditable(false); mostrador.setText("Calculadora"); calc.add(mostrador, BorderLayout.NORTH); criaButtons(); addButtons(); frame.repaint(); calc.repaint(); teclado.repaint(); } public void criaButtons() { um = new JButton(); dois = new JButton(); tres = new JButton(); quatro = new JButton(); cinco = new JButton(); seis = new JButton(); sete = new JButton(); oito = new JButton(); nove = new JButton(); zero = new JButton(); mais = new JButton(); menos = new JButton(); igual = new JButton(); multiplicar = new JButton(); dividir = new JButton(); apagar = new JButton(); um.setName("1"); } public void addButtons() { teclado.add(sete); teclado.add(oito); teclado.add(nove); teclado.add(multiplicar); teclado.add(quatro); teclado.add(cinco); teclado.add(seis); teclado.add(dividir); teclado.add(um); teclado.add(dois); teclado.add(tres); teclado.add(mais); teclado.add(zero); teclado.add(igual); teclado.add(apagar); teclado.add(menos); } private JFrame frame; private Container calc; private JPanel teclado; private JTextField mostrador; private JButton um; private JButton dois; private JButton tres; private JButton quatro; private JButton cinco; private JButton seis; private JButton sete; private JButton oito; private JButton nove; private JButton zero; private JButton mais; private JButton menos; private JButton igual; private JButton multiplicar; private JButton dividir; private JButton apagar; } quando executo este programa a JFrame aparece em branco, e os components só aparecem se minimizar e maximizar a JFrame... acho que preciso de fazer repaint()... alguém sabe qual é o problema?
KiNgPiTo Posted April 19, 2009 at 01:31 PM Report #257612 Posted April 19, 2009 at 01:31 PM A linha de código frame.setVisible(true); tira-a de onde está e põe a seguir á linha em que chamas o método addbuttons();
Aqua Costa Posted April 19, 2009 at 01:34 PM Author Report #257613 Posted April 19, 2009 at 01:34 PM tens razão que erro "estupido" lol já agora como ponho a JTextField maior????
KiNgPiTo Posted April 19, 2009 at 01:47 PM Report #257614 Posted April 19, 2009 at 01:47 PM Tens o setSize(widht, height) para controlares o tamanho da textfield
Knitter Posted April 19, 2009 at 04:27 PM Report #257632 Posted April 19, 2009 at 04:27 PM Nunca faças um repaint a não ser que tenhas re-implementado o método, repaint não é método para ser chamado directamente e pode ter efeitos negativos na tua aplicação. Tens métodos para forçar os componentes a redesenharem, tal com o validate ou invalidate, ou o pack da frame.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now