java初学--JFrame



import java.awt.Color;
import java.awt.Container;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class Example1 extends JFrame{
	public void CreateJFrame(String title) {
		JFrame jf=new JFrame(title);
		Container container=jf.getContentPane();//获取一个容器
		JLabel jl=new JLabel("这是一个twh233写的JFrame窗体");//创建一个JLabel标签
		jl.setHorizontalAlignment(SwingConstants.CENTER);//居中
		container.add(jl);//添加
		container.setBackground(Color.white);
		jf.setVisible(true);
		jf.setSize(200,150);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		//关闭模式
	}
	public static void main(String[] args) {
		new Example1().CreateJFrame("创建一个JFrame窗体");
	}
}





窗口关闭方式。

DO_NOTHING_ON_CLOSE  代表什么都不做就将窗口关闭

DISPOSE_ON_CLOSE 代表任何注册监听程序对象后会自动隐藏并释放窗体

HIDE_ON_CLOSE 表示隐藏窗口的默认窗口关闭

EXIT_ON_CLOSE 表示退出应用程序默认窗口关闭



对话框窗体:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MyJDialog extends JDialog { // 创建新类继承JDialog类

	private static final long serialVersionUID = 1L;

	public MyJDialog(MyFrame frame) {
		// 实例化一个JDialog类对象,指定对话框的父窗体、窗体标题和类型
		super(frame, "第一个JDialog窗体", true);
		Container container = getContentPane(); // 创建一个容器
		container.add(new JLabel("这是一个对话框")); // 在容器中添加标签
		setBounds(120, 120, 100, 100); // 设置对话框窗体大小
	}
}

public class MyFrame extends JFrame { // 创建新类

	private static final long serialVersionUID = 1L;

	public static void main(String args[]) {
		new MyFrame(); // 实例化MyJDialog类对象
	}
	
	public MyFrame() {
		Container container = getContentPane(); // 创建一个容器
		container.setLayout(null);
		JLabel jl = new JLabel("这是一个JFrame窗体"); // 在窗体中设置标签
		// 将标签的文字置于标签中间位置
		jl.setHorizontalAlignment(SwingConstants.CENTER);
		container.add(jl);
		JButton bl = new JButton("弹出对话框"); // 定义一个按钮
		bl.setBounds(10, 10, 100, 21);
		bl.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件
					public void actionPerformed(ActionEvent e) {
						// 使MyJDialog窗体可见
						new MyJDialog(MyFrame.this).setVisible(true);
					}
				});
		container.add(bl); // 将按钮添加到容器中
		
		container.add(bl);
		container.setBackground(Color.white);
		setSize(200, 200);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
}





//Icon 创建图标
import java.awt.*;

import javax.swing.*;

public class DrawIcon implements Icon { // 实现Icon接口
	private int width; // 声明图标的宽
	private int height; // 声明图标的长
	
	public int getIconHeight() { // 实现getIconHeight()方法
		return this.height;
	}
	
	public int getIconWidth() { // 实现getIconWidth()方法
		return this.width;
	}
	
	public DrawIcon(int width, int height) { // 定义构造方法
		this.width = width;
		this.height = height;
	}
	
	// 实现paintIcon()方法
	public void paintIcon(Component arg0, Graphics arg1, int x, int y) {
		arg1.fillOval(x, y, width, height); // 绘制一个圆形
	}
	
	public static void main(String[] args) {
		DrawIcon icon = new DrawIcon(15, 15);
		// 创建一个标签,并设置标签上的文字在标签正中间
		JLabel j = new JLabel("测试", icon, SwingConstants.CENTER);
		JFrame jf = new JFrame(); // 创建一个JFrame窗口
		Container c = jf.getContentPane();
		c.add(j);
		jf.setSize(100,100);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
}


使用图片图标

import java.awt.Container;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class MyImageIcon extends JFrame{
	private static final long serialVersionUID = 1676749251669355900L;
	public MyImageIcon() {
		Container c=getContentPane();
		JLabel jf=new JLabel("这是一个JFrame窗体",JLabel.CENTER);
		//获取图片所在的URL
		URL url=MyImageIcon.class.getResource("imageButton.jpg");
		Icon icon=new ImageIcon(url);//实例化Icon对象
		jf.setIcon(icon);
		//放入中间
		jf.setHorizontalAlignment(SwingConstants.CENTER);
		jf.setOpaque(true);//设置标签为不透明状态
		c.add(jf);
		setSize(250,100);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new MyImageIcon();

	}

}



布局类:

1.自定义布局:

import java.awt.*;

import javax.swing.*;

public class AbsolutePosition extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AbsolutePosition() {
		setTitle("本窗体使用绝对布局"); // 设置该窗体的标题
		setLayout(null); // 使该窗体取消布局管理器设置
		setBounds(0, 0, 200, 150); // 绝对定位窗体的位置与大小
		Container c = getContentPane(); // 创建容器对象
		JButton b1 = new JButton("按钮1"); // 创建按钮
		JButton b2 = new JButton("按钮2"); // 创建按钮
		b1.setBounds(10, 30, 80, 30); // 设置按钮的位置与大小
		b2.setBounds(60, 70, 100, 20);
		c.add(b1); // 将按钮添加到容器中
		c.add(b2);
		setVisible(true); // 使窗体可见
		// 设置窗体关闭方式
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new AbsolutePosition();
	}
}

2.流布局

import java.awt.*;

import javax.swing.*;

public class FlowLayoutPosition extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public FlowLayoutPosition() {
		setTitle("本窗体使用流布局管理器"); // 设置窗体标题
		Container c = getContentPane();
		// 设置窗体使用流布局管理器,使组件右对齐,并且设置组件之间的水平间隔与垂直间隔
		setLayout(new FlowLayout(2, 10, 10));
		for (int i = 0; i < 10; i++) { // 在容器中循环添加10个按钮
			c.add(new JButton("button" + i));
		}
		setSize(300, 200); // 设置窗体大小
		setVisible(true); // 设置窗体可见
		// 设置窗体关闭方式
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new FlowLayoutPosition();
	}
}


3.边界布局

import java.awt.*;

import javax.swing.*;

public class BorderLayoutPosition extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 定义组件摆放位置的数组
	String[] border = { BorderLayout.CENTER, BorderLayout.NORTH,
			BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST };
	String[] buttonName = { "center button", "north button",
			"south button", "west button", "east button" };
	
	public BorderLayoutPosition() {
		setTitle("这个窗体使用边界布局管理器");
		Container c = getContentPane(); // 定义一个容器
		setLayout(new BorderLayout()); // 设置容器为边界布局管理器
		for (int i = 0; i < border.length; i++) {
			// 在容器中添加按钮,并设置按钮布局
			c.add(border[i], new JButton(buttonName[i]));
		}
		setSize(350, 200); // 设置窗体大小
		setVisible(true); // 使窗体可视
		// 设置窗体关闭方式
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new BorderLayoutPosition();
	}
}

4.网格布局


面板类:

1.JPanel 


import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class JPanelTest extends JFrame{
	public JPanelTest(){
		Container c = getContentPane();
		// 将整个容器设置为2行1列的网格布局
		c.setLayout(new GridLayout(2, 1, 10, 10));
		// 初始化一个面板,设置1行3列的网格布局
		JPanel p1 = new JPanel(new GridLayout(1, 3, 10, 10));
		JPanel p2 = new JPanel(new GridLayout(1, 2, 10, 10));
		JPanel p3 = new JPanel(new GridLayout(1, 2, 10, 10));
		JPanel p4 = new JPanel(new GridLayout(2, 1, 10, 10));
		p1.add(new JButton("1")); // 在面板中添加按钮
		p1.add(new JButton("1"));
		p1.add(new JButton("2"));
		p1.add(new JButton("3"));
		p2.add(new JButton("4"));
		p2.add(new JButton("5"));
		p3.add(new JButton("6"));
		p3.add(new JButton("7"));
		p4.add(new JButton("8"));
		p4.add(new JButton("9"));
		c.add(p1); // 在容器中添加面板
		c.add(p2);
		c.add(p3);
		c.add(p4);
		setTitle("在这个窗体中使用了面板");
		setSize(420, 200);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new JPanelTest();
	}
}




2.JScrollPane

带滚动条的面板

package com.lzw;

import java.awt.*;

import javax.swing.*;

public class JScrollPaneTest extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public JScrollPaneTest() {
		Container c = getContentPane(); // 创建容器
		JTextArea ta = new JTextArea(20, 50); // 创建文本区域组件
		JScrollPane sp = new JScrollPane(ta); // 创建JScrollPane面板对象
		c.add(sp); // 将该面板添加到该容器中
		
		setTitle("带滚动条的文字编译器");
		setSize(200, 200);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new JScrollPaneTest();
		
	}
	
}



按钮类:

1.JButton

public JButton(String text,Icon icon)


import java.awt.*;
import java.awt.event.*;
import java.net.*;

import javax.swing.*;

public class JButtonTest extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public JButtonTest() {
		URL url = JButtonTest.class.getResource("imageButtoo.jpg");
		Icon icon = new ImageIcon(url);
		setLayout(new GridLayout(3, 2, 5, 5)); // 设置网格布局管理器
		Container c = getContentPane(); // 创建容器
		for (int i = 0; i < 5; i++) {
			// 创建按钮,同时设置按钮文字与图标
			JButton J = new JButton("button" + i, icon);
			c.add(J); // 在容器中添加按钮
			if (i % 2 == 0) {
				J.setEnabled(false); // 设置其中一些按钮不可用
			}
		}
		JButton jb = new JButton(); // 实例化一个没有文字与图片的按钮
		jb.setMaximumSize(new Dimension(90, 30)); // 设置按钮与图片相同大小
		jb.setIcon(icon); // 为按钮设置图标
		jb.setHideActionText(true);
		jb.setToolTipText("图片按钮"); // 设置按钮提示为文字
		jb.setBorderPainted(false); // 设置按钮边界不显示
		jb.addActionListener(new ActionListener() { // 为按钮添加监听事件
					public void actionPerformed(ActionEvent e) {
						// 弹出确认对话框
						JOptionPane.showMessageDialog(null, "弹出对话框");
					}
				});
		c.add(jb); // 将按钮添加到容器中
		
		setTitle("创建带文字与图片的按钮");
		setSize(350, 150);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String args[]) {
		new JButtonTest();
	}
}


2.单选按钮

3.复选按钮


列表类:



1.JComboBox类

import java.awt.*;

import javax.swing.*;

public class JComboBoxModelTest extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JComboBox<String> jc = new JComboBox<>(new MyComboBox());
	JLabel jl = new JLabel("请选择证件:");
	
	public JComboBoxModelTest() {
		setSize(new Dimension(160, 180));
		setVisible(true);
		setTitle("在窗口中设置下拉列表框");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(jl);
		cp.add(jc);
	}
	
	public static void main(String[] args) {
		new JComboBoxModelTest();
	}
}

class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String selecteditem = null;
	String[] test = { "身份证", "军人证", "学生证", "工作证" };
	
	public String getElementAt(int index) {
		return test[index];
	}
	
	public int getSize() {
		return test.length;
	}
	
	public void setSelectedItem(Object item) {
		selecteditem = (String) item;
	}
	
	public Object getSelectedItem() {
		return selecteditem;
	}
	
	public int getIndex() {
		for (int i = 0; i < test.length; i++) {
			if (test[i].equals(getSelectedItem()))
				return i;
		}
		return 0;
	}
}


2.列表框组件



文本类:

1.文本框组件


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JTextFieldTest extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JTextFieldTest(){
		setSize(250,100);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container cp=getContentPane();
		getContentPane().setLayout(new FlowLayout());
		final JTextField jt=new JTextField("aaa",20);
		final JButton jb=new JButton("清除");
		cp.add(jt);
		cp.add(jb);
		jt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成方法存根
				jt.setText("触发事件");
			}
		});
		jb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jt.setText("");
				jt.requestFocus();
			}
		});	
		setVisible(true);
	}
	public static void main(String[] args) {
		new JTextFieldTest();
	}
}

2.密码框组件

JpasswordField jp=new JPasswordField();

jp.setEchoChar('#');

3.文本域组件


监听类:


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleEvent extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton jb=new JButton("我是按钮,单击我");
	public SimpleEvent(){
		setLayout(null);
		setSize(200,100);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container cp=getContentPane();
		cp.add(jb);
		jb.setBounds(10, 10,100,30);
		jb.addActionListener(new jbAction());
		setVisible(true);
	}
	class jbAction implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			jb.setText("我被单击了");
		}
	}
	public static void main(String[] args) {
		new SimpleEvent();
	}
}







猜你喜欢

转载自blog.csdn.net/qq_36553623/article/details/78745222
今日推荐