Open all | Close all

Code of simple Swing Java Applications

Below you will find the Java code for creating a simple Swing application.

Simple Swing java application(Label)
//package swing;
import java.awt.*;
import javax.swing.*;
public class Panel1 {
	public static void main(String[] args) {

		JFrame frame = new JFrame("Texts");
		JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
		JLabel label = new JLabel("This is a Label");


		pane.setLayout(new GridLayout(2, 2));
		pane.add(label);
		pane.setBounds(20, 30, 10, 30);

		frame.getContentPane().add(pane, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

 

Simple Swing java application(Text Area)
//package swing;
import java.awt.*;
import javax.swing.*;
public class scrollPane {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Scrollpane");
		JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));

      JTextArea myText = new JTextArea("This is an example \n"
      				+  	" swing text area");
		JScrollPane scrollPane = new JScrollPane(myText);
		pane.setLayout(new GridLayout(1, 1));
		pane.add(scrollPane);

		frame.getContentPane().add(pane, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

 

Simple Swing java application(two buttons which increment counters)
//package swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonApplication {
	private static String labelPrefix =
		                "No. of times button clicked: ";
	private int numClicks  = 0;
	private int numClicks2 = 0;

	public Component createComponents() {
		final JLabel label  = new JLabel(labelPrefix + "0    ");
		final JLabel label2 = new JLabel(labelPrefix + "0    ");
		
		JButton button1 = new JButton("Click me");
		JButton button2 = new JButton("Click me");
		
		button1.setMnemonic('i'); // press alt+i
		button2.setMnemonic('j'); // press alt+j	
			
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				numClicks++;
				label.setText(labelPrefix + numClicks);
			}
		});
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				numClicks2++;
				label2.setText(labelPrefix + numClicks2);
			}
		});
		label.setLabelFor(button1);
		label.setLabelFor(button2);

		JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createEmptyBorder(30, //top
		30, //left
		10, //bottom
		30) //right
		);
		pane.setLayout(new GridLayout(2, 2));
		pane.add(button1);
		pane.add(button2);
		pane.add(label);
		pane.add(label2);
		return pane;
	}
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(
			UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
		}
		//Create the top-level container and add contents to it.
		JFrame frame = new JFrame("SwingApplication");
		ButtonApplication app = new ButtonApplication();
		
		Component contents = app.createComponents();
		frame.getContentPane().add(contents, BorderLayout.CENTER);

		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

 


SAP ABAP development help
More SAP ABAP development help and information


ABAP code and information
More SAP ABAP development tips and tricks

 
 

Number of SAP Development Users currently online hit counters