JavaSwing 船只停靠管理可视化(三)
2020-12-13 02:06
标签:form extends while 结构 before dao get htm tween JavaSwing 船只停靠管理可视化(一) JavaSwing 船只停靠管理可视化(二) JavaSwing 船只停靠管理可视化(三) JavaSwing 船只停靠管理可视化(四) JavaSwing 船只停靠管理可视化(五) 项目源码 :https://github.com/Wo-com/ShipPort 如果觉得不错的话就在GitHub里面给个Star吧 JavaSwing 船只停靠管理可视化,功能选项卡实现,通过继承JPanel添加功能。 MainUI 为项目框架。Pane为选项卡,选项卡实现具体的功能。 项目界面结构: Mainui 源代码: Pane1效果图:实现对泊位的增删改查 Pane1源码: Pane2 效果图:实现对船只的增删改查。 Pane2源码: Pane3效果图:实现对船只的自动指定和手动指定。 Pane3 源码:import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
public class MainUI {
public static JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MainUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("船只停靠管理可视化");
frame.setBounds(100, 100, 840, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JLabel label = new JLabel("欢迎使用,本管理系统,没有使用任何框架,界面可随意拖动,界面与数据操作分离方便修改");
panel.add(label);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
Pane1 panel_1 = new Pane1();
tabbedPane.addTab("泊位管理", null, panel_1, null);
panel_1.setLayout(null);//清空布局
Pane2 panel_2 = new Pane2();
tabbedPane.addTab("船只管理", null, panel_2, null);
panel_2.setLayout(null);//清空布局
Pane3 panel_3 = new Pane3();
tabbedPane.addTab("停靠指定", null, panel_3, null);
panel_3.setLayout(null);//清空布局
Pane4 panel_4 = new Pane4();
tabbedPane.addTab("查看甘特图", null, panel_4, null);
panel_4.setLayout(null);//清空布局
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import consql.Dao;
public class Pane1 extends JPanel{
private static final long serialVersionUID = 1L;
Pane1(){
System.out.println("面板1被调用");
initialize();
}
private void initialize() {
Dao db = new Dao();
db.connSQL();
JLabel lblNewLabel_1 = new JLabel("泊位ID");
lblNewLabel_1.setBounds(81, 32, 61, 16); //初始位置
this.add(lblNewLabel_1);
JTextField textField = new JTextField();
textField.setBounds(190, 27, 130, 26); //初始位置
this.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("泊位名称");
lblNewLabel_2.setBounds(81, 73, 61, 16); //初始位置
this.add(lblNewLabel_2);
JTextField textField_1 = new JTextField();
textField_1.setBounds(190, 65, 130, 26); //初始位置
this.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("添加");
btnNewButton.setBounds(44, 101, 55, 58); //初始位置
this.add(btnNewButton);
JButton btnNewButton_1 = new JButton("查看");
btnNewButton_1.setBounds(145, 103, 61, 58); //初始位置
this.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("删除");
btnNewButton_2.setBounds(259, 103, 61, 58); //初始位置
this.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("更改");
btnNewButton_3.setBounds(345, 65, 78, 29); //初始位置
this.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("帮助");
btnNewButton_4.setBounds(362, 101, 61, 58); //初始位置
this.add(btnNewButton_4);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 100, 570, 200);
// scrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //水平滚动条
// scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //垂直滚动条
this.add(scrollPane);
btnNewButton.addActionListener(new ActionListener()//添加按钮
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用文本框ID
String name = textField_1.getText(); // 取得用name
if((id.length()==0)||(name.length()==0)){
JOptionPane.showMessageDialog(null,"插入数据为空成功","插入数据失败",JOptionPane.PLAIN_MESSAGE);
}else{
//获取行数
String sqlline = "select * from port";
ResultSet rs = db.selectSQL(sqlline);
rs.last() ; int row = rs.getRow()+1; rs.beforeFirst();//光标回滚 获取行数 光标回滚
String sql1 = "insert into port(id,name,num_add) values("+id+",‘"+name+"‘,‘"+row+"‘)";
boolean tf;
tf=db.insertSQL(sql1);
if (tf){
JOptionPane.showMessageDialog(null,"插入id:"+id+" 泊位:"+name+" 成功","插入数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"插入id:"+id+" 泊位:"+name+" 失败","插入数据",JOptionPane.PLAIN_MESSAGE);
}
}
System.out.println("添加执行完成");
}catch(Exception e1){
System.out.println("面板1,查询出错");
}
}
}
);
btnNewButton_1.addActionListener(new ActionListener()//查询按钮
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用文本框ID
String[] columnNames = { "Id", "泊位名","泊位编号"};
String sql2;
if (id.length()==0){
sql2 = "select * from port";
}else{
sql2 = "select * from port where id=‘"+id+"‘";
}
ResultSet rs = db.selectSQL(sql2);
rs.last() ; int row = rs.getRow(); rs.beforeFirst();//光标回滚 获取行数 光标回滚
String data[][] =new String[row][3];
row=0;
while(rs.next()){
data[row][0]=rs.getString(1);
data[row][1]=rs.getString(2);
int num=rs.getInt(3);
data[row][2]=String.valueOf(num);
row++;
}
JTable table = new JTable(data, columnNames);
scrollPane.setViewportView(table);
System.out.println("查询"+row+"行,完成查询");
}catch(Exception e1){
System.out.println("面板1,查询出错");
}
}
}
);
btnNewButton_2.addActionListener(new ActionListener()//监听删除泊位
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText();// 取得用ID
String sql3 = "delete from port where id=‘"+id+"‘;";
boolean dl;
dl=db.deleteSQL(sql3);
if (dl){
JOptionPane.showMessageDialog(null,"删除id:"+id+"成功","删除数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"删除id:"+id+"失败","删除数据",JOptionPane.PLAIN_MESSAGE);
}
System.out.println("面板1 删除完成");
}catch(Exception e1){
System.out.println("面板1 删除出错");
}
}
}
);
btnNewButton_3.addActionListener(new ActionListener()// 数据修改
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用ID
String name = textField_1.getText(); // 取得用name
if (name.length()!=0){ //修改name
String sql = "update port set name=‘"+name+"‘ where id=‘"+id+"‘;";
boolean na;
na=db.updateSQL(sql);
if (na){
JOptionPane.showMessageDialog(null,"更改名字成功","更新数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"更改名字失败","更新数据",JOptionPane.PLAIN_MESSAGE);
}
}
System.out.print("面板1 更新完成");
}catch(Exception e1){
System.out.print("面板1 更新出错");
}
}
}
);
btnNewButton_4.addActionListener(new ActionListener()// 提示帮助
{
public void actionPerformed(ActionEvent e4)
{
try{
JOptionPane.showMessageDialog(null,"在查询时,ID文本框为空查询的是全部数据\n输入ID时查询的时一条数据","查询提示",JOptionPane.PLAIN_MESSAGE);
System.out.print("面板1 帮助完成");
}catch(Exception e1){
System.out.print("面板1 帮助出错");
}
}
}
);
int between=11; //定义分段
this.addComponentListener(new ComponentAdapter() {//拖动窗口监听
public void componentResized(ComponentEvent e) {
int width=MainUI.frame.getWidth(); //获取窗口宽度
int height=MainUI.frame.getHeight(); //获取窗口高度
lblNewLabel_1.setBounds(width/between*2, 10, width/between, 20);
textField.setBounds(width/between*3, 10, width/between*2, 20);
lblNewLabel_2.setBounds(width/between*6, 10, width/between, 20);
textField_1.setBounds(width/between*7, 10, width/between*2, 20);
btnNewButton.setBounds(width/between*1, 40, 60, 20);
btnNewButton_1.setBounds(width/between*3,40, 60, 20);
btnNewButton_2.setBounds(width/between*5, 40, 60, 20);
btnNewButton_3.setBounds(width/between*7, 40, 60, 20);
btnNewButton_4.setBounds(width/between*9, 40, 60, 20);
scrollPane.setBounds(0, 70, width-30,height-170);
}
});
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.ResultSet;
import java.sql.Timestamp;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import consql.Dao;
public class Pane2 extends JPanel{
private static final long serialVersionUID = 1L;
Pane2(){
System.out.println("面板2被调用");
initialize();
}
private void initialize() {
Dao db = new Dao();
db.connSQL();
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerLocation(300);
this.add(splitPane);
JSplitPane splitPane_1 = new JSplitPane();
splitPane_1.setDividerLocation(300);
//splitPane_1.setEnabled(false);//竖条 不可拖动
splitPane.setLeftComponent(splitPane_1);
JPanel panel = new JPanel();
splitPane_1.setLeftComponent(panel);
panel.setLayout(null);
JLabel lblid = new JLabel("船只ID");
lblid.setBounds(6, 25, 61, 16);
panel.add(lblid);
JLabel label = new JLabel("船只名称");
label.setBounds(6, 70, 61, 16);
panel.add(label);
JLabel label_1 = new JLabel("到达时间");
label_1.setBounds(6, 115, 61, 16);
panel.add(label_1);
JLabel label_2 = new JLabel("离开时间");
label_2.setBounds(6, 160, 61, 16);
panel.add(label_2);
JLabel label_3 = new JLabel("停靠偏好");
label_3.setBounds(6, 205, 61, 16);
panel.add(label_3);
JTextField textField = new JTextField();
textField.setBounds(80, 20, 200, 26);
panel.add(textField);
textField.setColumns(10);
JTextField textField_1 = new JTextField();
textField_1.setBounds(80, 65, 200, 26);
panel.add(textField_1);
textField_1.setColumns(10);
JTextField textField_2 = new JTextField();
textField_2.setBounds(80, 110, 200, 26);
panel.add(textField_2);
textField_2.setColumns(10);
JTextField textField_3 = new JTextField();
textField_3.setBounds(80, 155, 200, 26);
panel.add(textField_3);
textField_3.setColumns(10);
JTextField textField_4 = new JTextField();
textField_4.setBounds(80, 200, 200, 26);
panel.add(textField_4);
textField_4.setColumns(10);
JTextPane textPane = new JTextPane();
textPane.setText("提示:\n1、在查询时,ID文本框为空查询的是全部数据\n\t输入ID时查询的时一条数据 \n\n2、输入时间格式:2019-06-08 01:12:11");
textPane.setBounds(6, 238, 270, 100);
panel.add(textPane);
JScrollPane scrollPane = new JScrollPane();
splitPane_1.setRightComponent(scrollPane);
JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
JButton btnNewButton = new JButton("船只添加");
panel_2.add(btnNewButton);
JButton btnNewButton_1 = new JButton("船只删除");
panel_2.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("船只修改");
panel_2.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("船只查询");
panel_2.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("操作帮助");
panel_2.add(btnNewButton_4);
btnNewButton.addActionListener(new ActionListener()//添加
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用ID
String name = textField_1.getText(); // 取得用name
String arrive = textField_2.getText(); // 取得用arrive_time
String leave = textField_3.getText(); // 取得用leave_time
String perfer = textField_4.getText(); // 取得偏好
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss[]");
if((id.length()==0)||(name.length()==0)||(arrive.length()==0)||(leave.length()==0)||(perfer.length()==0)){
JOptionPane.showMessageDialog(null,"插入数据为空","插入数据失败",JOptionPane.PLAIN_MESSAGE);
}else{
Timestamp ar = java.sql.Timestamp.valueOf(arrive);
Timestamp lv = java.sql.Timestamp.valueOf(leave);
String sql1 = "insert into ship values(‘"+id+"‘,‘"+name+"‘,‘"+ar+"‘,‘"+lv+"‘,‘"+perfer+"‘,‘‘)";
boolean tf;
tf=db.insertSQL(sql1);
if (tf){
JOptionPane.showMessageDialog(null,"插入id:"+id+" 泊位:"+name+" 成功","插入数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"插入id:"+id+" 泊位:"+name+" 失败","插入数据",JOptionPane.PLAIN_MESSAGE);
}
}
System.out.print("添加");
}catch(Exception e1){
System.out.print("确认添加,出错");
}
}
}
);
btnNewButton_1.addActionListener(new ActionListener()//删除
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText();// 取得用ID
if (id.length()==0){//
JOptionPane.showMessageDialog(null,"输入为空","请输入数据",JOptionPane.PLAIN_MESSAGE);
}else{
String sql3 = "delete from ship where id=‘"+id+"‘;";
boolean dl;
dl=db.deleteSQL(sql3);
if (dl){
JOptionPane.showMessageDialog(null,"删除id:"+id+"成功","删除数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"删除id:"+id+"失败","删除数据",JOptionPane.PLAIN_MESSAGE);
}
}
System.out.print("面板2 删除");
}catch(Exception e1){
System.out.print("面板2 删除,出错");
}
}
}
);
btnNewButton_2.addActionListener(new ActionListener()//修改
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用ID
String name = textField_1.getText(); // 取得用name
String arrive = textField_2.getText(); // 取得用arrive_time
String leaves = textField_3.getText(); // 取得用leave_time
String perfer = textField_4.getText(); // 取得偏好
System.out.print("id="+id+"name="+name+"arrive="+arrive+"leave"+leaves+"perfer"+perfer);
//2012-12-12 01:12:11
if (name.length()!=0){//修改name
System.out.print("--name修改--");
String sql = "update ship set name=‘"+name+"‘ where id=‘"+id+"‘;";
boolean na;
na=db.updateSQL(sql);
if (na){
JOptionPane.showMessageDialog(null,"更改名字成功","更新数据",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"更改名字失败","更新数据",JOptionPane.PLAIN_MESSAGE);
}
}
if (arrive.length()!=0){//修改到达时间
System.out.print("--arrive修改--");
String sql ="update ship set arrive=‘"+arrive+"‘ where id=‘"+id+"‘;";
db.updateSQL(sql);
}
//为什么不用leave字段! leave字段不能更新数据库
if (leaves.length()!=0){//修改leave时间
System.out.print("--leaves修改--");
String sql ="update ship set leaves=‘"+leaves+"‘ where id=‘"+id+"‘;";
db.updateSQL(sql);
}
if (perfer.length()!=0){//修改偏好位置
System.out.print("--prefer修改--");
String sql = "update ship set perfer=‘"+perfer+"‘ where id=‘"+id+"‘;";
db.updateSQL(sql);
}
System.out.print("面板2 修改");
}catch(Exception e1){
System.out.print("面板2 修改,出错");
}
}
}
);
btnNewButton_3.addActionListener(new ActionListener()//查询按钮
{
public void actionPerformed(ActionEvent e4)
{
try{
String id = textField.getText(); // 取得用ID
String[] columnNames = { "Id", "泊位名","到达时间","离开时间","偏好位置","停靠位置"};
String sql2;
if (id.length()==0){
sql2 = "select * from ship";
}else{
sql2 = "select * from ship where id=‘"+id+"‘";
}
ResultSet rs = db.selectSQL(sql2);
rs.last() ; int row = rs.getRow(); rs.beforeFirst();//光标回滚 获取行数 光标回滚
String data[][] =new String[row][6];
row=0;
while(rs.next()){
data[row][0]=rs.getString(1);
data[row][1]=rs.getString(2);
data[row][2]=rs.getString(3);
data[row][3]=rs.getString(4);
data[row][4]=rs.getString(5);
data[row][5]=rs.getString(6);
row++;
}
JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(0).setPreferredWidth(30);//设置列宽度比例
table.getColumnModel().getColumn(1).setPreferredWidth(30);
table.getColumnModel().getColumn(2).setPreferredWidth(120);
table.getColumnModel().getColumn(3).setPreferredWidth(120);
table.getColumnModel().getColumn(4).setPreferredWidth(30);
table.getColumnModel().getColumn(5).setPreferredWidth(30);
scrollPane.setViewportView(table);
System.out.println("面板2查询ship"+row+"行,完成查询");
}catch(Exception e1){
System.out.println("面板2查询ship,查询出错");
}
}
}
);
btnNewButton_4.addActionListener(new ActionListener()//帮助
{
public void actionPerformed(ActionEvent e4)
{
try{
JOptionPane.showMessageDialog(null,"在修改数据时 ID不能为空,其他数据可以选填","修改提示",JOptionPane.PLAIN_MESSAGE);
System.out.print("面板2 帮助");
}catch(Exception e1){
System.out.print("面板2 帮助,出错");
}
}
}
);
this.addComponentListener(new ComponentAdapter() {//拖动窗口监听
public void componentResized(ComponentEvent e) {
int width=MainUI.frame.getWidth(); //获取窗口宽度
int height=MainUI.frame.getHeight(); //获取窗口高度
splitPane.setBounds(0, 0, width-20, height);
splitPane.setDividerLocation(height-140);
}
});
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import consql.Dao;
import tool.Appoint;
public class Pane3 extends JPanel{
private static final long serialVersionUID = 1L;
Pane3(){
System.out.println("面板3被调用");
initialize();
}
private void initialize() {
Dao db = new Dao();
db.connSQL();
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerLocation(80);//水平条
this.add(splitPane, BorderLayout.CENTER);
JSplitPane splitPane_1 = new JSplitPane();
splitPane.setRightComponent(splitPane_1);
JPanel panel = new JPanel();
splitPane.setLeftComponent(panel);
panel.setLayout(null);
JLabel lblid = new JLabel("船只ID");
lblid.setBounds(6, 19, 61, 16);
panel.add(lblid);
JLabel lblid_1 = new JLabel("泊位号");
lblid_1.setBounds(237, 19, 61, 16);
panel.add(lblid_1);
JTextField textField = new JTextField();
textField.setBounds(74, 14, 130, 26);
panel.add(textField);
textField.setColumns(10);
JTextField textField_1 = new JTextField();
textField_1.setBounds(310, 14, 130, 26);
panel.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("查询船只");
button.setBounds(6, 58, 102, 29);
panel.add(button);
JButton button_1 = new JButton("查询港口");
button_1.setBounds(120, 58, 117, 29);
panel.add(button_1);
JButton button_2 = new JButton("手动指定");
button_2.setBounds(247, 58, 117, 29);
panel.add(button_2);
JButton button_3 = new JButton("自动指定");
button_3.setBounds(366, 58, 117, 29);
panel.add(button_3);
JButton button_4 = new JButton("指定帮助");
button_3.setBounds(366, 58, 117, 29);
panel.add(button_4);
JScrollPane scrollPane = new JScrollPane();//添加带滚动条的容器在左下
splitPane_1.setLeftComponent(scrollPane);
JScrollPane scrollPane_1 = new JScrollPane();//添加带滚动条的容器在右下
splitPane_1.setRightComponent(scrollPane_1);
button.addActionListener(new ActionListener() {//查询船只
public void actionPerformed(ActionEvent e) {
try{
String id = textField.getText(); // 取得用ID
String[] columnNames = { "Id", "泊位名","到达时间","离开时间","偏好位置","停靠位置"};
String sql2;
if (id.length()==0){
sql2 = "select * from ship";
}else{
sql2 = "select * from ship where id=‘"+id+"‘";
}
ResultSet rs = db.selectSQL(sql2);
rs.last() ; int row = rs.getRow(); rs.beforeFirst();//光标回滚 获取行数 光标回滚
String data[][] =new String[row][6];
row=0;
while(rs.next()){
data[row][0]=rs.getString(1);
data[row][1]=rs.getString(2);
data[row][2]=rs.getString(3);
data[row][3]=rs.getString(4);
data[row][4]=rs.getString(5);
data[row][5]=rs.getString(6);
row++;
}
JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(0).setPreferredWidth(25);//设置列宽度比例
table.getColumnModel().getColumn(1).setPreferredWidth(25);
table.getColumnModel().getColumn(2).setPreferredWidth(120);
table.getColumnModel().getColumn(3).setPreferredWidth(120);
table.getColumnModel().getColumn(4).setPreferredWidth(20);
table.getColumnModel().getColumn(5).setPreferredWidth(20);
scrollPane.setViewportView(table); //将表格添加到容器
System.out.println("面板3查询ship"+row+"行,完成查询");
}catch(Exception e1){
System.out.println("面板3查询ship,查询出错");
}
}
});
button_1.addActionListener(new ActionListener() {//查询港口
public void actionPerformed(ActionEvent e) {
try{
String id = textField_1.getText(); // 取得用ID
String[] columnNames = { "泊位名","泊位号"};
String sql2;
if (id.length()==0){
sql2 = "select * from port";
}else{
sql2 = "select * from ship where id=‘"+id+"‘";
}
ResultSet rs = db.selectSQL(sql2);
rs.last() ; int row = rs.getRow(); rs.beforeFirst();//光标回滚 获取行数 光标回滚
String data[][] =new String[row][2];
row=0;
while(rs.next()){
data[row][0]=rs.getString(2);
int num=rs.getInt(3);
data[row][1]=String.valueOf(num);
row++;
}
JTable table = new JTable(data, columnNames);
scrollPane_1.setViewportView(table); //将表格添加到容器
System.out.println("面板3查询port"+row+"行,完成查询");
}catch(Exception e1){
System.out.println("面板3查询port,查询出错");
}
}
});
button_2.addActionListener(new ActionListener()//手动指定
{
public void actionPerformed(ActionEvent e4)
{
String shipid = textField.getText(); // 取得用ID
String portid = textField_1.getText();
if ((shipid.length()==0)||(portid.length()==0)){//
JOptionPane.showMessageDialog(null,"输入为空","请输入ID",JOptionPane.PLAIN_MESSAGE);
}else{
Appoint hand=new Appoint();
boolean tf= hand.human_appoint(shipid, portid);
if (tf){
JOptionPane.showMessageDialog(null,"指定位置:"+shipid+"成功","指定位置",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"指定位置:"+shipid+"失败","指定位置",JOptionPane.PLAIN_MESSAGE);
}
}
}
}
);
button_3.addActionListener(new ActionListener()//自动指定
{
public void actionPerformed(ActionEvent e4)
{
String shipid = textField.getText(); // 取得用ID
if (shipid.length()==0){//
JOptionPane.showMessageDialog(null,"输入为空","请输入ID",JOptionPane.PLAIN_MESSAGE);
}else{
Appoint hand=new Appoint();