在PC端点击图片,鼠标右键可把图片素材另存到桌面使用,手机端可长按图片保存到本地相册,夏欢和认为有用的话就点个赞,三连就更满足我的期待了


JPanel切换案例
package swing;
public class mains {
public static void main(String[] args) {
new swingJpanelQieHuan();
}
}

package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class swingJpanelQieHuan extends JFrame{
public static JPanel jpRed,jpPink,jpBlueRightBottom1,
jpGreenRightBottom2;
public static JButton anNiu1,anNiu2;
JLabel JLabel1;
public static JLabel JLabel2;
public swingJpanelQieHuan(){
this.setLayout(null);
this.setSize(700,700);
this.setLocationRelativeTo(null);
jpRed=new JPanel();
jpPink=new JPanel();
jpBlueRightBottom1=new JPanel();
jpGreenRightBottom2=new JPanel();
jpRed.setLayout(null);
anNiu1=new JButton("点赞界面");
anNiu2=new JButton("三连关注界面");
anNiu1.setBounds(150,30,120,30);
anNiu2.setBounds(300,30,120,30);
anNiu1.addActionListener(new swingJpanelShiJian(this));
anNiu2.addActionListener(new swingJpanelShiJian(this));
jpRed.add(anNiu1);jpRed.add(anNiu2);
jpRed.setBorder(BorderFactory.createLineBorder(Color.red));
jpPink.setBorder(BorderFactory.createLineBorder(Color.pink));
jpBlueRightBottom1.setBorder
(BorderFactory.createLineBorder(Color.blue));
jpGreenRightBottom2.setBorder
(BorderFactory.createLineBorder(Color.green));
jpRed.setBounds(10,10,600,150);
jpPink.setBounds(10,170,200,450);
jpBlueRightBottom1.setBounds(220, 170, 380, 450);
jpGreenRightBottom2.setBounds(220, 170, 380, 450);
JLabel1 = new JLabel();
JLabel2=new JLabel();
JLabel1. setIcon(new ImageIcon("img//1.png"));
JLabel2. setIcon(new ImageIcon("img//2.png"));
jpBlueRightBottom1.add(JLabel1);
jpGreenRightBottom2.add(JLabel2);
this.add(jpRed);this.add(jpPink);
this.add(jpGreenRightBottom2);
this.add(jpBlueRightBottom1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class swingJpanelShiJian implements ActionListener{
//jieShou接收
//chuangTi窗体
public static swingJpanelQieHuan jieShou;
public swingJpanelShiJian(swingJpanelQieHuan chuangTi){
jieShou=chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String neiRong=arg0.getActionCommand();
if(neiRong.equals("点赞界面")){
jieShou.jpBlueRightBottom1.setVisible(true);
jieShou.jpGreenRightBottom2.setVisible(false);
}else if(neiRong.equals("三连关注界面")){
jieShou.jpBlueRightBottom1.setVisible(false);
jieShou.jpGreenRightBottom2.setVisible(true);
}
}
}




JTree树形控件点击内容弹出新的窗体
package swing;
public class mains {
public static void main(String[] args) {
new swingJpanelQieHuan();
}
}


package swing;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
public class newDengLu extends JFrame{
public static JLabel lb1,lb2,lb3,lb4=null;
public static JTextField txt1=null;
public static JPasswordField pwd=null;
public static JComboBox com=null;
public static JButton btn1,btn2=null;
public newDengLu(){
this.setTitle("诗书画唱登录页面");
this.setLayout(null);
this.setSize(500,400);
this.setLocationRelativeTo(null);
lb1=new JLabel("用户名");
lb2=new JLabel("用户密码");
lb3=new JLabel("用户类型");
lb4=new JLabel("登录窗体");
Font f=new Font("微软雅黑",Font.BOLD,35);
lb4.setFont(f);
lb4.setForeground(Color.red);
lb4.setBounds(160,30,140,40);
lb1.setBounds(100, 100, 70,30);
lb2.setBounds(100,140,70,30);
lb3.setBounds(100,180,70,30);
txt1=new JTextField();
txt1.setBounds(170,100,150,30);
pwd=new JPasswordField();
pwd.setBounds(170,140,150,30);
com=new JComboBox();
com.addItem("会员用户");
com.addItem("普通用户");
com.setBounds(170,180,150,30);
btn1=new JButton("登录");
btn1.setBounds(130,220,70,30);
btn2=new JButton("取消");
btn2.setBounds(240,220,70,30);
this.add(lb1);this.add(lb2);this.add(lb3);
this.add(txt1);this.add(pwd);this.add(com);
this.add(btn1);this.add(btn2);this.add(lb4);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class swingJpanelQieHuan extends JFrame{
public static JPanel jpRed,jpPinkLeft,jpBlueRightBottom1,
jpGreenRightBottom2;
public static JTree JTree1,JTree2;
public static JButton anNiu1,anNiu2;
public static JLabel JLabel1,JLabel2;
public swingJpanelQieHuan(){
this.setLayout(null);
this.setSize(700,700);
this.setLocationRelativeTo(null);
jpRed=new JPanel();
jpPinkLeft=new JPanel();
jpBlueRightBottom1=new JPanel();
jpGreenRightBottom2=new JPanel();
jpRed.setLayout(null);
anNiu1=new JButton("点赞界面");
anNiu2=new JButton("三连关注界面");
anNiu1.setBounds(150,30,120,30);
anNiu2.setBounds(300,30,120,30);
anNiu1.addActionListener(new swingJpanelShiJian(this));
anNiu2.addActionListener(new swingJpanelShiJian(this));
jpRed.add(anNiu1);jpRed.add(anNiu2);
jpRed.setBorder(BorderFactory.createLineBorder(Color.red));
jpPinkLeft.setBorder(BorderFactory.createLineBorder(Color.pink));
jpBlueRightBottom1.setBorder
(BorderFactory.createLineBorder(Color.blue));
jpGreenRightBottom2.setBorder
(BorderFactory.createLineBorder(Color.green));
jpRed.setBounds(10,10,600,150);
jpPinkLeft.setBounds(10,170,200,450);
jpBlueRightBottom1.setBounds(220, 170, 380, 450);
jpGreenRightBottom2.setBounds(220, 170, 380, 450);
JLabel1 = new JLabel();
JLabel2=new JLabel();
JLabel1. setIcon(new ImageIcon("img//1.png"));
JLabel2. setIcon(new ImageIcon("img//2.png"));
jpBlueRightBottom1.add(JLabel1);
jpGreenRightBottom2.add(JLabel2);
DefaultMutableTreeNode dmtn1 = new DefaultMutableTreeNode("图书管理");
DefaultMutableTreeNode dmtn_yonghu = new DefaultMutableTreeNode
("用户管理");
DefaultMutableTreeNode dmtnQieHuan = new DefaultMutableTreeNode
("切换到登录界面");
DefaultMutableTreeNode dmtn_yonghu_insert =
new DefaultMutableTreeNode("增加用户");
DefaultMutableTreeNode dmtn_yonghu_update =
new DefaultMutableTreeNode("修改用户");
DefaultMutableTreeNode dmtn_yonghu_delete =
new DefaultMutableTreeNode("删除用户");
DefaultMutableTreeNode dmtn_yonghu_select =
new DefaultMutableTreeNode("查询用户");
DefaultMutableTreeNode dmtn_jieyue =
new DefaultMutableTreeNode("借阅管理");
DefaultMutableTreeNode dmtn_jieyue_insert =
new DefaultMutableTreeNode("增加借阅信息");
DefaultMutableTreeNode dmtn_jieyue_update =
new DefaultMutableTreeNode("修改借阅信息");
DefaultMutableTreeNode dmtn_jieyue_delete =
new DefaultMutableTreeNode("删除借阅信息");
DefaultMutableTreeNode dmtn_jieyue_select =
new DefaultMutableTreeNode("查询借阅信息");
dmtn_yonghu.add(dmtnQieHuan);
dmtn_yonghu.add(dmtn_yonghu_insert);
dmtn_yonghu.add(dmtn_yonghu_update);
dmtn_yonghu.add(dmtn_yonghu_delete);
dmtn_yonghu.add(dmtn_yonghu_select);
dmtn_jieyue.add(dmtn_jieyue_insert);
dmtn_jieyue.add(dmtn_jieyue_update);
dmtn_jieyue.add(dmtn_jieyue_delete);
dmtn_jieyue.add(dmtn_jieyue_select);
dmtn1.add(dmtn_yonghu);
dmtn1.add(dmtn_jieyue);
JTree1 = new JTree(dmtn1);
JTree1.addTreeSelectionListener(new
swingJpanelShiJian(this));
JTree1.setBackground(Color.white);
jpPinkLeft.setBackground(Color.white);
//JTree1.setBounds(10,170,200,450);在这里是一句没效果的代码
jpPinkLeft.add(JTree1);
this.add(jpRed);this.add(jpPinkLeft);
this.add(jpGreenRightBottom2);
this.add(jpBlueRightBottom1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class swingJpanelShiJian implements ActionListener,
TreeSelectionListener{
//jieShou接收
//chuangTi窗体
public static swingJpanelQieHuan jieShou;
public swingJpanelShiJian(swingJpanelQieHuan chuangTi){
jieShou=chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String neiRong=arg0.getActionCommand();
if(neiRong.equals("点赞界面")){
jieShou.jpBlueRightBottom1.setVisible(true);
jieShou.jpGreenRightBottom2.setVisible(false);
}else if(neiRong.equals("三连关注界面")){
jieShou.jpBlueRightBottom1.setVisible(false);
jieShou.jpGreenRightBottom2.setVisible(true);
}
}
@Override
public void valueChanged(TreeSelectionEvent arg0) {
DefaultMutableTreeNode str = (DefaultMutableTreeNode)
jieShou.JTree1
.getLastSelectedPathComponent();
if (str.toString().equals("切换到登录界面")) {
new newDengLu();
} else {
}
}
}



JTable初始化表格
package swing;
public class mains {
public static void main(String[] args) {
new swingBiaoGe();
}
}

package swing;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class swingBiaoGe extends JFrame{
//要声明 : 装载内容的容器,table的控件, 容器的标题, 容器的具体的内容。
public static JTable biaoGe=null;//JTable为表格的控件
//要声明装载内容的容器,如下:
public static DefaultTableModel DTM=null;
//Vector中:
//一个放标题,一个放内容
//>表示只接受集合的类型
Vector biaoTi;
Vector> neiRong;
public swingBiaoGe(){
this.setLayout(null);
this.setSize(600,600);
this.setLocationRelativeTo(null);
//给标题赋值:
biaoTi=new Vector();
biaoTi.add("编号");biaoTi.add("姓名");
biaoTi.add("性别");biaoTi.add("年龄");
//给内容赋值:
neiRong=new Vector>();
for(int i=0;i<5;i++){
Vector v=new Vector();
v.add("编号"+(i+6));v.add("诗书画唱"+(i+6));
v.add("性别"+(i+6));v.add("年龄"+(i+6));
neiRong.add(v);
}
//将内容添加到装载内容的容器中:
DTM=new DefaultTableModel(neiRong,biaoTi);
DTM=new DefaultTableModel(neiRong,biaoTi) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
biaoGe=new JTable(DTM);
//设置滚动条:
JScrollPane jsp=new JScrollPane(biaoGe);
jsp.setBounds(10,10,400,400);
this.add(jsp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}


JTable初始化数据,数据要求链接JDBC获取

create database yonghu
select * from shangpin;
select * from sp_Type;
create table sp_Type(
sp_TypeID int primary key identity(1,1),
sp_TypeName varchar(100) not null
);
insert into sp_Type values('水果');
insert into sp_Type values('零食');
insert into sp_Type values('小吃');
insert into sp_Type values('日常用品');
create table shangpin(
sp_ID int primary key identity(1,1),
sp_Name varchar(100) not null,
sp_Price decimal(10,2) not null,
sp_TypeID int,
sp_Jieshao varchar(300)
);
insert into shangpin values('苹果',12,1,'好吃的苹果');
insert into shangpin values('香蕉',2,1,'好吃的香蕉');
insert into shangpin values('橘子',4,1,'好吃的橘子');
insert into shangpin values('娃哈哈',3,2,'好吃营养好');
insert into shangpin values('牙刷',5,4,'全自动牙刷');




package SwingJdbc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class biaoGe extends JFrame {
class shiJian implements MouseListener, ActionListener {
public biaoGe jieShou = null;
public shiJian(biaoGe chuangTi) {
this.jieShou = chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String name = jieShou.wenBenKuangName.getText();
String price = jieShou.wenBenKuangPrice.getText();
String type = jieShou.wenBenKuangTypeId.getText();
String jieshao = jieShou.wenBenKuangJieShao.
getText();
String sql = "insert into shangpin values('" + name + "'" + ", "
+ price + "," + type + ",'" + jieshao + "')";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "增加成功");
jieShou.chaxunchushihua();
} else {
JOptionPane.showMessageDialog(null, "出现了未知的错误,增加失败");
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = jieShou.biaoGe1.getSelectedRow();
jieShou.wenBenKuangBianHao
.setText(jieShou.biaoGe1.getValueAt(
row, 0).toString());
jieShou.wenBenKuangName
.setText(jieShou.biaoGe1.getValueAt(
row, 1).toString());
jieShou.wenBenKuangPrice
.setText(jieShou.biaoGe1.getValueAt(
row, 2).toString());
jieShou.wenBenKuangTypeId
.setText(jieShou.biaoGe1.getValueAt(
row, 3).toString());
jieShou.wenBenKuangJieShao
.setText(jieShou.biaoGe1.getValueAt(
row, 4).toString());
}
if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "是否确认删除这条信息?");
if (num == 0) {
int row = jieShou.biaoGe1
.getSelectedRow();
String sql = "delete shangpin where sp_id="
+ jieShou.biaoGe1.getValueAt(
row, 0) + "";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "册除成功");
jieShou.chaxunchushihua();
} else {
JOptionPane.showMessageDialog(null, "出现了未知的错误,请重试");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
static JButton zengJiaAnNiu = null;
static DefaultTableModel biaoGeMoXing1 = null;
static JScrollPane gunDongTiao = null;
static JTable biaoGe1 = null;
static JLabel wenZiBianHao, wenZiName, wenZiPrice, wenZiTypeId,
wenZiJieShao;
static JTextField wenBenKuangBianHao, wenBenKuangName,
wenBenKuangPrice,
wenBenKuangTypeId,
wenBenKuangJieShao;
static Vector BiaoTiJiHe = null;
static Vector> NeiRongJiHe = null;
JPanel mianBan1, mianBan2 = null;
public biaoGe() {
this.setTitle("登录后的界面");
this.setSize(800, 600);
this.setLayout(null);
this.setLocationRelativeTo(null);
wenZiBianHao = new JLabel("编号");
wenZiName = new JLabel("名称");
wenZiPrice = new JLabel("价格");
wenZiTypeId = new JLabel("类型ID");
wenZiJieShao = new JLabel("介绍");
zengJiaAnNiu = new JButton("添加数据");
zengJiaAnNiu.setBounds(530, 390, 100, 30);
zengJiaAnNiu.addActionListener(new shiJian(this));
this.add(zengJiaAnNiu);
wenZiBianHao.setBounds(560, 100, 70, 30);
wenZiName.setBounds(560, 140, 70, 30);
wenZiPrice.setBounds(560, 180, 70, 30);
wenZiTypeId.setBounds(560, 220, 70, 30);
wenZiJieShao.setBounds(560, 260, 70, 30);
this.add(wenZiBianHao);
this.add(wenZiName);
this.add(wenZiPrice);
this.add(wenZiTypeId);
this.add(wenZiJieShao);
wenBenKuangBianHao = new JTextField();
wenBenKuangBianHao.setEditable(false);
wenBenKuangName = new JTextField();
wenBenKuangPrice = new JTextField();
wenBenKuangTypeId = new JTextField();
wenBenKuangJieShao = new JTextField();
wenBenKuangBianHao.setBounds(640, 100, 130, 30);
wenBenKuangName.setBounds(640, 140, 130, 30);
wenBenKuangPrice.setBounds(640, 180, 130, 30);
wenBenKuangTypeId.setBounds(640, 220, 130, 30);
wenBenKuangJieShao.setBounds(640, 260, 130, 30);
this.add(wenBenKuangBianHao);
this.add(wenBenKuangName);
this.add(wenBenKuangPrice);
this.add(wenBenKuangTypeId);
this.add(wenBenKuangJieShao);
biaoGeFengZhuangFangFa();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//biaoGeFengZhuangFangFa表格的封装方法
private void biaoGeFengZhuangFangFa() {
BiaoTiJiHe = new Vector();
BiaoTiJiHe.add("编号");
BiaoTiJiHe.add("名称");
BiaoTiJiHe.add("价格");
BiaoTiJiHe.add("类型");
BiaoTiJiHe.add("介绍");
String sql = "select * from shangpin";
ResultSet res = DBUtils.Select(sql);
try {
NeiRongJiHe = new Vector>();
while (res.next()) {
Vector v = new Vector();
v.add(res.getInt("sp_ID"));
v.add(res.getString("sp_Name"));
v.add(res.getDouble("sp_price"));
v.add(res.getInt("sp_TypeID"));
v.add(res.getString("sp_Jieshao"));
NeiRongJiHe.add(v);
}
biaoGeMoXing1 = new DefaultTableModel(NeiRongJiHe,
BiaoTiJiHe) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
biaoGe1 = new JTable(biaoGeMoXing1);
biaoGe1.addMouseListener(new shiJian(this));
biaoGe1.setBounds(0, 0, 500, 500);
gunDongTiao= new JScrollPane(biaoGe1);
gunDongTiao .setBounds(0, 0, 550, 150);
mianBan1 = new JPanel();
mianBan1.add(gunDongTiao );
mianBan1.setBounds(0, 0, 550, 250);
this.add(mianBan1);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void chaxunchushihua() {
if (this.mianBan1 != null) {
this.remove(mianBan1);
}
biaoGeFengZhuangFangFa();
// 释放资源:this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}


package SwingJdbc;
import java.sql.*;
public class DBUtils {
static Connection con=null;
static Statement sta=null;
static ResultSet res=null;
//在静态代码块中执行
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装链接数据库的方法
public static Connection getCon(){
if(con==null){
try {
con=DriverManager.getConnection
("jdbc:sqlserver://localhost;databaseName=yonghu","qqq","123");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return con;
}
//查询的方法
public static ResultSet Select(String sql){
con=getCon();//建立数据库链接
try {
sta=con.createStatement();
res=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
//增删改查的方法
//返回int类型的数据
public static boolean ZSG(String sql){
con=getCon();//建立数据库链接
boolean b=false;
try {
sta=con.createStatement();
int num=sta.executeUpdate(sql);
//0就是没有执行成功,大于0 就成功了
if(num>0){
b=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}

package SwingJdbc;
public class mains {
public static void main(String[] args) {
new biaoGe();
}
}
