Java swing jdbc:設(shè)置背景顏色,獲取素材方法,表格,圖片等的切換【詩書畫唱】

在PC端點擊圖片,鼠標(biāo)右鍵可把圖片素材另存到桌面使用,手機端可長按圖片保存到本地相冊,夏歡和認為有用的話就點個贊,三連就更滿足我的期待了

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("三連關(guān)注界面");
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("三連關(guān)注界面")){
jieShou.jpBlueRightBottom1.setVisible(false);
jieShou.jpGreenRightBottom2.setVisible(true);
}
}
}




JTree樹形控件點擊內(nèi)容彈出新的窗體

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("三連關(guān)注界面");
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("三連關(guān)注界面")){
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{
//要聲明 : 裝載內(nèi)容的容器,table的控件, 容器的標(biāo)題, 容器的具體的內(nèi)容。
public static JTable biaoGe=null;//JTable為表格的控件
//要聲明裝載內(nèi)容的容器,如下:
public static DefaultTableModel DTM=null;
//Vector中:
//一個放標(biāo)題,一個放內(nèi)容
// <Vector<String>>表示只接受集合的類型
Vector<String> biaoTi;
Vector<Vector<String>> neiRong;
public swingBiaoGe(){
this.setLayout(null);
this.setSize(600,600);
this.setLocationRelativeTo(null);
//給標(biāo)題賦值:
biaoTi=new Vector<String>();
biaoTi.add("編號");biaoTi.add("姓名");
biaoTi.add("性別");biaoTi.add("年齡");
//給內(nèi)容賦值:
neiRong=new Vector<Vector<String>>();
for(int i=0;i<5;i++){
Vector<String> v=new Vector<String>();
v.add("編號"+(i+6));v.add("詩書畫唱"+(i+6));
v.add("性別"+(i+6));v.add("年齡"+(i+6));
neiRong.add(v);
}
//將內(nèi)容添加到裝載內(nèi)容的容器中:
DTM=new DefaultTableModel(neiRong,biaoTi);
DTM=new DefaultTableModel(neiRong,biaoTi) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
biaoGe=new JTable(DTM);
//設(shè)置滾動條:
JScrollPane jsp=new JScrollPane(biaoGe);
jsp.setBounds(10,10,400,400);
this.add(jsp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}


JTable初始化數(shù)據(jù),數(shù)據(jù)要求鏈接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,'好吃營養(yǎng)好');
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, "出現(xiàn)了未知的錯誤,增加失敗");
}
}
@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, "出現(xiàn)了未知的錯誤,請重試");
}
}
}
}
@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<Object> BiaoTiJiHe = null;
static Vector<Vector<Object>> 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("添加數(shù)據(jù)");
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<Object>();
BiaoTiJiHe.add("編號");
BiaoTiJiHe.add("名稱");
BiaoTiJiHe.add("價格");
BiaoTiJiHe.add("類型");
BiaoTiJiHe.add("介紹");
String sql = "select * from shangpin";
ResultSet res = DBUtils.Select(sql);
try {
NeiRongJiHe = new Vector<Vector<Object>>();
while (res.next()) {
Vector<Object> v = new Vector<Object>();
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;
//在靜態(tài)代碼塊中執(zhí)行
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封裝鏈接數(shù)據(jù)庫的方法
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();//建立數(shù)據(jù)庫鏈接
try {
sta=con.createStatement();
res=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
//增刪改查的方法
// 返回int類型的數(shù)據(jù)
public static boolean ZSG(String sql){
con=getCon();//建立數(shù)據(jù)庫鏈接
boolean b=false;
try {
sta=con.createStatement();
int num=sta.executeUpdate(sql);
//0就是沒有執(zhí)行成功,大于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();
}
}

