SWT

package com.swt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class Client {

	public Client() {
		this.test_();
	}

	public void test() {
		JLabel jl = new JLabel("This is a text label");

		JButton jb = new JButton("OK");
		// jb.setPreferredSize(new Dimension(100, 50));
		// jb.setBounds(100, 0, 100, 50);
		/*
		 * //设置button 边框 LineBorder border = new LineBorder(Color.black);
		 * jb.setBorder(border);
		 */
		jb.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("Button Clicked");

			}
		});
		/*
		 * jb.addChangeListener(new ChangeListener() {
		 * 
		 * @Override public void stateChanged(ChangeEvent arg0) { // TODO
		 * Auto-generated method stub System.out.println("Button Changed"); }
		 * });
		 */

		JTextArea jt = new JTextArea("JTextArea");
		jt.setBorder(new LineBorder(Color.lightGray));

		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(2, 2));
		/*
		 * 顺序布局(FlowLayout) 左右规矩相邻 
		 * 边界布局(BorderLayout) 分东南西北中
		 * 网格布局(GridLayout) 上下左右规矩相邻
		 * 网格袋布局(GridBagLayout) 上下左右不规矩相邻 
		 * null布局 自由布局
		 * 卡片布局(CardLayout)
		 * 
		 * jp.setLayout(new BorderLayout()); jp.add(BorderLayout.NORTH, jl);
		 * jp.add(BorderLayout.EAST, jb); jp.add(BorderLayout.SOUTH, jt);
		 * jp.setLayout(new CardLayout());
		 * jp.setLayout(new BorderLayout());
		 * jp.setLayout(new GridLayout());
		 * jp.setLayout(new GridBagLayout());
		 * jp.setLayout(null);
		 */
		jp.add(jl);
		jp.add(jt);
		jp.add(jb);

		JFrame jf = new JFrame("Swing");
		jf.setSize(400, 300);
		jf.add(jp);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLayout(new FlowLayout(FlowLayout.LEFT));
		// 设置swing JFrame初始位置为屏幕中央
		jf.setLocationRelativeTo(null);
		jf.setVisible(true);
		// jf.getRootPane().setDefaultButton(jb);
	}

	public void test_() {
		
		JFrame f = new JFrame();
		f.setSize(220, 120);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel l_username = new JLabel("用户名");
		JLabel l_password = new JLabel("密码");
		final JTextField t_username = new JTextField();
		final JPasswordField t_password = new JPasswordField();
		
		JButton b1 = new JButton("登录");
		JButton b2 = new JButton("注册");
		JButton b3 = new JButton("取消");
		
		class IListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String name = null;
				String pass = null;
				pass = t_password.getPassword().toString();
				name = t_username.getText();
				System.out.println(name);
				System.out.println(pass);
			}
		}
		IListener l = new IListener();
		b1.addActionListener(l);
		
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(2, 2));
		p1.add(l_username);
		p1.add(t_username);
		p1.add(l_password);
		p1.add(t_password);
		
		JPanel p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		p2.add(b1);
		p2.add(b2);
		p2.add(b3);
		
		f.setLayout(new BorderLayout());
		f.add(p1, BorderLayout.CENTER);
		f.add(p2, BorderLayout.SOUTH);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}

	public static void main(String[] args) {
		/*
		 * Client client = new Client(); client.test();
		 */
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Client();
			}
		});
	}
}

你可能感兴趣的:(SWT)