The code below I got from the internet is working for me.
In main
, it executes a form with the name to type and the ok
button.
I wanted to run in main
, right after the
new MeuPrograma();
a System.out.println("##")
where it displays the name that the guy typed there in the field of swing
How do I do this?
public class MeuPrograma extends JFrame {
public static JTextField text = new JTextField(10);
private static final long serialVersionUID = 1L;
private JLabel labelNome;
private JTextField textFieldNome;
private JButton buttonOk;
public MeuPrograma() {
setTitle("Programa Swing1");
setLayout(new FlowLayout());
labelNome = new JLabel("Nome: ");
textFieldNome = new JTextField(15);
buttonOk = new JButton("OK");
// --> adiciona os componentes a janela
add(labelNome);
add(textFieldNome);
add(buttonOk);
// --> ajusta o tamanho, a posicao e a acao ao fechar
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// --> mostra a janela
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// --> cria um novo objeto do tipo Swing1
// por causa da execucao multithreading da
// API swing,isso deve ser feito dessa forma:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MeuPrograma();
}
});