VB icon

Simple SWING Example Very Helpful, For Me Anyway.

Submitted on: 1/3/2015 5:07:00 PM
By: C.V. (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 1699
     This little thingy here just demonstrates the very primitive use of SWING!!!!!!!!!!!!!!!!!!!!!!!!!
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Simple SWING Example Very Helpful, For Me Anyway.
// Description:This little thingy here just demonstrates the very primitive use of SWING!!!!!!!!!!!!!!!!!!!!!!!!!
// By: C.V. (from psc cd)
// Side Effects:Sticky fingers.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Swing1 extends JFrame implements DocumentListener, ActionListener {
public Swing1() {
//Set attributes of the frame
setTitle("SWING Sample One");
setSize(600, 400);
setLocation(100, 100);
//Define the container for all of the controls and the main panel(s)
Container contentPane = getContentPane();
JPanel panel1 = new JPanel(null);
//Define the components of the held within the frame and panel(s)
text1 = new JTextField("", 10);
button1 = new JButton("Set");
button2 = new JButton("Get");
text1.setBounds(20, 20, 80, 20);
button1.setBounds(120, 20, 80, 20);
button2.setBounds(220, 20, 80, 20);//Defines the coordinates of the component Format=(x, y, w, h)
panel1.add(button2);//Adds the component to the panel
//Add the ActionListeners for the components
text1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
System.out.println("Key pressed!");
button1.addMouseListener(new javax.swing.event.MouseInputAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
button2.addMouseListener(new javax.swing.event.MouseInputAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
JOptionPane.showMessageDialog(null, text1.getText(), "text1 has the value of:", 1);
//Define the menu and their items. Coded here in the heirarchy's visual format
menuBar = new JMenuBar();
mnuFile = new JMenu("File");
mnuNew = new JMenuItem("New");
mnuNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, mnuNew.getText(), "The item you selected is:", 1);
mnuOpen = new JMenu("Open");
mnuItem1 = new JMenuItem("Item 1");
mnuItem2 = new JMenuItem("Item 2");
mnuItem3 = new JMenuItem("Item 3");
//Add the menu items
mnuExit = new JMenuItem("Exit");
mnuCheckBoxMenuItem = new JCheckBoxMenuItem("Check Box Menu Item");
sepOne = new JSeparator();
//Add the menu items
//Add the top-level menus to the menuBar
//Add the top-level components (i.e. Panels, MenuBar, etc...)
contentPane.add(menuBar, "North");
//Monitor for the closing of the window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
public static void main(String[] args) {
JFrame frame = new Swing1();
public void actionPerformed(ActionEvent e) {}
public void insertUpdate(DocumentEvent e) {}
public void changedUpdate(DocumentEvent e) {}
public void removeUpdate(DocumentEvent e) {}
//Global declarations for all components used
private JButton button1;
private JButton button2;
private JTextField text1;
private JMenuBar menuBar;
private JMenu mnuFile;
private JMenuItem mnuNew;
private JMenu mnuOpen;
private JMenuItem mnuExit;
private JMenuItem mnuItem1;
private JMenuItem mnuItem2;
private JMenuItem mnuItem3;
private JCheckBoxMenuItem mnuCheckBoxMenuItem;
private JSeparator sepOne;

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.