文章目录
- 一、组件和容器
- 1、JFrame
- 2、面板JPanel
- 3、布局管理器
- 流式布局(从左到右)
- 东南西北中
- 网格布局
- 4、事件监听
- 输入框事件监听
- 键盘监听
- 5、JDialog弹窗
- 6、Icon、ImageIcon标签
- Icon
- ImageIcon
- 7、按钮
- 单选按钮
- 复选按钮
- 8、列表
- 下拉框
- 列表框
- 9、文本框
- 密码框
- 文本域
- 10、菜单
- 关系图
- 案例练习
- 二、连接数据库
- 总结
大家好,我是秋意临。
一、组件和容器
1、JFrame
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
public class TestFrame {
public static void main(String[]args){
JFrame frame = new JFrame("第一个GUI窗口");
frame.setSize(400,400); //设置窗口大小
frame.setLocation(200,200); //弹出的初始位置
frame.setBackground(new Color(49, 77, 125)); //设置背景颜色
frame.setResizable(false); //设置大小固定(不可调整大小)
frame.setVisible(true); //设置窗口可见性
}
}
2、面板JPanel
注意: //JFrame设置背景色的区域一般是看不到的。一般看到的window背景区域是JFrame里的ContentPane,所以在ContentPane设置。
java frame.setBackground(new Color(52, 111, 121)); 改为 frame.getContentPane().setBackground(new Color(52, 111, 121));
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
JFrame frame = new JFrame(); //创建窗体
JPanel panel = new JPanel(); //创建面板
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗体可关闭
frame.setLayout(null); //定义布局为 绝对布局
frame.setBounds(200,200,300,300); //设置窗体x,y的坐标(弹出位置)和宽度、高度
// frame.setBackground(new Color(52, 111, 121)); 这样设置没有效果
frame.getContentPane().setBackground(new Color(52, 111, 121)); //设置窗体背景颜色
frame.setResizable(true);
frame.setVisible(true);
panel.setBackground(new Color(56, 127, 96)); //设置面板背景颜色
panel.setBounds(50,50,180,150);
frame.add(panel); //将面板放入到窗体中
}
}
3、布局管理器
流式布局(从左到右)
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
JFrame frame = new JFrame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //默认为从左到右,这里设置靠右边right
frame.setSize(300,300);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东南西北中
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("TestBorderLayout");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
//设置东南西北中布局
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
}
}
网格布局
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("TestBorderLayout");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
//设置网格布局,3行2列
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setSize(300,300);
frame.setVisible(true);
}
}
4、事件监听
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
JButton button = new JButton("button1");
jFrame.setBounds(10,10,300,300);
jFrame.setVisible(true);
jFrame.add(button);
button.addActionListener(new MyActionEvent());
}
}
class MyActionEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("你点击了按钮!!");
}
}
点击按钮后,就会触发System.out.println("你点击了按钮!!");
这条命令
输入框事件监听
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setBounds(10,10,300,300);
jFrame.setVisible(true);
JTextField jTextField = new JTextField("test:");
jFrame.getContentPane().add(jTextField);
jTextField.addActionListener(new MyActionEvent());
}
}
class MyActionEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField jTextField = (JTextField)e.getSource(); //获得一些资源、返回一个对象
System.out.println(jTextField.getText()); //获得输入框中的内容
jTextField.setText(""); //设置输入框为 null
}
}
文本框输入文字回车后提交到后台
键盘监听
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends JFrame{
public KeyFrame(){
setBounds(10,10,300,300);
setVisible(true);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode(); //获得键盘按下的哪一个键
System.out.println(keycode); //打印目前按下的键(会输出一个静态属性值)
if (keycode == KeyEvent.VK_SPACE){ //键盘上每个健对应一个静态属性值:VK_XX
System.out.println("你按下了空格");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
}
5、JDialog弹窗
package www.qyl.lesson01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDialog {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(10,10,300,300);
JButton button = new JButton("button1");
jFrame.getContentPane().add(button);
button.addActionListener(new MyAction1());
}
}
class MyAction1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
}
class MyDialogDemo {
public MyDialogDemo(){
JDialog jDialog = new JDialog();
jDialog.setVisible(true);
jDialog.setBounds(100,100,300,300);
}
}
点击按钮弹出JDialog窗口
6、Icon、ImageIcon标签
Icon
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width,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("icontest",iconDemo,SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setBounds(50,50,100,100);
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); //g:是画笔,g.fillOval画出一个图形
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
ImageIcon
图片也可以放在按钮上,称为图片按钮
package www.qyl.lesson02;
import com.sun.jndi.toolkit.url.UrlUtil;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("hhy.png"); //获取本类路径下的图片路径,获取图片路径
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(20,20,300,300);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
7、按钮
单选按钮
package www.qyl.lesson02;
import javax.swing.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,300,300);
jf.setVisible(true);
JPanel jPanel = new JPanel();
jRadioButtonJiZhuPass.isSelected()
JRadioButton jradio1 = new JRadioButton("男");
JRadioButton jradio2 = new JRadioButton("女");
//单选只能选择一个所以需要分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(jradio1);
group.add(jradio2);
//判断是否选中单选框
System.out.println(jradio1.isSelected()); //返回Boolean类型
jPanel.add(jradio1);
jPanel.add(jradio2);
jf.add(jPanel);
}
}
复选按钮
package www.qyl.lesson02;
import javax.swing.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,500,300);
jf.setVisible(true);
JPanel jPanel = new JPanel();
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
jPanel.add(jCheckBox1);
jPanel.add(jCheckBox2);
jPanel.add(jCheckBox3);
jf.add(jPanel);
}
}
8、列表
下拉框
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,500,300);
jf.setVisible(true);
JPanel jPanel = new JPanel();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("1");
jComboBox.addItem("2");
jComboBox.addItem("3");
System.out.println(jComboBox.getSelectedItem().toString().trim()); //获取下拉框选中的值
jPanel.add(jComboBox);
jf.add(jPanel);
}
}
列表框
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,500,300);
jf.setVisible(true);
JPanel jPanel = new JPanel();
jPanel.setBounds(10,10,300,300);
String[] con = {"1","2","3"};
JList jList = new JList(con);
jList.setBounds(10,10,100,100);
jPanel.add(jList);
jf.add(jPanel);
}
}
9、文本框
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,500,300);
jf.setLayout(null);
jf.setVisible(true);
JPanel jPanel = new JPanel();
jPanel.setBounds(10,10,300,300);
JTextField jTextField = new JTextField("hello qyl");
JTextField jTextField2 = new JTextField("hello 秋意临",20);
jTextField.setBounds(10,10,60,30);
jTextField2.setBounds(10,50,60,30);
jPanel.add(jTextField);
jPanel.add(jTextField2);
jf.add(jPanel);
}
}
密码框
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,1000,300);
jf.setLayout(null);
jf.setVisible(true);
JPanel jPanel = new JPanel();
jPanel.setBounds(10,10,500,300);
JPasswordField jPasswordField1 = new JPasswordField();
jPasswordField1.setBounds(10,10,200,100);
jPanel.add(jPasswordField1);
jf.add(jPanel);
}
}
文本域
package www.qyl.lesson02;
import javax.swing.*;
import java.awt.*;
public class TestradioBttton {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setBounds(10,10,1000,300);
jf.setLayout(null);
jf.setVisible(true);
JPanel jPanel = new JPanel();
jPanel.setBounds(10,10,500,300);
JTextArea jTextArea = new JTextArea(30,50);
jTextArea.setText("hello秋意临:");
jTextArea.setBounds(10,10,200,100);
jPanel.add(jTextArea);
jf.add(jPanel);
}
}
10、菜单
Java菜单提供JmenuBar,Jmenu,Jmenultem,JcheckboxMenultem类来创建和管理。
JcheckboxMenultem:复选框菜单项
关系图
案例练习
package www.qyl.lesson03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestJMenuBar{
public static void main(String[] args) {
JFrame jf = new JFrame("菜单");
Container container = jf.getContentPane();
container.setLayout(null);
jf.setBounds(10,10,300,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
JMenuBar menuBar = new JMenuBar(); //创建菜单栏
menuBar.setBounds(0,0,300,30);
container.add(menuBar); //菜单栏放入到JFrame顶层容器
JMenu m1 = new JMenu("系统(n)"); // 设置一级菜单
JMenu m2 = new JMenu("简介");
JMenu m3 = new JMenu("工具");
JMenu m4 = new JMenu("查询");
JMenu m5 = new JMenu("关闭");
m1.setMnemonic('n'); //设置快捷键
menuBar.add(m1); //将一级菜单放入菜单栏中
menuBar.add(m2);
menuBar.add(m3);
menuBar.add(m4);
menuBar.add(m5);
JMenu jmi1 = new JMenu("开始导航"); // 设置二级菜单项
JMenu jmi2 = new JMenu("刷新系统");
JMenu jmi3 = new JMenu("退出系统");
m1.add(jmi1); //将二级菜单项放入菜单中
m1.addSeparator(); //分隔线
m1.add(jmi2);
m1.add(jmi3);
JMenuItem jmi1_1 = new JMenuItem("硬件"); // 设置三级菜单项
JMenuItem jmi1_2 = new JMenuItem("软件");
JMenuItem jmi1_3 = new JMenuItem("杀毒");
JMenuItem jmi1_4 = new JMenuItem("确认");
JMenuItem jmi1_5 = new JMenuItem("取消");
jmi1.add(jmi1_1); //将三级菜单项放入菜单中
jmi1.addSeparator(); //分隔线
jmi1.add(jmi1_2);
jmi1.add(jmi1_3);
JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem("复选框菜单项"); //设置三级复选框
jmi1.add(jCheckBoxMenuItem1);
jmi3.add(jmi1_4);
jmi3.add(jmi1_5);
jmi1_4.addActionListener(new MyListener()); //点击确认退出程序
}
}
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
二、连接数据库
1、下载MySQL的驱动.jar包
下载地址:https://dev.mysql.com/downloads/connector/j/
2、IDEA导入连接MySQL的驱动.jar包
3、连接数据库
package www.qyl.lesson03;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
public class TestMysqlConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/db_student";
String user = "root";
String pass = "000000";
try {
// Class.forName("com.mysql.jdbc.Driver"); //旧驱动程序类,不推荐
Class.forName("com.mysql.cj.jdbc.Driver"); //驱动程序通过sql自动注册,驱动程序类
Connection conn = DriverManager.getConnection(url,user,pass); //连接数据库
Statement stmt = conn.createStatement(); //用于将sql语句发送到数据库中
String sql = "select * from t_major";
ResultSet rs = stmt.executeQuery(sql); //执行sql语句
System.out.println(rs.next()); //结果为true
while (rs.next()){
// String name = rs.getString("major_name"); //通过字段检索
// System.out.println(name); //输出查询字段
System.out.println(rs.getInt(1) + "\t" + rs.getString(2)); //通过字段的索引检索
}
conn.close();
rs.close();
stmt.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
欢迎加入云社区
总结
我是秋意临,欢迎大家一键三连、加入云社区
我们下期再见(⊙o⊙)!!!