[code]public class Janela extends JFrame{
private JLabel lbTitulo;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JButton btOk;
private JButton btFechar;
public JTextArea soma;
public Janela (){
super()
;}
public Janela (String titulo){
super(titulo);
Container c = this.getContentPane();
c.setLayout(null);
setSize(380, 270);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
CriarLabel(c);
CriarTextField(c);
CriarBotao(c);
}
public void CriarLabel(Container c){
this.lbTitulo = new JLabel("Calcular");
this.lbTitulo.setBounds(130, 20, 200, 20);
this.lbTitulo.setFont(new Font("Arial", Font.BOLD, 16));
this.lbTitulo.setForeground(Color.BLUE);
this.lbTitulo.setToolTipText("Calcula Alguma Coisa");
c.add(this.lbTitulo);
}
public void CriarTextField(Container c){
this.tf1 = criaTextFieldNumero(c, "Número 1:", 60);
this.tf2 = criaTextFieldNumero(c, "Número 2:", 85);
}
private JTextField criaTextFieldNumero(Container c, String rotulo, int posY) {
JLabel lb = new JLabel(rotulo);
lb.setBounds(20, posY, 60, 20);
c.add(lb);
JTextField tfNum = new JTextField(20);
tfNum.setToolTipText(rotulo);
tfNum.setBounds(85, posY, 100, 20);
c.add(tfNum);
return tfNum;
}
public void CriarBotao(Container c){
this.btOk = new JButton("Ok");
this.btFechar = new JButton("Fechar");
this.btOk.setBounds(115, 200, 60, 20);
this.btFechar.setBounds(195, 200, 80, 20);
this.btOk.setToolTipText("Clique aqui para confirmar");
this.btFechar.setToolTipText("Clique aqui para fechar o programa");
this.btOk.setActionCommand("OK");
this.btFechar.setActionCommand("FECHAR");
OuvinteBotao ouvinteBotao = new OuvinteBotao();
this.btOk.addActionListener(ouvinteBotao);
this.btFechar.addActionListener(ouvinteBotao);
c.add(this.btOk);
c.add(this.btFechar);
}
public String soma(){
int numero1 = Integer.parseInt(tf1.getText());
int numero2 = Integer.parseInt(tf2.getText());
tf3.setText("resultado: "+ (numero1 + numero2));
return tf3.getText();
}
private class OuvinteBotao implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() instanceof JButton) {
JButton jb = (JButton) ae.getSource();
if ("OK".equals(jb.getActionCommand())) {
System.out.println("Tela de resultado");
Jsoma js = new Jsoma("Resultado");
}
else if ("FECHAR".equals(jb.getActionCommand())) {
System.exit(1);
}
}
} }
}
[/code]
como podem ver estou criando uma tela no Swing onde quero pegar dois números no textfield e soma-los …
para exibir na mesma tela em outro text field da certo, só que quero exibir o resultado da soma em outra janela do tipo JWindow…fiz assim, só que só aparece o titulo da tela e o botão fechar…[code]@SuppressWarnings(“serial”)
public class Jsoma extends JWindow {
private JLabel lbTitulo;
private JButton btFechar;
private JTextArea resultTextArea;
private Object resultado;
public Jsoma(String titulo){
Container c = this.getContentPane();
Border raisedBorder = BorderFactory.createRaisedBevelBorder();
((JComponent) c).setBorder(raisedBorder);
c.setLayout(null);
setSize(500, 270);
setLocationRelativeTo(null);
setVisible(true);
this.criarLabels(c);
this.criarBotoes(c);
this.createTextArea(c);
}
private void createTextArea(Container c) {
Janela ja = new Janela();
JTextArea resultado= ja.soma;
((JTextComponent) this.resultado).setEditable(false);
((JComponent) this.resultado).setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller = new JScrollPane((Component) this.resultado);
scroller.setBounds(20, 60, 450, 130);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.add(resultado);
}
private void criarLabels(Container c) {
this.lbTitulo = new JLabel("Resultado");
this.lbTitulo.setBounds(200, 20, 200, 20);
this.lbTitulo.setFont(new Font("Arial", Font.BOLD, 16));
this.lbTitulo.setForeground(Color.BLUE);
this.lbTitulo.setToolTipText("Calculor de Primos");
c.add(this.lbTitulo);
}
private void criarBotoes(Container c) {
this.btFechar = new JButton("Fechar");
this.btFechar.setBounds(200, 200, 80, 20);
this.btFechar.setToolTipText("Clique aqui para fechar a janela");
this.btFechar.setActionCommand("FECHAR");
OuvinteBotao ouvinteBotao = new OuvinteBotao();
this.btFechar.addActionListener(ouvinteBotao);
c.add(this.btFechar);
}
// classe private para tratar os eventos dos botões
private class OuvinteBotao implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() instanceof JButton) {
JButton jb = (JButton) ae.getSource();
if ("FECHAR".equals(jb.getActionCommand())) {
Jsoma.this.dispose();
}
}
}
}
}[/code] …
Alguém pode me dar uma dica…?