package com.foreknow.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import com.foreknow.model.User;
import com.foreknow.service.UserService;
import com.foreknow.service.impl.UserServiceImpl;
public class MainWindow extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTable table;
protected static List<Object> list;
protected static int id;
private JScrollPane scrollPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setTitle("\u6210\u5458\u4FE1\u606F\u8868");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblId = new JLabel("id:");
lblId.setBounds(10, 33, 54, 15);
contentPane.add(lblId);
textField = new JTextField();
textField.setBounds(39, 30, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel label = new JLabel("\u59D3\u540D\uFF1A");
label.setBounds(115, 33, 54, 15);
contentPane.add(label);
textField_1 = new JTextField();
textField_1.setBounds(148, 30, 66, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("\u4FEE\u6539");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
id = Integer.parseInt(textField.getText());
Modify modify = new Modify();
MainWindow.this.dispose();
}
});
button.setBounds(234, 29, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton("\u5220\u9664");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 调用业务层的方法
UserService service = new UserServiceImpl();
// 获取页面中用户填写的用户名与密码
id = Integer.parseInt(textField.getText());
boolean isRight = service.deleteById(id);
if (isRight) {
System.out.println("删除成功...");
// lblNewLabel.setText("添加成功...");
} else {
System.out.println("error...");
}
}
});
button_1.setBounds(337, 29, 93, 23);
contentPane.add(button_1);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserService service = new UserServiceImpl();
if (textField.getText().equals("") && textField_1.getText().equals("")) {
list = service.getAll();
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
objects[i][0] = user.getId();
objects[i][1] = user.getUsername();
objects[i][2] = user.getPassword();
objects[i][3] = user.getEmail();
objects[i][4] = user.getPrower();
table.setModel(
new DefaultTableModel(objects, new String[] { "人员编号", "姓名", "密码", "email", "是/否管理员" }));
}
scrollPane.setViewportView(table);
table = new JTable();
} else if (textField.getText().equals("")) {
list = service.getByName(textField_1.getText());
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
objects[i][0] = user.getId();
objects[i][1] = user.getUsername();
objects[i][2] = user.getPassword();
objects[i][3] = user.getEmail();
objects[i][4] = user.getPrower();
table.setModel(
new DefaultTableModel(objects, new String[] { "人员编号", "姓名", "密码", "email", "是/否管理员" }));
}
// scrollPane.setViewportView(table);
table = new JTable();
} else if (textField_1.getText().equals("")) {
list = service.getQueryById(Integer.parseInt(textField.getText()));
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
objects[i][0] = user.getId();
objects[i][1] = user.getUsername();
objects[i][2] = user.getPassword();
objects[i][3] = user.getEmail();
objects[i][4] = user.getPrower();
table.setModel(
new DefaultTableModel(objects, new String[] { "人员编号", "姓名", "密码", "email", "是/否管理员" }));
}
// scrollPane.setViewportView(table);
table = new JTable();
}
}
});
btnNewButton.setBounds(337, 62, 93, 23);
contentPane.add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(38, 95, 366, 157);
contentPane.add(scrollPane);
UserService service = new UserServiceImpl();
list = service.getAll();
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
objects[i][0] = user.getId();
objects[i][1] = user.getUsername();
objects[i][2] = user.getPassword();
objects[i][3] = user.getEmail();
objects[i][4] = user.getPrower();
}
table = new JTable();
table.setModel(new DefaultTableModel(objects, new String[] { "人员编号", "姓名", "密码", "email", "是/否管理员" }));
scrollPane.setViewportView(table);
JButton button_2 = new JButton("\u8FD4\u56DE");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Logins logins = new Logins();
MainWindow.this.disable();
MainWindow.this.dispose();
}
});
button_2.setBounds(234, 62, 93, 23);
contentPane.add(button_2);
setVisible(true);
}
}
2019-08-31 MainWindow extends JFrame
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...