Click the button without using GUI

Asked

Viewed 56 times

1

I am developing a java system and would like to do it in a more organized way, so without using the netbeans tool to click and drag, but I’m having a hard time clicking the exit button. How to do ?

1 answer

1

What’s so hard about that?

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * @author Victor
 */
public class TelaSair {
    public static void main(String[] args) {
        EventQueue.invokeLater(TelaSair::criarTela);
    }

    private static void criarTela() {
        JFrame tela = new JFrame("Isto é uma tela");
        tela.setResizable(true);
        tela.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JButton seuBotao = new JButton("Sair");
        seuBotao.setPreferredSize(new Dimension(300, 30));
        tela.add(seuBotao);
        seuBotao.addActionListener(e -> tela.dispose());
        tela.pack();
        tela.setVisible(true);
    }
}

Compile and run with Java 8.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.