JAVA GUI 超市管理系统

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\\联想\\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");

//获取数据库连接

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));//设置展示的区域,如果大于scrollPane,就会显示出进度条

//当然也可以根据条件当装不下的时候重新设置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);

//执行sql语句,将数据库中所有的信息 都提取出来并将其打印在窗口中

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) {

//获取数据

String id=textPane9.getText();

System.out.println(id);

if(!id.isEmpty()) {

textPane9.setText(null);

//然后执行sql语句,将指定id值的数据进行删除

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) {

/*

* 当点击改动按钮的时候,获取到改动的值,id值是必须要输入的!

* 。。。好吧如果只改动一个值那么就要判断好多次,,,还是全都输入才能改动吧(其实就是懒)

*

*/

//获取数据

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);//首先清除所有的值

//然后执行sql语句,将数据值进行更新

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) {

/*

* 当点击提交按钮时, 先判断前四个按钮 是否都有文本,

* 如果含有文本,那么就创建 四个不可改变的textPane

* 给它们对应赋值以后,放到主显示框上

*/

//获取数据

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);

//输入正确,处理数据1、清空 输入框的数据 2、将数据存储到mysql 中3、将数据上传

textPane1.setText(null);

    textPane2.setText(null);

textPane3.setText(null);

textPane4.setText(null);

JTextPane t1=new JTextPane();//创建临时按钮,设置文本,设置不可编辑

t1.setText(id);//填充数值

t1.setEditable(false);//设置不可被改动

t1.setFont(new Font("宋体", Font.BOLD, 12));

t1.setBorder(BorderFactory.createEtchedBorder());//设置边框

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);

//将数据存入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();

}

}

}

});//第一个按钮的监听器

}

private int update_UI(JPanel panel, Statement stmt, JTextPane textPane, JTextPane textPane_1,

JTextPane textPane_2, JTextPane textPane_3) throws SQLException {

panel.updateUI();//首先将其全部组件进行刷新

panel.add(textPane);

panel.add(textPane_1);

panel.add(textPane_2);

panel.add(textPane_3);//添加上基础的四个组件

Statement stmt1=stmt;//获取操作数据库的权限

String sql="select * from goods_info";

ResultSet rs=stmt1.executeQuery(sql);//执行sql语句,这条语句会返回一个Set集合,里面包含要获取的信息

int pos=21;

while(rs.next()) {//对其进行迭代

String name=rs.getString("name");

int price=rs.getInt("price");

int id=rs.getInt("id");

String unit=rs.getString("unit");//每一次获取到完整的四个数据

System.out.println(id+name+price+"/"+"unit");

JTextPane t1=new JTextPane();//创建临时按钮,设置文本,设置不可编辑

t1.setText(id+"");//填充数值

t1.setEditable(false);//设置不可被改动

t1.setFont(new Font("宋体", Font.BOLD, 12));

t1.setBorder(BorderFactory.createEtchedBorder());//设置边框

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;

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容