JAVA GUI 超市管理系統(tǒng)

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;

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子乳规,更是在濱河造成了極大的恐慌形葬,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暮的,死亡現(xiàn)場離奇詭異笙以,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)冻辩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門猖腕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人恨闪,你說我怎么就攤上這事倘感。” “怎么了咙咽?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵老玛,是天一觀的道長。 經(jīng)常有香客問我犁珠,道長逻炊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任犁享,我火速辦了婚禮余素,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘炊昆。我一直安慰自己桨吊,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布凤巨。 她就那樣靜靜地躺著视乐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪敢茁。 梳的紋絲不亂的頭發(fā)上佑淀,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機(jī)與錄音彰檬,去河邊找鬼伸刃。 笑死,一個胖子當(dāng)著我的面吹牛逢倍,可吹牛的內(nèi)容都是我干的捧颅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼较雕,長吁一口氣:“原來是場噩夢啊……” “哼碉哑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤扣典,失蹤者是張志新(化名)和其女友劉穎妆毕,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體激捏,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡设塔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了远舅。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡痕钢,死狀恐怖图柏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情任连,我是刑警寧澤蚤吹,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站随抠,受9級特大地震影響裁着,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜拱她,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一二驰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秉沼,春花似錦桶雀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至敞咧,卻和暖如春棘捣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背休建。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工乍恐, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人丰包。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓禁熏,卻偏偏與公主長得像,于是被迫代替她去往敵國和親邑彪。 傳聞我的和親對象是個殘疾皇子瞧毙,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容