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

📄 mainframe.java

📁 完成界面功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					});
		}
		return deleteMenuItem;
	}

	/**
	 * This method initializes addButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText("新增");
			addButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String username = usernameTextField.getText();
					String password = String.valueOf(PasswordField
							.getPassword());
					String gender = "m";
					if (femaleRadioButton.isSelected()) {
						gender = "f";
					}
					String birthday = new SimpleDateFormat("yyyy-MM-dd")
							.format(birthTextField.getDate());
					// String birthday = birthTextField.getText();
					String address = addressTextField.getText();
					String user_desc = user_descTextField.getText();
					if (username.equals("")) {
						usernameTextField.setBackground(Color.pink);
						msgLabel.setText("请输入用户名");
						return;
					}
					int number = UserDao.userInsert(username, password, gender,
							birthday, address, user_desc);
					System.out.println(number);
					userTable.setModel(new UserTableModel());
					msgLabel.setText("添加成功!");
				}
			});
		}
		return addButton;
	}

	/**
	 * This method initializes deleteButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("删除");
			deleteButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// System.out.println("actionPerformed()"); // TODO
					// Auto-generated Event stub actionPerformed()
					int selectedRow = userTable.getSelectedRow();
					UserTableModel model = (UserTableModel) userTable
							.getModel();
					String id = ((String) model.getValueAt(selectedRow, 0));
					int number = UserDao.deleteUser(id);
					System.out.println(number);
					userTable.setModel(new UserTableModel());
					msgLabel.setText("删除成功");
				}
			});
		}
		return deleteButton;
	}

	/**
	 * This method initializes bookmainScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getBookmainScrollPane() {
		if (bookmainScrollPane == null) {
			bookmainScrollPane = new JScrollPane();
			bookmainScrollPane.setViewportView(getBookTable());
		}
		return bookmainScrollPane;
	}

	/**
	 * This method initializes bookTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getBookTable() {
		if (bookTable == null) {
			bookTable = new JTable();
			bookTable.setModel(new BookTableModel());
			bookTable.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					JPopupMenu popupMenu = getRightPopupMenu();

					if (e.getButton() == MouseEvent.BUTTON1) {
						int selectedRow = bookTable.getSelectedRow();
						BookTableModel model = (BookTableModel) bookTable
								.getModel();
						booknameTextField.setText((String) model.getValueAt(
								selectedRow, 1));
						// publishdateField.setText((String)
						// model.getValueAt(selectedRow, 2));
						SimpleDateFormat format = new SimpleDateFormat(
								"yyyy-MM-dd");
						try {
							publishdateField.setDate(format
									.parse((String) model.getValueAt(
											selectedRow, 2)));
						} catch (ParseException e1) {
							// TODO 自动生成 catch 块
							e1.printStackTrace();
						}
						// if (((String) model.getValueAt(selectedRow,
						// 3)).equals("m")) {
						// maleRadioButton.setSelected(true);
						// }else{
						// femaleRadioButton.setSelected(true);
						// }
						// addressTextField.setText((String) model.getValueAt(
						// selectedRow, 5));
						// user_descTextField.setText((String) model.getValueAt(
						// selectedRow, 6));
						System.out.println("left");

						deleteMenuItem.setEnabled(true);
					}
					if (e.getButton() == MouseEvent.BUTTON2) {
						System.out.println("middle");
					}
					if (e.getButton() == MouseEvent.BUTTON3) {
						System.out.println("right");
						popupMenu.show(bookTable, e.getX(), e.getY());
					}

				}
			});
		}
		return bookTable;
	}

	/**
	 * This method initializes booksearchPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBooksearchPanel() {
		if (booksearchPanel == null) {
			FlowLayout flowLayout2 = new FlowLayout();
			flowLayout2.setAlignment(FlowLayout.LEFT);
			searchLabel1 = new JLabel();
			searchLabel1.setText("输入要查询的书名");
			searchLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
			searchLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			booksearchPanel = new JPanel();
			booksearchPanel.setLayout(flowLayout2);
			booksearchPanel.add(searchLabel1, null);
			booksearchPanel.add(getSearchbookTextField(), null);
			booksearchPanel.add(getBooksearchButton(), null);
		}
		return booksearchPanel;
	}

	/**
	 * This method initializes bookInfoPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBookInfoPanel() {
		if (bookInfoPanel == null) {
			bookInfoPanel = new JPanel();
			bookInfoPanel.setLayout(new BorderLayout());
			bookInfoPanel.add(getBookinputPanel(), BorderLayout.CENTER);
			bookInfoPanel.add(getBookbtnPanel(), BorderLayout.SOUTH);
		}
		return bookInfoPanel;
	}

	/**
	 * This method initializes searchbookTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSearchbookTextField() {
		if (searchbookTextField == null) {
			searchbookTextField = new JTextField();
			searchbookTextField.setColumns(40);
		}
		return searchbookTextField;
	}

	/**
	 * This method initializes booksearchButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBooksearchButton() {
		if (booksearchButton == null) {
			booksearchButton = new JButton();
			booksearchButton.setText("查询");
			booksearchButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String condition = searchbookTextField.getText();
							bookTable.setModel(new BookTableModel(condition));
						}
					});
		}
		return booksearchButton;
	}

	/**
	 * This method initializes bookinputPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBookinputPanel() {
		if (bookinputPanel == null) {
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.gridx = 3;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints17.insets = new Insets(6, 6, 6, 6);
			gridBagConstraints17.gridx = 4;
			gridBagConstraints17.weightx = 1.0;
			publishdateLabel1 = new JLabel();
			publishdateLabel1.setText("出版日期");
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints16.insets = new Insets(6, 6, 6, 6);
			gridBagConstraints16.gridx = 2;
			gridBagConstraints16.weightx = 1.0;
			booknameLabel1 = new JLabel();
			booknameLabel1.setText("书名");
			bookinputPanel = new JPanel();
			bookinputPanel.setLayout(new GridBagLayout());
			bookinputPanel.add(booknameLabel1, new GridBagConstraints());
			bookinputPanel.add(getBooknameTextField(), gridBagConstraints16);
			bookinputPanel.add(publishdateLabel1, gridBagConstraints19);
			bookinputPanel.add(getPublishdateField(), gridBagConstraints17);
		}
		return bookinputPanel;
	}

	/**
	 * This method initializes bookbtnPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBookbtnPanel() {
		if (bookbtnPanel == null) {
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = -1;
			gridBagConstraints18.gridy = -1;
			bookbtnPanel = new JPanel();
			bookbtnPanel.setLayout(new FlowLayout());
			bookbtnPanel.add(getAddButton1(), null);
			bookbtnPanel.add(getUpdateButton1(), null);
			bookbtnPanel.add(getDeleteButton1(), null);
		}
		return bookbtnPanel;
	}

	/**
	 * This method initializes booknameTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getBooknameTextField() {
		if (booknameTextField == null) {
			booknameTextField = new JTextField();
		}
		return booknameTextField;
	}

	/**
	 * This method initializes publishdateField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JXDatePicker getPublishdateField() {
		if (publishdateField == null) {
			publishdateField = new JXDatePicker();
			String[] formats = { "yyyy-MM-dd" };
			publishdateField.setFormats(formats);

			JXMonthView monthview = publishdateField.getMonthView();
			String[] days = { "日	", "一", "二", "三", "四", "五", "六" };
			monthview.setDaysOfTheWeek(days);
			monthview.setFirstDayOfWeek(Calendar.MONDAY);
			publishdateField.setLinkDay(new Date(), "今天是{0,date,yyyy-MM-dd}");
		}
		return publishdateField;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
		}
		return jButton;
	}

	/**
	 * This method initializes addButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddButton1() {
		if (addButton1 == null) {
			addButton1 = new JButton();
			addButton1.setText("新增");
			addButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// new AddBookFrame().setVisible(true);
					String bookname = booknameTextField.getText();
					// String password =
					// String.valueOf(PasswordField.getPassword());
					// String gender = "m";
					// if(femaleRadioButton.isSelected()){
					// gender= "f";
					// }
					String publishdate = new SimpleDateFormat("yyyy-MM-dd")
							.format(publishdateField.getDate());
					// String publishdate = publishdateField.getText();
					// String address = addressTextField.getText();
					// String user_desc = user_descTextField.getText();
					if (bookname.equals("")) {
						booknameTextField.setBackground(Color.pink);
						msgLabel.setText("请输入书名");
						return;
					}
					int number = BookDao.bookInsert(bookname, publishdate);
					msgLabel.setText("添加成功!");
					bookTable.setModel(new BookTableModel());
				}
			});
		}
		return addButton1;
	}

	/**
	 * This method initializes updateButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getUpdateButton1() {
		if (updateButton1 == null) {
			updateButton1 = new JButton();
			updateButton1.setText("修改");
			updateButton1
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							int selectedRow = bookTable.getSelectedRow();
							BookTableModel model = (BookTableModel) bookTable
									.getModel();
							String id = ((String) model.getValueAt(selectedRow,
									0));
							String bookname = booknameTextField.getText();
							Date publishdate = publishdateField.getDate();
							// String gender = "m";
							// if (femaleRadioButton.isSelected()) {
							// gender = "f";
							// }
							// String birthday = new
							// SimpleDateFormat("yyyy-MM-dd").format(birthTextField.getDate());
							// Date birthday = birthTextField.getDate();
							// String birthday = birthTextField.getText();
							// String address = addressTextField.getText();
							// String user_desc = user_descTextField.getText();

							int number = BookDao.updateBook(id, bookname,
									publishdate);
							System.out.println(number);
							msgLabel.setText("更新成功!");
							bookTable.setModel(new BookTableModel());

						}
					});
		}
		return updateButton1;
	}

	/**
	 * This method initializes deleteButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton1() {
		if (deleteButton1 == null) {
			deleteButton1 = new JButton();
			deleteButton1.setText("删除");
			deleteButton1
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							int selectedRow = bookTable.getSelectedRow();
							BookTableModel model = (BookTableModel) bookTable
									.getModel();
							String id = ((String) model.getValueAt(selectedRow,
									0));
							int number = BookDao.deleteBook(id);
							System.out.println(number);
							bookTable.setModel(new BookTableModel());
							msgLabel.setText("删除成功");
						}
					});
		}
		return deleteButton1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	
		JFrame.setDefaultLookAndFeelDecorated(true);
		try {
			UIManager.setLookAndFeel(new SubstanceDefaultLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame thisClass = new MainFrame();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
//		MainFrame jContentPane = null;
//		this.jContentPane.setVisible(false);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(540, 348);
		this.setJMenuBar(getTopJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
		this.jContentPane.setVisible(false);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getStatusPanel(), BorderLayout.SOUTH);
			jContentPane.add(getMainTabbedPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	private void isSelectStatus(boolean isSelect) {
		if (isSelect) {
			addMenuItem.setEnabled(true);
			updateMenuItem.setEnabled(true);
			deleteMenuItem.setEnabled(true);

		} else {

			addMenuItem.setEnabled(true);
			updateMenuItem.setEnabled(false);
			deleteMenuItem.setEnabled(false);

		}

	}

} // @jve:decl-index=0:visual-constraint="59,-65"

⌨️ 快捷键说明

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