⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu1_2.java

📁 实现一个图书管理系统的界面。要求简洁美观。可以把主要功能放在状态栏上。
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.sql.*;
public class Menu1_2 extends JFrame implements ActionListener//JFrame implements ActionListener//
{
	//-------------------界面控件元素-----------------------------
	JLabel lbl1=new JLabel("分类号");
	JLabel lbl2=new JLabel("分类名");
	JTextField txt1=new JTextField(10);
	JTextField txt2=new JTextField(10);
	JButton btn1=new JButton("添加");
	JButton btn2=new JButton("更新");
	JButton btn3=new JButton("退出");
	JButton btn4=new JButton("退出");
	String col[]={"分类号","分类名"};
	String rows[][]=new String[20][2];
	JTable tb;
	JScrollPane jsp;
	JTabbedPane tab=new JTabbedPane();

    public Menu1_2()	//构造函数
    {
        init();
    	btn1.addActionListener(this);
    	btn2.addActionListener(this);
    	btn3.addActionListener(this);
    	btn4.addActionListener(this);
    }
    public void init()
    {
    	this.setTitle("图书分类管理");
        //-------------------Tab1-----------------------------
   		JPanel jp1=new JPanel();
   		jp1.setLayout(new GridLayout(2,2,10,10));
   	     txt1.setText("  ");
   		jp1.add(lbl1);jp1.add(txt1);
   		jp1.add(lbl2);jp1.add(txt2);
   		
   		JPanel jp2=new JPanel();
    	jp2.add(btn1);jp2.add(btn4);
    	
    	JPanel jp3=new JPanel();
    	jp3.setLayout(new BorderLayout());
    	jp3.add(jp1,BorderLayout.CENTER);
    	jp3.add(jp2,BorderLayout.SOUTH);
    	
    	//-------------------Tab2-----------------------------
    	
    	JPanel jp4=new JPanel();
    	jp4.add(btn2);jp4.add(btn3);
      	tb=new JTable(rows,col);
    	jsp=new JScrollPane(tb);

    	JPanel jp5=new JPanel();
    	jp5.setLayout(new BorderLayout());
    	jp5.add(jsp,BorderLayout.CENTER);
    	jp5.add(jp4,BorderLayout.SOUTH);
    	
    	//-------------------放置Tab-----------------------------
    	tab.add(jp3,"添加");tab.add(jp5,"删改");
    	this.getContentPane().add(tab);
    	
    	this.setLocation(200,200);
    	this.setSize(300,300);
    	this.setVisible(true);
    
    }
    public void actionPerformed(ActionEvent e)
    {
    	if(e.getSource()==btn1){
			try{	
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
			Statement cmd=con.createStatement();
			cmd.executeUpdate("insert into bookType(bTypeId,bTypeName) values('"+txt1.getText()+"',"+"'"+txt2.getText()+"')");	
			JOptionPane.showMessageDialog(this,"储存成功");		
    		}
				catch(Exception ex)
			{
				System.out.println(ex);
			}   
		}				
    		if(e.getSource()==btn2){
    		try{
    			int flag=0;
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
				Statement cmd=con.createStatement();
				ResultSet rs=cmd.executeQuery("select * from bookType");
				while(rs.next())
				{
						rows[flag][0]=rs.getString(1);
						rows[flag][1]=rs.getString(2);
						
					    flag++;
					    tb.setModel(new DefaultTableModel(rows,col));
				}
			}
				catch(Exception ex)
			{
				System.out.println(ex);
			}   		
			}		
    	if(e.getSource()==btn3)
    		this.dispose();
    	if(e.getSource()==btn4)
    		this.dispose();
    }
    public static void main(String arg[]){
    	Menu1_2 frm=new Menu1_2();
    	frm.setSize(400,320);
    	frm.setVisible(true);
    }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -