הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Swing UI Examples

import javax.swing.JFrame;
import javax.swing.JLabel;
 
 
public class MyFirstUI {
 
	public static void main(String [] args) {
		JFrame f = new JFrame("Hello World");
		f.setSize(300, 200);
		JLabel l = new JLabel("Hello World", JLabel.CENTER);
 
		f.add(l);
		f.setVisible(true);
	}
}
import javax.swing.JFrame;
import javax.swing.JLabel;
 
 
public class MySpecialHelloFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
 
	public MySpecialHelloFrame() {
		setSize(320, 200);
		setTitle("MySpecialHelloFrame");
		JLabel l = new JLabel("Hello World");
		add(l);
	}
 
	public static void main(String [] args) {
		MySpecialHelloFrame f = new MySpecialHelloFrame();
		f.setVisible(true);
	}
}
import java.awt.Color;
import java.awt.FlowLayout;
 
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
 
public class ComplexLayouts extends JFrame {
 
	public ComplexLayouts() {
		setupComponents();
		setupActions();
 
		setVisible(true);
	}
 
	public void setupComponents() {
		setSize(300, 200);
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
 
		p1.setLayout(new FlowLayout());
		p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
 
		p1.setBackground(Color.RED);
		p2.setBackground(Color.blue);
 
		p1.add(new JButton("p1"));
		p1.add(new JButton("p2"));
		p1.add(new JButton("p3"));
 
		p2.add(new JButton("q1"));
		p2.add(new JButton("q3"));
		p2.add(new JButton("q2"));
 
		getContentPane().setLayout(new FlowLayout());
		getContentPane().setBackground(Color.yellow);
		getContentPane().add(p1);
		getContentPane().add(p2);
	}
 
	public void setupActions() {
		// this one is left empty on purpose - no actions in this frame
	}
 
	public static void main(String [] args) {
		new ComplexLayouts();		
	}
}
import java.awt.BorderLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
 
public class Buttons1 {
	private JFrame frame;
 
	public Buttons1() {
		this.frame = new JFrame("Buttons 1");
		this.frame.setSize(300, 300);
		JButton ben = new JButton("Click Me !");
 
		this.frame.add(ben, BorderLayout.SOUTH);
		System.out.println("The south is: ");
		this.frame.setVisible(true);
	}
 
	public static void main(String [] args) {
		Buttons1 b1 = new Buttons1();
		Buttons2 b2 = new Buttons2();
		Buttons3 b3 = new Buttons3();
	}
}
import java.awt.BorderLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
 
public class Buttons2 {
	private JFrame frame;
 
	public Buttons2() {
		this.frame = new JFrame("Buttons 2");
		this.frame.setSize(300, 300);
		JButton ben   = new JButton("Click Me !");
		JButton jerry = new JButton("Click Him !");
		JButton tom   = new JButton("7");
 
		this.frame.getContentPane().add(ben, BorderLayout.SOUTH);
		this.frame.getContentPane().add(jerry, BorderLayout.CENTER);
		this.frame.getContentPane().add(tom, BorderLayout.WEST);
 
		System.out.println("Frame = " + this.frame.getClass() + "; content pane = " + this.frame.getContentPane().getClass());
		this.frame.setVisible(true);
	}
}
import java.awt.FlowLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
 
public class Buttons3 {
	private JFrame frame;
 
	public Buttons3() {
		this.frame = new JFrame("Buttons 3");
		this.frame.setSize(300, 300);
 
		JButton ben = new JButton("Click Me !");
		FlowLayout flow = new FlowLayout();
		this.frame.getContentPane().setLayout(flow);
		this.frame.getContentPane().add(ben);
 
		System.out.println("Frame = " + this.frame.getClass() + "; content pane = " + this.frame.getContentPane().getClass());
		this.frame.setVisible(true);
 
	}
}
import java.awt.BorderLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
 
public class Rockets {
	private JFrame frame;
 
	public Rockets() {
		frame = new JFrame("לא מדע טילים");
		frame.setSize(300, 200);
 
		JPanel p = new JPanel();
		JLabel text = new JLabel("ברוכים הבאים");
 
		text.setHorizontalAlignment(JLabel.CENTER);
 
		frame.getContentPane().add(p, BorderLayout.SOUTH);
		p.add(new JButton("1"));
		p.add(new JButton("2"));
		p.add(new JButton("3"));
		p.add(new JButton("4"));
		p.add(new JButton("5"));
 
		frame.getContentPane().add(text, BorderLayout.CENTER);
 
		frame.setVisible(true);
	}
 
	public static void main(String [] args) {
		Rockets r = new Rockets();
 
	}
}
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
import javax.swing.JButton;
 
 
public class ChangeTextAction extends AbstractAction {
	private String text1;
	private String text2;
	private JButton b;
	private String currentText;
 
	public ChangeTextAction(String text1, String text2) {
		this.text1 = text1;
		this.text2 = text2;
		this.currentText = text1;
	}
 
	public void setButton(JButton b) {
		this.b = b;
	}
 
	public void actionPerformed(ActionEvent arg0) {
		this.b.setText(this.currentText);
 
		if ( this.currentText == this.text1 ) {
			this.currentText = this.text2;
		} else {
			this.currentText = this.text1;
		}
	}
 
}
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
 
public class Buttons4 {
	private JFrame frame;
 
	public Buttons4() {
		frame = new JFrame("Buttons 4");
 
		ChangeTextAction action = new ChangeTextAction("Eat", "Drink"); 
		JButton b = new JButton(action);
		action.setButton(b);
 
		frame.add(b);
		b.setText("AHHHHHHHHHHHHHHHHHH !!!!!");
		frame.setVisible(true);
	}
 
	public static void main(String [] args) {
		Buttons4 b = new Buttons4();
	}
}
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
 
 
public class MyReallySimpleExample implements MouseMotionListener {
	JLabel label;
 
	public MyReallySimpleExample(JLabel label) {
		this.label = label;
	}
	public void mouseDragged(MouseEvent arg0) {
 
	}
 
	public void mouseMoved(MouseEvent arg0) {
		Point p = arg0.getLocationOnScreen();
		this.label.setText("x = " + p.getX() + " , y = " + p.getY());
	}
 
	public static void main(String [] args) {
		JFrame frame = new JFrame("Window Listeners are fun");
		JLabel label = new JLabel();
		frame.add(label, BorderLayout.CENTER);
 
		MyReallySimpleExample ex = new MyReallySimpleExample(label);
 
		frame.addMouseMotionListener(ex);
		frame.setSize(400, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
package models;
 
import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
 
public class CarsModel extends AbstractListModel {
 
	public Object getElementAt(int arg0) {
 
		return "Chevrolet";
	}
 
	public int getSize() {
		// TODO Auto-generated method stub
		return 7;
	}
 
}
package views;
 
import javax.swing.JFrame;
import javax.swing.JList;
 
import models.CarsModel;
 
public class MainView {
	private JFrame frame;
	private JList  list;
 
	public MainView() {
		this.frame = new JFrame("Hello World");
		this.frame.setSize(200, 300);
 
		list = new JList(new CarsModel());
		this.frame.add(list);
		this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.frame.setVisible(true);
	}
 
	public static void main(String [] args) {
		new MainView();
	}
}
course: