Swing.JFrame基础

Swing.JFrame基础

弹出对话框

/*
 * TreeFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jframe_1;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author 弹出对话框
 */
class MyJDialog extends JDialog {
	public MyJDialog(num1 frame) {
		super(frame,"第一个JDialog窗体",true);
		Container contianer = getContentPane();
		contianer.add(new JLabel("这是一个对话框"));
		setBounds(120,120,100,100);
	}
}
public class num1 extends JFrame {
	
	public num1() {
		JFrame jf = new JFrame();
		Container container = jf.getContentPane();//创建一个容器
		container.setLayout(null);//使该窗体取消布局管理器设置,即绝对布局
		JLabel jl = new JLabel("这是一个JFrame窗体");//在窗体中设置一个标签
		jl.setHorizontalAlignment(SwingConstants.CENTER);//标签文字在标签中居中
		container.add(jl);//把标签添加到容器中
		JButton bl = new JButton("弹出对话框");//定义一个按钮
		bl.setBounds(100, 10, 100, 21);//x;y;wight;heigth
		bl.addActionListener((ActionListener) new ActionListener() {//为按钮添加鼠标单击事件
			public void actionPerformed(ActionEvent e) {
				new MyJDialog(num1.this).setVisible(true);
			}
		});
		container.add(bl);//添加到容器中
		container.setBackground(Color.white);//容器背景色
		jf.setVisible(true);//使窗体可视
		jf.setSize(400,300);//窗体大小
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体关闭方式
	}

	public void CreateJFrame(String title) {
		JFrame jf = new JFrame(title);
		Container container = jf.getContentPane();
		JLabel jl = new JLabel("这是一个窗体");
		jl.setHorizontalAlignment(SwingConstants.HORIZONTAL);
		jl.setSize(100, 20);
		container.add(jl);
		container.setBackground(Color.white);
		jf.setVisible(true);
		jf.setSize(400,300);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String arg[]) {
		new num1();
	}
}

在这里插入图片描述

图标的使用

package com.jframe_1;
import java.awt.*;

import javax.swing.*;
/**
*
* @author 设置带图标的标签
*/
public class DrawIcon implements Icon{
	private int width;
	private int height;
	public int getIconHeight() {
		return this.height;
	}
	public int getIconWidth() {
		return this.width;
	}
	public DrawIcon(int width, int height) {
		this.width = width;
		this.height = height;
	}
	public void paintIcon(Component arg0,Graphics arg1,int x,int y) {
		arg1.fillOval(x, y, width, height);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DrawIcon icon = new DrawIcon(15,15);
		JLabel j = new JLabel("测试",icon,SwingConstants.CENTER);
		JFrame jf = new JFrame();
		Container c = jf.getContentPane();
		c.add(j);
		c.setBackground(Color.white);
		jf.setVisible(true);
		jf.setSize(400,300);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

}

在这里插入图片描述
绝对布局

package com.jframe_1;
import javax.swing.*;
import java.awt.*;
public class AbsolutePosition extends JFrame{
	
	public AbsolutePosition() {
		setTitle("本窗体使用绝对布局");
		getContentPane().setLayout(null);//使窗体取消布局管理器设置
		setBounds(0, 0, 400, 300);
		Container c =  getContentPane();
		JButton b1 = new JButton("按钮_1");
		JButton b2 = new JButton("按钮_2");
		b1.setBounds(30, 25, 80, 30);
		b2.setBounds(200, 100, 100, 20);
		c.add(b1);
		c.add(b2);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new AbsolutePosition();
	}

}

在这里插入图片描述
流布局管理器

package com.jframe_1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FlowLayoutPosition extends JFrame{

	public FlowLayoutPosition() {
		setTitle("本窗体使用流布局管理器");
		Container c = getContentPane();
		setLayout(new FlowLayout(2,10,10));//0:向左对齐,2:向右对齐;水平间隔;垂直间隔。
		for(int i = 0; i < 10; i++) {
			c.add(new JButton("button" + i));
		}
		JButton jb = new JButton();
		jb.setText("jb");
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "弹出对话框");
			}
		});
		c.add(jb);
		setSize(300, 200);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new FlowLayoutPosition();
	}

}

在这里插入图片描述
单选按钮

package com.jframe_1;

import java.awt.*;

import javax.swing.*;

public class JRadioBtn extends JFrame{

	public JRadioBtn() {
		setTitle("单选按钮");
		setLayout(null);
		Container c = getContentPane();
		setBounds(0, 0, 400, 300);
		JRadioButton b1 = new JRadioButton("B1");
		JRadioButton b2 = new JRadioButton("B2");
		JRadioButton b3 = new JRadioButton("B3");
		b1.setBounds(50, 50, 40, 30);
		b2.setBounds(100, 50, 40, 30);
		b3.setBounds(150, 50, 40, 30);
		ButtonGroup group = new ButtonGroup();
		group.add(b1);
		group.add(b2);
		group.add(b3);
		c.add(b2);
		c.add(b3);
		c.add(b1);
		
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new JRadioBtn();
	}

}

在这里插入图片描述复选框

package com.jframe_1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class CheckBoxTest extends JFrame{

	public CheckBoxTest() {
		setTitle("复选框");
		Container c = getContentPane();
		setSize(500,500);
		c.setLayout(new BorderLayout());
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		c.add(panel1,BorderLayout.NORTH);
		JTextArea jt = new JTextArea("文本域\n",10,20);
		jt.setLineWrap(true);//文本域自动换行
		final JScrollPane scrollpane = new JScrollPane(jt);
		panel1.add(scrollpane);
		c.add(panel2,BorderLayout.SOUTH);
		JCheckBox jc1 = new JCheckBox("1");
		JCheckBox jc2 = new JCheckBox("2");
		JCheckBox jc3 = new JCheckBox("3");
		panel2.add(jc1);
		panel2.add(jc2);
		panel2.add(jc3);
		jc1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jt.append("复选框1被选中");
			}
		});
	
		jc2.addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jt.append("复选框2被选中");
			}
		});
		jc3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jt.append("复选框3被选中");
			}
		});
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new CheckBoxTest();
	}

}

在这里插入图片描述下拉列表框

package com.jframe_1;
import java.awt.*;

import javax.swing.*;

public class JComboBoxTest extends JFrame {

	public JComboBoxTest() {
		JComboBox<String> jc = new JComboBox();
		JLabel ji = new JLabel("请选择证件:");
		setTitle("下拉列表框");
		setSize(500,400);
		jc.addItem("身份证");
		jc.addItem("学生证");
		jc.addItem("工作证");
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		c.add(ji);
		c.add(jc);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new JComboBoxTest();
	}

}

在这里插入图片描述列表框组件、文本框组件、密码框

package com.jframe_1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class JlistTest extends JFrame {

	public JlistTest() {
		setTitle("列表框组件、文本框组件、密码框");
		setBounds(0, 0, 500, 300);
		Container c = getContentPane();
		c.setLayout(null);
		//列表框
		String[] contents = {"列表1","列表2","列表3","列表4","列表5","列表6","列表7","列表8"};
 		JList<String> jl = new JList(contents);
		JScrollPane js = new JScrollPane(jl);
		js.setBounds(10, 10, 100, 100);
		c.add(js);
		//文本框
		JTextField jtf = new JTextField("文本框",20);
		JButton jb = new JButton("清除");
		jtf.setBounds(200, 20, 200, 20);
		jb.setBounds(250, 50, 80, 20);
		c.add(jb);
		c.add(jtf);
		jtf.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtf.setText("触发事件");
			}
		});
		jb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtf.setText("");
				jtf.requestFocus();
			}
		});
		
		//密码框
		JPasswordField jp = new JPasswordField(20);
		jp.setEchoChar('*');
		jp.setBounds(200, 100, 200, 20);
		c.add(jp);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new JlistTest();
	}

}

在这里插入图片描述边界布局管理器
设置容器为边界布局管理器:setLayout(new BorderLayout());
添加组件时的位置:BorderLayout.NORTH/SOUTH/EAST/WEST/CENTER 上下右左中
添加 列 :container.add(BorderLayout.NORTH,button);
网格布局管理器
设置容器为网格布局管理器: setLayout(new GridLayout(网格行数,网格列数,网格之间水平间距,垂直间距));
JPanel面板
也是一种容器,继承自java.awt.Container类,可在JPanel中设置布局管理器,在添加至容器中。
JScrollPane面板
带滚动条的面板,也是容器,只能放置一个组件,可见多个组件放置到JPanel中,在将JPanel添加到JScrollPane中,在将面板添加到容器中。
常用的Swing组件
JButton 按钮
JCheckBix 复选框
JComBox 下拉列表框
JFrame 框架类
JDialog 对话框
JLabel 标签
JRadioButton 单选按钮
JList 列表
JTextField 文本框
JPasswordField 密码框
JTextArea 文本区域
JOptionPane 对话框

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在 GameWin 类中添加一个 JPanel,来实现登录注册面板的功能。具体实现步骤如下: 1. 在 GameWin 类中添加一个 JPanel 变量,用于存放登录注册面板。 2. 在 launch 方法中,初始化登录注册面板,设置其大小和位置,并添加到窗口中。 3. 在登录注册面板中添加必要的组件,如文本框、标签和按钮等,用于实现登录和注册功能。 4. 根据需要,可以在登录注册面板中添加监听器,用于响应用户的操作。 下面是修改后的代码示例: ```java package xjdz; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class GameWin extends JFrame{ private JPanel loginPanel; // 登录注册面板 public void launch(){ this.setVisible(true); this.setSize(999, 666); this.setLocationRelativeTo(null); this.setTitle("oqcw星际大战仿飞机大战小游戏"); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 初始化登录注册面板 loginPanel = new JPanel(); loginPanel.setLayout(null); // 使用绝对布局 loginPanel.setBounds(300, 200, 400, 200); // 设置大小和位置 this.getContentPane().add(loginPanel); // 添加到窗口中 // 在登录注册面板中添加组件 JLabel usernameLabel = new JLabel("用户名:"); usernameLabel.setBounds(50, 50, 60, 30); loginPanel.add(usernameLabel); JTextField usernameField = new JTextField(); usernameField.setBounds(120, 50, 200, 30); loginPanel.add(usernameField); JLabel passwordLabel = new JLabel("密码:"); passwordLabel.setBounds(50, 90, 60, 30); loginPanel.add(passwordLabel); JTextField passwordField = new JTextField(); passwordField.setBounds(120, 90, 200, 30); loginPanel.add(passwordField); JButton loginButton = new JButton("登录"); loginButton.setBounds(100, 130, 80, 30); loginPanel.add(loginButton); JButton registerButton = new JButton("注册"); registerButton.setBounds(220, 130, 80, 30); loginPanel.add(registerButton); } public static void main(String[] args) { GameWin gameWin = new GameWin(); gameWin.launch(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值