窗口监听
package Lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,200,200);
setBackground(Color.CYAN);
setVisible(true);
addWindowListener(new MyWindowListener());
}
//内部类
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//通过按钮,隐藏窗口
System.exit(0);//正常退出,非正常退出是1
}
}
}
用匿名内部类进行优化:
package Lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,200,200);
setBackground(Color.CYAN);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类,更简介
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
}
}
);
}
}
package Lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,200,200);
setBackground(Color.CYAN);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类,更简介
new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口已经关闭");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source=(WindowFrame)e.getSource();
source.setTitle("被激活了");
System.out.println("激活");
}
}
);
}
}
键盘监听
package Lesson3;
import javafx.animation.KeyFrame;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个
int keyCode=e.getKeyCode();
System.out.println(keyCode);
//根据按下的不同操作,产生不同的结果
if(keyCode==KeyEvent.VK_UP){
System.out.println("按下了上键");
}
}
});
}
}
Swing
窗口、面板
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
JFrame jframe = new JFrame("这是一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,200,200);
//jframe.setBackground(Color.orange);//不起作用
//设置文字
JLabel label = new JLabel("欢迎",SwingConstants.CENTER);//设置文本并居中
jframe.add(label);
//获得一个容器
Container container=jframe.getContentPane();
container.setBackground(Color.PINK);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
弹窗
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container=this.getContentPane();
container.setBackground(Color.PINK);
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击这个按钮,弹出一个弹窗 监听器
button.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
//将按钮加入容器
container.add(button);
}
public static void main(String[] args) {
//启动
new DialogDemo();
}
}
//弹窗也是一个窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);弹窗默认就有关闭事件
Container container=this.getContentPane();
//container.setBackground(Color.pink);没起作用
//绝对布局
//container.setLayout(null);注释掉,不然不显示文字
container.add(new JLabel("啦啦啦"));//这里需要用JLable,设置为JLable,显示文字
}
}
标签
图标:
package Lesson4;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}//无参构造
public IconDemo(int width,int height){//有参构造
this.width=width;
this.height=height;
}
//初始化
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("放置图标",iconDemo,SwingConstants.CENTER);
Container container=this.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);//画一个圆
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片:
package Lesson4;
import javax.swing.*;
import java.net.URL;
import java.awt.*;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址 图片和类在同级目录下
URL url = ImageIconDemo.class.getResource("1f20816e15694cf3b56262c1f87e2a14.png");//获取这个类同级下面的资源
//url是具体的地址
JLabel label = new JLabel("ImageIcon");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);//居中显示
Container container=getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container=this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//10是间距
JPanel jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(2,1));
JPanel jPanel3 = new JPanel(new GridLayout(2,2));
JPanel jPanel4 = new JPanel(new GridLayout(2,3));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("2"));
jPanel1.add(new JButton("3"));
jPanel2.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("1"));
jPanel3.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("4"));
jPanel4.add(new JButton("1"));
jPanel4.add(new JButton("2"));
jPanel4.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("5"));
jPanel4.add(new JButton("6"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel:边框滚动条
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 30);
textArea.setText("啦啦啦");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);//将面板放在容器中
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
图片按钮:
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo(){
//用JFrame就需要构造一个容器,用awt就不需要
Container container = this.getContentPane();
//放图片上去
URL url = JButtonDemo.class.getResource("1f20816e15694cf3b56262c1f87e2a14.png");
Icon icon = new ImageIcon(url);//将图片变成图标
//把图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("提示");//按钮的提示文字
container.add(jButton);
setVisible(true);
setBounds(200,200,500,700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
单选框:
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo2 extends JFrame {
public JButtonDemo2(){
//用JFrame就需要构造一个容器,用awt就不需要
Container container = this.getContentPane();
//放图片上去
URL url = JButtonDemo2.class.getResource("1f20816e15694cf3b56262c1f87e2a14.png");
Icon icon = new ImageIcon(url);//将图片变成图标
//单选框 不能被同时选择
JRadioButton radioButton1 = new JRadioButton("01");
JRadioButton radioButton2 = new JRadioButton("02");
JRadioButton radioButton3 = new JRadioButton("03");
//分组,一个组中只能选择一个,实现单选
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
setVisible(true);
setBounds(200,200,500,700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo2();
}
}
复选框(多选框):
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo3 extends JFrame {
public JButtonDemo3(){
//用JFrame就需要构造一个容器,用awt就不需要
Container container = this.getContentPane();
//放图片上去
URL url = JButtonDemo3.class.getResource("1f20816e15694cf3b56262c1f87e2a14.png");
Icon icon = new ImageIcon(url);//将图片变成图标
//多选框
JCheckBox checkBox1 = new JCheckBox("1");
JCheckBox checkBox2 = new JCheckBox("2");
JCheckBox checkBox3 = new JCheckBox("3");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.CENTER);
container.add(checkBox3,BorderLayout.SOUTH);
setVisible(true);
setBounds(200,200,500,700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo3();
}
}
列表
下拉框
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo(){
Container container = this.getContentPane();
JComboBox status = new JComboBox<>();
status.addItem(null);
status.addItem("蓝光");
status.addItem("高清");
status.addItem("标清");
container.add(status);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
}
列表框
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo2 extends JFrame {
public TestComboboxDemo2(){
Container container = this.getContentPane();
//列表框
//生成列表内容
String[] contents={"1","2","3"};
//列表中需要放入内容
JList<Object> list = new JList<>(contents);
container.add(list);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo2();
}
}
动态添加内容:
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo2 extends JFrame {
public TestComboboxDemo2(){
Container container = this.getContentPane();
//列表框
//生成列表内容
//String[] contents={"1","2","3"};\
//动态添加内容
Vector<Object> contents = new Vector<>();
//列表中需要放入内容
JList<Object> list = new JList<>(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
contents.add("zhaoliu");
container.add(list);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo2();
}
}
文本框
文本框:
package Lesson4;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo extends JFrame {
public TestTextDemo(){
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo();
}
}
密码框:
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo2 extends JFrame {
public TestTextDemo2(){
Container container = this.getContentPane();
//密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');//设置密码为*
container.add(passwordField);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo2();
}
}
文本域:
package Lesson4;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo3 extends JFrame {
public TestTextDemo3(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,30);
textArea.setText("啦啦啦哈哈哈哈哈");
container.add(textArea);
this.setVisible(true);
this.setBackground(Color.orange);
this.setBounds(100,100,400,500);//x,y是框在屏幕的定位,与框本身的大小没有关系
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo3();
}
}
2万+

被折叠的 条评论
为什么被折叠?



