import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
@SuppressWarnings("serial")
public class Janela extends JFrame {
private JTabbedPane tabbedPane = null;
private PainelPrincipal principal = new PainelPrincipal();
private PainelSecundario secundario = new PainelSecundario();
private JButton button = new JButton("Confirmar");
public Janela() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
setTitle("Janela Principal");
setLayout(null);
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Nome", principal);
tabbedPane.addTab("Idade", secundario);
tabbedPane.setSize(300, 100);
tabbedPane.setLocation(0, 0);
add(tabbedPane);
button.setSize(100, 30);
button.setLocation(100,110);
button.addActionListener(new Acao());
add(button);
setVisible(true);
}
class Acao implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
if(principal.getFieldNome().getText().equals("")){
principal.getFieldNome().requestFocus();
} else {
String mensagem = "Nome: " + principal.getFieldNome().getText();
JOptionPane.showMessageDialog(null, mensagem);
}
if(secundario.getFieldIdade().getText().equals("")){
secundario.getFieldIdade().requestFocus();
}else {
String mensagem = "Idade: " + secundario.getFieldIdade().getText();
JOptionPane.showMessageDialog(null, mensagem);
}
}
}
}
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class PainelPrincipal extends JPanel {
private JLabel labelNome = null;
private JTextField fieldNome = null;
public PainelPrincipal() {
initialize();
}
private void initialize() {
setSize(300, 100);
setLayout(null);
labelNome = new JLabel();
labelNome.setText("Nome");
labelNome.setBounds(new Rectangle(10, 10, 70, 30));
add(labelNome);
fieldNome = new JTextField();
fieldNome.setBounds(new Rectangle(90, 10, 70, 30));
add(fieldNome);
}
public JTextField getFieldNome() {
return fieldNome;
}
}
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class PainelSecundario extends JPanel {
private JLabel labelIdade = null;
private JTextField fieldIdade = null;
public PainelSecundario() {
initialize();
}
private void initialize() {
setSize(300, 100);
setLayout(null);
labelIdade = new JLabel();
labelIdade.setText("Idade");
labelIdade.setBounds(new Rectangle(10, 10, 70, 30));
add(labelIdade);
fieldIdade = new JTextField();
fieldIdade.setBounds(new Rectangle(90, 10, 70, 30));
add(fieldIdade);
}
public JTextField getFieldIdade() {
return fieldIdade;
}
}
public class Main {
public static void main(String[] args) {
Janela j = new Janela();
}
}