📄 addboatframe.java
字号:
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
public class AddBoatFrame implements ActionListener {
private JFrame acFrame; // 定义FRAME
ArrayList array = new ArrayList();
Customer cust;
static int c_i = 0;
// 定义PANEL
private JPanel up_panel;
private JPanel name_panel;
private JPanel tel_panel;
private JPanel add_panel;
private JPanel button_panel;
private JPanel main_panel;
private JPanel cb_panel;
// 定义标签
private JLabel up_lb;
private JLabel length_lb;
private JLabel loca_lb;
private JLabel bName_lb;
private JLabel cb_lb;
// 定义输入框
private JTextField bName_tf;
private JTextField length_tf;
private JTextField loca_tf;
private JTextField cb_tf;
// 定义按妞
JButton check_button;
JButton del_button;
JButton update_button;
JButton return_button;
public AddBoatFrame(ArrayList ar) {
//public AddBoatFrame(){
int ar_i = ar.size();
for (int j = 0; j < ar_i; j++) {
Customer ct = (Customer) ar.get(j);
String id = new String(ct.getB().getId());
String length = new String(ct.getB().getLength());
String name = new String(ct.getName());
String tel = new String(ct.getTel());
String pwd = new String(ct.getPwd());
String add = new String(ct.getAddress());
cust = new Customer(id, length, name, tel, pwd, add);
this.array.add(ct);
}
acFrame = new JFrame("增加船只/查看船只"); // 定义 caFRAME
// 构件容器
up_panel = new JPanel();
name_panel = new JPanel();
tel_panel = new JPanel();
add_panel = new JPanel();
button_panel = new JPanel();
cb_panel = new JPanel();
main_panel = new JPanel(new GridLayout(6, 1));
// 构件标签
up_lb = new JLabel("修改船只/查看船只");
up_lb.setForeground(Color.red);
up_lb.setFont(new Font("仿宋体", 20, 20));
length_lb = new JLabel("船长:");
bName_lb = new JLabel("船号:");
loca_lb = new JLabel("船位:");
cb_lb = new JLabel("所属:");
// 构件输入框
bName_tf = new JTextField(10);
length_tf = new JTextField(10);
loca_tf = new JTextField(10);
cb_tf = new JTextField(10);
cb_tf.setEditable(false);
// 构件按妞
check_button = new JButton("查找");
update_button = new JButton("更新");
del_button = new JButton("删除");
return_button = new JButton("返回");
check_button.addActionListener(this);
update_button.addActionListener(this);
del_button.addActionListener(this);
return_button.addActionListener(this);
// 将组件加入容器
up_panel.add(up_lb);
name_panel.add(bName_lb);
name_panel.add(bName_tf);
tel_panel.add(length_lb);
tel_panel.add(length_tf);
//add_panel.add(loca_lb);
//add_panel.add(loca_tf);
cb_panel.add(cb_lb);
cb_panel.add(cb_tf);
button_panel.add(check_button);
button_panel.add(update_button);
button_panel.add(del_button);
button_panel.add(return_button);
//TestCombobox tt = new TestCombobox();
//Component cc = tt.addComponentPanel(acFrame.getContentPane());
main_panel.add(up_panel);
main_panel.add(name_panel);
main_panel.add(add_panel);
main_panel.add(tel_panel);
//main_panel.add(cc);
main_panel.add(cb_panel);
main_panel.add(button_panel);
// acFrame 属性设置
acFrame.add(main_panel);
acFrame.pack();
acFrame.setLocation(350, 250);
acFrame.setDefaultCloseOperation(acFrame.EXIT_ON_CLOSE);
acFrame.setResizable(false);
acFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/**
* 返回
*/
if(e.getActionCommand().equals("返回")){
new MainFrame(array);
acFrame.dispose();
}
// 接受ArrayList 索引数据
if (e.getActionCommand().equals("查找")) {
int f = 0;
int ci = array.size();
for (int i = 0; i < ci; i++) {
try {
Customer em = (Customer) array.get(i);
// System.out.println(em.getName());
// System.out.println("input name:"+name_tf.getText());
if (em.getB().getId().equals(bName_tf.getText())) {
f++;
bName_tf.setText(em.getB().getId());
length_tf.setText(em.getB().getLength());
//loca_tf.setText(em.getB());
cb_tf.setText(em.getName());
c_i = i;
System.out.println(c_i);
}
if (e.getActionCommand().equals("更新")) {
}
} catch (Exception ee) {
System.out.println(ee.getMessage());
}
}
if (f == 0) {
JOptionPane.showMessageDialog(acFrame, "没有找到你需要的数据,请重新查找",
"提示信息", JOptionPane.ERROR_MESSAGE);
bName_tf.setText("");
bName_tf.requestFocus(true);
}
}
// 数据更新
else if (e.getActionCommand().equals("更新")) {
//System.out.println("到这里了哦,更新");
System.out.println("ci" + c_i);
if (c_i > 0) {
System.out.println("到数据更新了");
//System.out.println("是第:" + c_i);
Customer ct = (Customer) array.get(c_i);
String id = new String(bName_tf.getText());
String length = new String(length_tf.getText());
String name = new String(ct.getName());
String tel = new String(ct.getTel());
String pwd = new String(ct.getPwd());
String add = new String(ct.getAddress());
cust = new Customer(id, length, name, tel, pwd, add);
array.set(c_i, cust);
Customer rr = (Customer)array.get(c_i);
System.out.println("船位"+rr.getB().getId());
JOptionPane.showMessageDialog(acFrame, "数据成功更新!", "提示信息",
JOptionPane.ERROR_MESSAGE);
}
/**
* 删除数据!
*/
} else if (e.getActionCommand().equals("删除")) {
array.remove(c_i);
JOptionPane.showMessageDialog(acFrame, "数据成功删除!", "提示信息",
JOptionPane.ERROR_MESSAGE);
bName_tf.setText("");
bName_tf.requestFocus(true);
length_tf.setText("");
loca_tf.setText("");
}
}
public static void main(String[] args) {
// new AddBoatFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -