import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JScrollBar;
import javax.swing.JLabel;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JMenuBar;
import javax.swing.JEditorPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JMenuItem;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Font;
import javax.swing.UIManager;
import javax.swing.JTabbedPane;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class supermarket {
ImageIcon icon=new ImageIcon("C:\\Users\\聯(lián)想\\Desktop\\歡迎界面.jpg");//要加載的開始界面的圖片
private int y_pos=0;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
supermarket window = new supermarket();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @throws SQLException
* @throws ClassNotFoundException
*/
public supermarket() throws ClassNotFoundException, SQLException {
initialize();
}
/**
* Initialize the contents of the frame.
* @throws ClassNotFoundException
* @throws SQLException
*/
private void initialize() throws ClassNotFoundException, SQLException {//初始化窗口
//首先初始化JDBC需要的條件
Class.forName("com.mysql.jdbc.Driver");
//獲取數(shù)據(jù)庫連接
String url="jdbc:mysql://localhost:3306/supermarket?userSSL=ture&serverTimezone=UTC";
String username="root";
String pswd="4758stuv870399";
Connection conn=DriverManager.getConnection(url,"root","4758stuv870399");
//獲取statement對象
Statement stmt=conn.createStatement();
frame = new JFrame("水果超市歡迎您!");
frame.setBackground(Color.WHITE);
frame.getContentPane().setBackground(new Color(64, 224, 208));
frame.setTitle("\u8D85\u5E02\u8D27\u7269\u7BA1\u7406");
frame.setBounds(100, 100, 725, 443);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewJgoodiesTitle = DefaultComponentFactory.getInstance().createTitle("\u6C34\u679C\u5217\u8868");
lblNewJgoodiesTitle.setBounds(271, 0, 139, 36);
lblNewJgoodiesTitle.setFont(new Font("Monospaced", Font.PLAIN, 24));
frame.getContentPane().add(lblNewJgoodiesTitle);
JButton btnNewButton = new JButton("\u63D0\u4EA4");//提交按鈕
btnNewButton.setBounds(580, 262, 93, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.getContentPane().add(btnNewButton);
JButton button = new JButton("\u6539\u52A8");
button.setBounds(580, 295, 93, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u5220\u9664");
button_1.setBounds(580, 328, 93, 23);
frame.getContentPane().add(button_1);
JTextPane textPane3 = new JTextPane();
textPane3.setBounds(319, 262, 93, 23);
frame.getContentPane().add(textPane3);
JTextPane textPane2 = new JTextPane();
textPane2.setBounds(210, 262, 84, 23);
frame.getContentPane().add(textPane2);
JTextPane textPane4 = new JTextPane();
textPane4.setBounds(440, 262, 102, 23);
frame.getContentPane().add(textPane4);
JTextPane textPane6 = new JTextPane();
textPane6.setBackground(new Color(255, 255, 255));
textPane6.setBounds(210, 295, 84, 23);
frame.getContentPane().add(textPane6);
JTextPane textPane7 = new JTextPane();
textPane7.setBounds(319, 295, 93, 23);
frame.getContentPane().add(textPane7);
JTextPane textPane8 = new JTextPane();
textPane8.setBounds(440, 295, 102, 23);
frame.getContentPane().add(textPane8);
JTextPane textPane9 = new JTextPane();
textPane9.setBounds(92, 328, 93, 23);
frame.getContentPane().add(textPane9);
JTextPane textPane1 = new JTextPane();
textPane1.setBounds(92, 262, 93, 23);
frame.getContentPane().add(textPane1);
JTextPane textPane5 = new JTextPane();
textPane5.setBounds(92, 295, 93, 23);
frame.getContentPane().add(textPane5);
JLabel label = new JLabel("\u6C34\u679C\u7F16\u53F7");
label.setBounds(103, 240, 54, 15);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u6C34\u679C\u5355\u4EF7");
label_1.setBounds(331, 240, 54, 15);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("\u6C34\u679C\u540D\u79F0");
label_2.setBounds(210, 240, 54, 15);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("\u8BA1\u4EF7\u5355\u4F4D");
label_3.setBounds(440, 240, 54, 15);
frame.getContentPane().add(label_3);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(107, 49, 402, 168);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300,1000));//設(shè)置展示的區(qū)域矫夯,如果大于scrollPane舌界,就會顯示出進(jìn)度條
//當(dāng)然也可以根據(jù)條件當(dāng)裝不下的時候重新設(shè)置pannel的展示大小
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setText("\u6C34\u679C\u540D\u79F0");
textPane.setEditable(false);
textPane.setBounds(100, 0, 100, 20);
JTextPane textPane_1 = new JTextPane();
textPane_1.setText("\u6C34\u679C\u5355\u4EF7");
textPane_1.setBackground(Color.BLUE);
textPane_1.setBounds(200, 0, 100, 20);
JTextPane textPane_2 = new JTextPane();
textPane_2.setText("\u8BA1\u4EF7\u5355\u4F4D");
textPane_2.setEditable(false);
textPane_2.setBounds(300, 0, 100, 20);
JTextPane textPane_3 = new JTextPane();
textPane_3.setText("\u6C34\u679C\u7F16\u53F7");
textPane_3.setForeground(Color.BLACK);
textPane_3.setEditable(false);
textPane_3.setBackground(Color.BLUE);
textPane_3.setBounds(0, 0, 100, 20);
//執(zhí)行sql語句躯嫉,將數(shù)據(jù)庫中所有的信息 都提取出來并將其打印在窗口中
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);//update_UI用于刷新UI界面
button_1.addMouseListener(new MouseAdapter() {//刪除按鈕事件
@Override
public void mouseClicked(MouseEvent e) {
//獲取數(shù)據(jù)
String id=textPane9.getText();
System.out.println(id);
if(!id.isEmpty()) {
textPane9.setText(null);
//然后執(zhí)行sql語句元镀,將指定id值的數(shù)據(jù)進(jìn)行刪除
String sql="delete from goods_info where id="+id;
try {
stmt.execute(sql);
stmt.execute("commit");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//更新 窗口
panel.removeAll();
try {
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
button.addMouseListener(new MouseAdapter() {//改動按鈕事件
@Override
public void mouseClicked(MouseEvent e) {
/*
* 當(dāng)點擊改動按鈕的時候沧踏,獲取到改動的值步悠,id值是必須要輸入的脆淹!
* 常空。。盖溺。好吧如果只改動一個值那么就要判斷好多次漓糙,,烘嘱,還是全都輸入才能改動吧(其實就是懶)
*
*/
//獲取數(shù)據(jù)
String id=textPane5.getText();
String name=textPane6.getText();
String price=textPane7.getText();
String danwei=textPane8.getText();
//判斷是否為空值
if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必須用是否為空判斷 ==昆禽?
textPane5.setText(null);
? ? textPane6.setText(null);
textPane7.setText(null);
textPane8.setText(null);//首先清除所有的值
//然后執(zhí)行sql語句,將數(shù)據(jù)值進(jìn)行更新
String sql="update goods_info set name=\""+name+"\",price="+price+",unit=\""+danwei+"\" where id="+id;
try {
stmt.execute(sql);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//然后刷新一下就好了
try {
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
btnNewButton.addMouseListener(new MouseAdapter() {//提交按鈕事件蝇庭,比較特殊醉鳖,因為不需要刷新,只要在末尾添加即可哮内。盗棵。大概吧
@Override
public void mouseClicked(MouseEvent e) {
/*
* 當(dāng)點擊提交按鈕時, 先判斷前四個按鈕 是否都有文本北发,
* 如果含有文本纹因,那么就創(chuàng)建 四個不可改變的textPane
* 給它們對應(yīng)賦值以后,放到主顯示框上
*/
//獲取數(shù)據(jù)
String id=textPane1.getText();
String name=textPane2.getText();
String price=textPane3.getText();
String danwei=textPane4.getText();
//判斷是否為空值
if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必須用是否為空判斷 ==鲫竞?
y_pos+=20;
System.out.println(id+"---"+name+"---"+price+"---"+danwei);
//輸入正確辐怕,處理數(shù)據(jù)1、清空 輸入框的數(shù)據(jù) 2从绘、將數(shù)據(jù)存儲到mysql 中3寄疏、將數(shù)據(jù)上傳
textPane1.setText(null);
? ? textPane2.setText(null);
textPane3.setText(null);
textPane4.setText(null);
JTextPane t1=new JTextPane();//創(chuàng)建臨時按鈕,設(shè)置文本僵井,設(shè)置不可編輯
t1.setText(id);//填充數(shù)值
t1.setEditable(false);//設(shè)置不可被改動
t1.setFont(new Font("宋體", Font.BOLD, 12));
t1.setBorder(BorderFactory.createEtchedBorder());//設(shè)置邊框
JTextPane t2=new JTextPane();
t2.setText(name);
t2.setEditable(false);
t2.setFont(new Font("宋體", Font.BOLD, 12));
t2.setBorder(BorderFactory.createEtchedBorder());
JTextPane t3=new JTextPane();
t3.setText(price);
t3.setEditable(false);
t3.setFont(new Font("宋體", Font.BOLD, 12));
t3.setBorder(BorderFactory.createEtchedBorder());
JTextPane t4=new JTextPane();
t4.setText(danwei);
t4.setEditable(false);
t4.setFont(new Font("宋體", Font.BOLD, 12));
t4.setBorder(BorderFactory.createEtchedBorder());
t1.setBounds(0,y_pos, 100, 20);//X Y? WIDTH HEIGH
t2.setBounds(100,y_pos, 100, 20);
t3.setBounds(200,y_pos, 100, 20);
t4.setBounds(300,y_pos, 100, 20);
//將數(shù)據(jù)存入MySQL中
String sql="insert into goods_info value("+id+",\""+name+"\","+price+",\""+danwei+"\");";
try {
stmt.execute(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});//第一個按鈕的監(jiān)聽器
}
private int update_UI(JPanel panel, Statement stmt, JTextPane textPane, JTextPane textPane_1,
JTextPane textPane_2, JTextPane textPane_3) throws SQLException {
panel.updateUI();//首先將其全部組件進(jìn)行刷新
panel.add(textPane);
panel.add(textPane_1);
panel.add(textPane_2);
panel.add(textPane_3);//添加上基礎(chǔ)的四個組件
Statement stmt1=stmt;//獲取操作數(shù)據(jù)庫的權(quán)限
String sql="select * from goods_info";
ResultSet rs=stmt1.executeQuery(sql);//執(zhí)行sql語句陕截,這條語句會返回一個Set集合,里面包含要獲取的信息
int pos=21;
while(rs.next()) {//對其進(jìn)行迭代
String name=rs.getString("name");
int price=rs.getInt("price");
int id=rs.getInt("id");
String unit=rs.getString("unit");//每一次獲取到完整的四個數(shù)據(jù)
System.out.println(id+name+price+"/"+"unit");
JTextPane t1=new JTextPane();//創(chuàng)建臨時按鈕批什,設(shè)置文本农曲,設(shè)置不可編輯
t1.setText(id+"");//填充數(shù)值
t1.setEditable(false);//設(shè)置不可被改動
t1.setFont(new Font("宋體", Font.BOLD, 12));
t1.setBorder(BorderFactory.createEtchedBorder());//設(shè)置邊框
JTextPane t2=new JTextPane();
t2.setText(name);
t2.setEditable(false);
t2.setFont(new Font("宋體", Font.BOLD, 12));
t2.setBorder(BorderFactory.createEtchedBorder());
JTextPane t3=new JTextPane();
t3.setText(price+"");
t3.setEditable(false);
t3.setFont(new Font("宋體", Font.BOLD, 12));
t3.setBorder(BorderFactory.createEtchedBorder());
JTextPane t4=new JTextPane();
t4.setText(unit+"");
t4.setEditable(false);
t4.setFont(new Font("宋體", Font.BOLD, 12));
t4.setBorder(BorderFactory.createEtchedBorder());
t1.setBounds(0,pos, 100, 20);//X Y? WIDTH HEIGH
panel.add(t1);
t2.setBounds(100,pos, 100, 20);
panel.add(t2);
t3.setBounds(200,pos, 100, 20);
panel.add(t3);
t4.setBounds(300,pos, 100, 20);
panel.add(t4);
pos+=20;
}
return pos-20;
}
}