Galera estou estudando Swing e o problema é o seguinte:
1- fiz um programa que soma dois números e retorna um Jtextarea com o resultado.
ele está compilando: segue o código:
public class Main {
public static void main(String[] args) {
Janela j = new Janela("Robson");
j.repaint();
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.PlainDocument;
public class Janela extends JFrame{
private JLabel lbTitulo;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JButton btOk;
private JButton btFechar;
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);
}
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");
//mande a soma por parametro do construtor
Jsoma js = new Jsoma("Resultado", (Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText())));
} else if ("FECHAR".equals(jb.getActionCommand())) {
System.exit(1);
}
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.JTextComponent;
@SuppressWarnings("serial")
public class Jsoma extends JWindow {
private JLabel lbTitulo;
private JButton btFechar;
private JTextArea resultTextArea;
private Integer resultado;
public Jsoma(String titulo, Integer soma) {
Container c = this.getContentPane();
Border raisedBorder = BorderFactory.createRaisedBevelBorder();
((JComponent) c).setBorder(raisedBorder);
c.setLayout(null);
setSize(500, 270);
setLocationRelativeTo(null);
this.resultado = soma;
this.criarLabels(c);
this.criarBotoes(c);
this.createTextArea(c);
setVisible(true);
}
private void createTextArea(Container c) {
//aqui foi alterado e agora criado um area de texto e pega o que foi guardado no objeto resultado
JTextArea areaResultado = new JTextArea();
areaResultado.setText("Soma: "+this.resultado);
areaResultado.setEditable(false);
areaResultado.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller = new JScrollPane(areaResultado);
scroller.setBounds(20, 60, 450, 130);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.add(scroller);
}
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("");
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();
}
}
}
}
}
Agora queria saber se alguém pode me mostrar como fazer um prorama assim:
1- No lugar de somar duas textField eu queria, pegar todo o numero do text fild, ordena-lo e assim com o textfield abaixo, sendo que , para cada ação é executada uma
thread diferente. no fim ele ordena os numeros e retorna o numero maior depois de ordenado..