package day09;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame=new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600,400);
//设置窗体大小
jFrame.setSize(400,200);
//定义按钮组件
JButton jButton=new JButton("显示");
//定义文本显示组件
JLabel jLabel=new JLabel();
//定义单行文本输入框
JTextField jTextField=new JTextField(10);
//定义JPanel容器
JPanel jPanel=new JPanel();
//定义字体对象
Font font=new Font("Serief",Font.BOLD,20);
//给文本显示组件设置字体
jLabel.setFont(font);
//给文本显示组件设置默认字体
jLabel.setText("等待用户操作");
//给按钮设置监听动作事件
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取文本输入框的内容
String result=jTextField.getText();
System.out.println("输入内容为:"+result);
//将输入内容赋值给文本显示组件
jLabel.setText(result);
}
});
//将定义好的三个组件放入容器
jPanel.add(jLabel);
jPanel.add(jTextField);
jPanel.add(jButton);
//将jpanel放入窗体
jFrame.add(jPanel);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class JCheckBoxTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame=new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600,400);
//设置窗体大小
jFrame.setSize(400,200);
//定义复选框组件
JComboBox jc1=new JComboBox();
JComboBox jc2=new JComboBox();
//设置复选框的状态监听
jc1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("jc1的状态:"+e.getStateChange());
}
});
jc2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("jc2的状态:"+e.getStateChange());
}
});
//定义JPanel对象
JPanel jPanel=new JPanel();
//将组件放入容器中
jPanel.add(jc1);
jPanel.add(jc2);
//将容器放入窗体中
jFrame.add(jPanel);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class JMenuBarTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame = new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600, 400);
//设置窗体大小
jFrame.setSize(800, 500);
//定义菜单对象
JMenu jMenu=new JMenu("文件");
//菜单项
JMenuItem jMenuItem=new JMenuItem("新建");
JMenuItem jMenuItem1=new JMenuItem("打开");
//菜单项放入菜单
jMenu.add(jMenuItem);
jMenu.add(jMenuItem1);
//深菜单项事件
jMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("菜单项:"+e.getActionCommand());
}
});
JMenu jMenu1=new JMenu("帮助");
//创建JMenuBar对象
JMenuBar jMenuBar=new JMenuBar();
//放入jMenuBar中
jMenuBar.add(jMenu);
jMenuBar.add(jMenu1);
//将jMenuBar对象设置到窗体上
jFrame.setJMenuBar(jMenuBar);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.*;
public class Jpaneltest {
public static void main(String[] args) {
JFrame jFrame=new JFrame("中北大学");
JPanel jPanel=new JPanel();
jPanel.add(new JLabel("标签-A"));
jPanel.add(new JLabel("标签-B"));
jPanel.add(new JLabel("标签-C"));
jPanel.add(new JButton("标签-X"));
jPanel.add(new JButton("标签-Y"));
jPanel.add(new JButton("标签-Z"));
jFrame.add(jPanel);
jFrame.pack();
jFrame.setSize(600,400);
jFrame.setLocation(500,400);
jFrame.setBackground(Color.BLACK);
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class JRadioButtonTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame=new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600,400);
//设置窗体大小
jFrame.setSize(400,200);
//定义单选按钮组件
JRadioButton jb1=new JRadioButton("男");
JRadioButton jb2=new JRadioButton("女");
//设置单选按钮的状态监听
jb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("jb1的状态:"+e.getStateChange());
}
});
jb2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("jb2的状态:"+e.getStateChange());
}
});
//定义JPanel对象
JPanel jPanel=new JPanel();
//将组件放入容器中
jPanel.add(jb1);
jPanel.add(jb2);
//将容器放入窗体中
jFrame.add(jPanel);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import day03.Vehicle;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class JTableTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame = new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600, 400);
//设置窗体大小
jFrame.setSize(800, 500);
//定义列名称
Vector<String> titles=new Vector<>();
titles.add("姓名");
titles.add("年龄");
titles.add("性别");
titles.add("生日");
String[] title={"姓名","年龄","性别","生日"};
//行数据
//Vector<String> vector=new Vector();
//vector.add("{\"张三\",30,\"男\",\"2004-1-2\"}");
Object[][] date={{"张三",22,"男","1003-1-3"},{"李四",21,"女","1002-4-3"}};
//创建表格
JTable jTable=new JTable(date,title);
//容器
JScrollPane jScrollPane=new JScrollPane(jTable);
jFrame.add(jScrollPane);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
public class JTextCompoentTest {
public static void main(String[] args) {
JFrame jFrame=new JFrame("中北大学");
jFrame.setLayout(null);
/*单行
JTextField jTextField=new JTextField();
jTextField.setBounds(100,150,400,40);
jFrame.add(jTextField);
*/
//多行
JTextArea jTextArea=new JTextArea();
jTextArea.setBounds(100,150,400,80);
jFrame.add(jTextArea);
jFrame.setLocation(400,500);
jFrame.setSize(600,400);
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class keyListenerTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame=new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600,400);
//设置窗体大小
jFrame.setSize(400,200);
//定义多行文本输入组件
JTextArea text=new JTextArea();
//给JTextArea对象添加键盘监听事件
text.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {//输入某个键时
//text.append("输入的内容是:"+e.getKeyChar()+'\n');
}
@Override
public void keyPressed(KeyEvent e) {//键盘按下时
//给多行文本输入框设置内容
text.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键盘按下了");
}
@Override
public void keyReleased(KeyEvent e) {//键盘松开时
text.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键盘松开了");
}
});
JScrollPane scr=new JScrollPane(text);//加入滚动条
scr.setBounds(5,5,300,200);
jFrame.add(scr);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseLisnerTest {
public static void main(String[] args) {
//创建窗体对象
JFrame jFrame=new JFrame("中北大学");
//设置窗体的坐标
jFrame.setLocation(600,400);
//设置窗体大小
jFrame.setSize(400,200);
//定义多行文本输入组件
JTextArea text=new JTextArea();
//给JTextArea对象添加鼠标监听事件
text.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {//单击
text.append("鼠标单击"+e.getButton());
}
@Override
public void mousePressed(MouseEvent e) {//按下
text.append("鼠标-->"+e.getButton()+"按下了");
}
@Override
public void mouseReleased(MouseEvent e) {//松开
text.append("鼠标-->"+e.getButton()+"松开了");
}
@Override
public void mouseEntered(MouseEvent e) {//进入组件
text.append("进入组件");
}
@Override
public void mouseExited(MouseEvent e) {//离开组件
text.append("离开组件");
}
});
JScrollPane scr=new JScrollPane(text);//加入滚动条
scr.setBounds(5,5,300,200);
jFrame.add(scr);
//显示当前窗体
jFrame.setVisible(true);
}
}
package day09;
import javax.swing.*;
import java.awt.*;
public class MyTest {
public static void main(String[] args) {
//1.实例化容器
JFrame jFrame=new JFrame("中北大学");
//Dimension dimension=new Dimension(430,280);//设置宽高
//jFrame.setSize(dimension);
//Point point=new Point(500,400);//设置显示坐标
//jFrame.setLocation(point);
jFrame.setSize(500,400);//大小
jFrame.setLocation(300,200);//显示位置
jFrame.setBackground(Color.BLACK);//颜色
jFrame.setVisible(true);//让窗体显示
/*
(1)
jFrame.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
JButton but=null;
for (int i=0;i<12;i++){
but=new JButton("按钮"+i);
jFrame.add(but);
}
(2)
jFrame.setLayout(new BorderLayout(3,3));
jFrame.add(new JButton("东"),BorderLayout.EAST);
jFrame.add(new JButton("西"),BorderLayout.WEST);
jFrame.add(new JButton("南"),BorderLayout.SOUTH);
jFrame.add(new JButton("北"),BorderLayout.NORTH);
jFrame.add(new JButton("中"),BorderLayout.CENTER);
jFrame.pack();
*/
//2.实例化组件
JLabel jLabel=new JLabel("用户名",JLabel.CENTER);
//设置字体样式
Font font=new Font("Serief",Font.BOLD,60);
jLabel.setFont(font);
jLabel.setForeground(Color.PINK);
JButton jButton=new JButton("点击我");
jButton.setBounds(150,75,100,50);
jButton.setBackground(Color.blue);
//3.将组件放在容器中
jFrame.add(jLabel);
jFrame.add(jButton);
}
}
package day09;
import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowListenerTest {
public static void main(String[] args) {
JFrame jFrame=new JFrame("中北大学");
jFrame.setSize(600,400);
jFrame.setLocation(600,400);
jFrame.addWindowListener(new WindowListener() {
@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("窗口关闭了");
}
@Override
public void windowIconified(WindowEvent e) {//窗口变为最小化时触发
System.out.println("窗口最小化了");
}
@Override
public void windowDeiconified(WindowEvent e) {//窗口从最小化恢复到正常状态时触发
System.out.println("窗口恢复了");
}
@Override
public void windowActivated(WindowEvent e) {//窗口变为活动窗口时触发
System.out.println("窗口变为活动窗口了");
}
@Override
public void windowDeactivated(WindowEvent e) {//窗口变为不活动窗口时触发
System.out.println("窗口变为不活动窗口了");
}
});
jFrame.setVisible(true);
}
}