Created
October 8, 2013 01:18
-
-
Save matiasfha/6877914 to your computer and use it in GitHub Desktop.
Java Swing Quit Button example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
public class QuitButtonExample extends JFrame { | |
public QuitButtonExample() { | |
initUI(); | |
} | |
private void initUI() { | |
JPanel panel = new JPanel(); | |
getContentPane().add(panel); | |
panel.setLayout(null); | |
JButton quitButton = new JButton("Quit"); | |
quitButton.setBounds(50, 60, 80, 30); | |
quitButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent event) { | |
System.exit(0); | |
} | |
}); | |
panel.add(quitButton); | |
setTitle("Quit button"); | |
setSize(300, 200); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
QuitButtonExample ex = new QuitButtonExample(); | |
ex.setVisible(true); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment