Como faço para colocar ScrollBar no código abaixo, já fiz várias tentativas e nenhuma deu certo.
package br.com.people;
//bibliotecas
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import javax.swing.text.MaskFormatter;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//classe exBlog extendida de JFrame, implementando ActionListener
public class Formulario extends JFrame implements ActionListener{
//Variáveis
private JLabel lblEst,lblNome,lblSex,lblNiv,lblCPF,lblInt;
private JTextField texNome;
private JFormattedTextField texCPF;
private ButtonGroup gruSex;
private JRadioButton radMasc, radFem;
private JComboBox cmbNiv;
private JCheckBox chkIng, chkMat, chkPro, chkLog;
private JButton btnGrav;
private JPanel panel;
private JTextArea txtArea;
//Iniciando o Formulario
public Formulario(){
setLayout(null);
//Label Estudante
lblEst = new JLabel("Estudante: ");
lblEst.setForeground(Color.BLUE);
lblEst.setBounds(120,5,100,20);
add(lblEst);
//Label Nome
lblNome = new JLabel("Nome: ");
lblNome.setBounds(10,35,100,20);
lblNome.setForeground(Color.WHITE);
add(lblNome);
//Text Nome
texNome = new JTextField("");
texNome.setBounds(50,35,250,20);
add(texNome);
//Label Sexo
lblSex = new JLabel("Sexo: ");
lblSex.setBounds(10,65,100,20);
lblSex.setForeground(Color.WHITE);
add(lblSex);
//Radio Masculino
radMasc = new JRadioButton(“Masculino”);
radMasc.setBounds(60,65,120,20);
radMasc.setForeground(Color.BLUE);
radMasc.setBackground(Color.RED);
radMasc.setSelected(true);
//Radio Feminino
radFem = new JRadioButton(“Feminino”);
radFem.setBounds(200,65,120,20);
radFem.setForeground(Color.BLUE);
radFem.setBackground(Color.RED);
//Grupo de Botões
gruSex = new ButtonGroup();
gruSex.add(radMasc);
gruSex.add(radFem);
add(radMasc);
add(radFem);
//Label Nível
lblNiv = new JLabel("Nível Estudantil: ");
lblNiv.setBounds(10,95,200,20);
lblNiv.setForeground(Color.WHITE);
add(lblNiv);
//Combo Nível
cmbNiv = new JComboBox();
cmbNiv.setBackground(Color.WHITE);
cmbNiv.addItem(“Básico Completo”);
cmbNiv.addItem(“Médio Completo”);
cmbNiv.addItem(“Superior Completo”);
cmbNiv.addItem(“Doutorado e/ou Mestrado Completo”);
cmbNiv.setBounds(10,125,250,20);
add(cmbNiv);
//Label CPF
lblCPF = new JLabel("CPF: ");
lblCPF.setBounds(10,155,100,20);
lblCPF.setForeground(Color.WHITE);
add(lblCPF);
//Campo com máscara - CPF
try {
MaskFormatter mk= new MaskFormatter("###.###.###-##");
mk.setPlaceholderCharacter(’’);
/* ou, pode setar os caracteres que irá receber
MaskFormatter mk= new MaskFormatter("***.***.*-");
mk.setPlaceholderCharacter(’’);
mk.setValidCharacters(“ABC0123456.”);
*/
texCPF = new JFormattedTextField(mk);
texCPF.setBounds(50,155,100,20);
add(texCPF);
}
catch(Exception e){}
//Label Interesses
lblInt = new JLabel("Interesses: ");
lblInt.setBounds(10,185,100,20);
lblInt.setForeground(Color.WHITE);
add(lblInt);
//Check Inglês
chkIng = new JCheckBox(“Inglês”);
chkIng.setBounds(10,205,120,20);
chkIng.setForeground(Color.BLUE);
chkIng.setBackground(Color.RED);
add(chkIng);
//Check Matemática
chkMat = new JCheckBox(“Matemática”);
chkMat.setBounds(170,205,120,20);
chkMat.setForeground(Color.BLUE);
chkMat.setBackground(Color.RED);
add(chkMat);
//Check Programação
chkPro = new JCheckBox(“Programação”);
chkPro.setBounds(10,235,120,20);
chkPro.setForeground(Color.BLUE);
chkPro.setBackground(Color.RED);
add(chkPro);
//Check Lógica
chkLog = new JCheckBox(“Lógica”);
chkLog.setBounds(170,235,120,20);
chkLog.setForeground(Color.BLUE);
chkLog.setBackground(Color.RED);
add(chkLog);
//Botão Ok
btnGrav = new JButton(“Ok”);
btnGrav.setBounds(110,285,120,40);
btnGrav.setMnemonic(‘O’);
btnGrav.setToolTipText(“Ok…”);
btnGrav.setForeground(Color.RED);
btnGrav.addActionListener(this);
add(btnGrav);
}
//Função responsável pelos cliques
public void actionPerformed(ActionEvent acesso){
String sSexo="";
String sNivel="";
String sExec="";
Boolean bChave=true;
//se o acesso for via botão gravar-ok
if(acesso.getSource() == btnGrav){
//pegando o nivel de escolaridade
sNivel=cmbNiv.getSelectedItem().toString();
//verificando os radios
if(radFem.isSelected())
sSexo=“F”;
if(radMasc.isSelected())
sSexo=“M”;
Calendar c = Calendar.getInstance();
//montando a frase que será mostrada
sExec="Nome = '"+texNome.getText()+"'\n"+
"Sexo = '"+sSexo+"'\n"+
"Nivel= '"+sNivel+"'\n"+
"CPF = '"+texCPF.getText()+"'\n"+
"-----------------------------\n"+
"Interesses:\n"+
"Inglês? "+chkIng.isSelected()+"\n"+
"Matemática? "+chkMat.isSelected()+"\n"+
"Programação? "+chkPro.isSelected()+"\n"+
"Lógica? "+chkLog.isSelected()+"\n"+"\n"+
//c+"\n"+"\n"+
"Data: "+c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR)+"\n"+
"Hora: "+c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
//mostrando a frase
JOptionPane.showMessageDialog(null,sExec,"Atenção",1);
//se a chave for verdadeira, chama função limpaDados
if(bChave){
limpaDados();
}
//Muda o foco do componente
texNome.grabFocus();
}
}
//Função principal que irá criar a janela
public static void main(String arg[])
{
Formulario ex = new Formulario();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.getContentPane().setBackground(Color.RED);
//ícone na mesma pasta do código fonte
ex.setIconImage(new ImageIcon(“ico.png”).getImage());
ex.setTitle(“Estudantes…”);
//tira o maximizar e alteração do tamanho
ex.setResizable(true);
ex.setSize(350,300);
ex.setVisible(true);
ex.setLocationRelativeTo(null);
}
//função que limpa o programa e deixa ele como executado da primeira vez
private void limpaDados(){
texNome.setText("");
texCPF.setText("");
radMasc.setSelected(true);
chkIng.setSelected(false);
chkMat.setSelected(false);
chkPro.setSelected(false);
chkLog.setSelected(false);
cmbNiv.setSelectedIndex(0);
}
}