项目源码 :https://github.com/Wo-com/ShipPort
如果觉得不错的话就在GitHub里面给个Star吧
JavaSwing 船只停靠管理可视化,功能选项卡实现,通过继承JPanel添加功能。
MainUI 为项目框架。Pane为选项卡,选项卡实现具体的功能。
项目界面结构:
Mainui 源代码:
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);//清空布局
}
}
Pane1效果图:实现对泊位的增删改查
Pane1源码:
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);
}
});
}
}
Pane2 效果图:实现对船只的增删改查。
Pane2源码:
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);
}
});
}
}
Pane3效果图:实现对船只的自动指定和手动指定。
Pane3 源码:
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();
boolean tf= hand.auto\_appoint(shipid);
if (tf){
JOptionPane.showMessageDialog(null,"指定位置:"+shipid+"成功","指定位置",JOptionPane.PLAIN\_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"指定位置:"+shipid+"失败","指定位置",JOptionPane.PLAIN\_MESSAGE);
}
}
}
}
);
button\_4.addActionListener(new ActionListener()//帮助
{
public void actionPerformed(ActionEvent e4)
{
JOptionPane.showMessageDialog(null,"手动指定位置,要输入船只ID和泊位号,手动指定可能导致时间冲突。\\n 自动指定输入ID即可 自动指定 不会出现时间冲突","指定位置",JOptionPane.PLAIN\_MESSAGE);
}
}
);
int between=11;
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);
lblid.setBounds(width/between\*2, 10, width/between, 20);
textField.setBounds(width/between\*3, 10, width/between\*2, 20);
lblid\_1.setBounds(width/between\*6, 10, width/between, 20);
textField\_1.setBounds(width/between\*7, 10, width/between\*2, 20);
button.setBounds(width/between\*1, 40, 60, 20);
button\_1.setBounds(width/between\*3,40, 60, 20);
button\_2.setBounds(width/between\*5, 40, 60, 20);
button\_3.setBounds(width/between\*7, 40, 60, 20);
button\_4.setBounds(width/between\*9, 40, 60, 20);
splitPane\_1.setDividerLocation(width/11\*7); //垂直条 黄金比例
splitPane.setBounds(0, 0, width-20, height-90);
}
});
}
}
Pane4 效果图:应用java实现甘特图
Pane4 源码:
import java.awt.Color;
import java.awt.Dimension;
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.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import consql.Dao;
import tool.DateFormat;
public class Pane4 extends JPanel{
private static final long serialVersionUID = 1L;
Pane4(){
System.out.println("面板4被调用");
initialize();
}
private void initialize() {
Dao db = new Dao();
db.connSQL();
DateFormat da=new DateFormat();
JLabel label = new JLabel("起始时间");
label.setBounds(20, 16, 61, 16);
this.add(label);
JTextField textField = new JTextField();
textField.setBounds(93, 11, 130, 26);
this.add(textField);
textField.setColumns(10);
JLabel label\_1 = new JLabel("结束时间");
label\_1.setBounds(235, 16, 61, 16);
this.add(label\_1);
JTextField textField\_1 = new JTextField();
textField\_1.setBounds(291, 11, 130, 26);
this.add(textField\_1);
textField\_1.setColumns(10);
JButton btnNewButton = new JButton("查询");
btnNewButton.setBounds(423, 11, 50, 29);
this.add(btnNewButton);
JButton btn = new JButton("帮助");
btn.setBounds(473, 11, 50, 29);
this.add(btn);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(17, 60, 404, 185);
this.add(scrollPane);
btnNewButton.addActionListener(new ActionListener()//查询
{
public void actionPerformed(ActionEvent e4)
{
JPanel panel = new JPanel();
String text1=textField.getText();
String text2=textField\_1.getText();
String min\_time=null;
String max\_time=null;
int line\_height=35; //行高
int star=100; //时段图 左距
try{//绘制 每条港口信息
String min\_sql="select min(arrive) from portship;";
String max\_sql="select max(leaves) from portship;";
ResultSet rs\_min=db.selectSQL(min\_sql);
if(rs\_min.first()) { //第一行
min\_time=rs\_min.getString(1);
System.out.println("最小时间"+rs\_min.getString(1));
}
ResultSet rs\_max=db.selectSQL(max\_sql);
if(rs\_max.first()) {
max\_time=rs\_max.getString(1); //第一行
System.out.println("最大时间"+rs\_max.getString(1));
}
int width=da.dateDiff(min\_time, max\_time)+star; //绘图窗口宽度
String sql1 = "select \* from port";
ResultSet rs1=db.selectSQL(sql1);
rs1.last() ; int portrow = rs1.getRow(); rs1.beforeFirst();//光标回滚 获取行数 光标回滚
panel.setPreferredSize(new Dimension(width, portrow\*line\_height));//设置宽度显示滚动条
panel.setLayout(null);
while(rs1.next()){
String name=rs1.getString(2); //取得数据表里面的 name(泊位名)
int line=rs1.getInt(3); //取得数据表里面的 num\_add (行号) 行号用于绘图
JLabel lblOne = new JLabel(name);
lblOne.setForeground(new Color(0, 0, 0));
lblOne.setBounds(18, line\*line\_height-10, 61, 16); //整体往下移动10的距离
panel.add(lblOne);
}
//绘制空的填充 防止查询为空 遗留数据显示
JEditorPane editorPane1 = new JEditorPane();
editorPane1.setEditable(false);
editorPane1.setBackground(new Color(238,238,238));
editorPane1.setBounds(star,line\_height-13, width-star, portrow\*line\_height-13);
panel.add(editorPane1,3);
String sql3 = null;
if((text1.length()==0)&&(text2.length()!=0)){//\[ ,y\] 查询起始到 y时段段
sql3="select \* from portship where leaves<='"+text2+"'";
System.out.println("\[ ,y\]");
textField.setText(min\_time);
}else if((text1.length()!=0)&&(text2.length()==0)){//\[x,\] 查询x到 结束时段段
sql3="select \* from portship where arrive>='"+text1+"'";
System.out.println("\[x, \]");
textField\_1.setText(max\_time);
}else if((text1.length()!=0)&&(text2.length()!=0)){//\[x,y\] 查询x到 y时段段
sql3="select \* from portship where arrive>='"+text1+"'and leaves<='"+text2+"'";
System.out.println("\[x,y\]");
//设置文本框内容
}else if((text1.length()==0)&&(text2.length()==0)){
sql3="select \* from portship";
textField.setText(min\_time); //设置文本框内容
textField\_1.setText(max\_time); //设置文本框内容
}
System.out.println("sql "+sql3);
ResultSet rs3=db.selectSQL(sql3);
while(rs3.next()){ //绘制甘特图
String artime=rs3.getString(3);
String lvtime=rs3.getString(4);
int port=rs3.getInt(5);
int hour=da.dateDiff(artime, lvtime);//长度
int dwstr=da.dateDiff(min\_time, artime);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setBackground(new Color(0, 191, 255));
editorPane.setBounds(star+dwstr, port\*line\_height-13, hour, 20);
panel.add(editorPane,3);//放在后面第一层
}
scrollPane.setViewportView(panel);//绘制完成再添加,不然就会不显示
}catch(Exception sss){
System.out.println("查询港口名Sql出错");
}
}
}
);
btn.addActionListener(new ActionListener()//帮助监听
{
public void actionPerformed(ActionEvent e4)
{
JOptionPane.showMessageDialog(null,"无输入查询 \[min,max\]\\n输入起始时间查询 \[ x , max\]\\n输入结束时间查询 \[min, y \]\\n输入起始和结束时间查询 \[ x , y \]\\n","指定位置",JOptionPane.PLAIN\_MESSAGE);
}
}
);
int between=15;
this.addComponentListener(new ComponentAdapter() {//拖动窗口监听
public void componentResized(ComponentEvent e) {
int width=MainUI.frame.getWidth(); //获取窗口宽度
int height=MainUI.frame.getHeight(); //获取窗口高度
scrollPane.setBounds(0, 40, width-23, height-140);
label.setBounds(width/between\*1, 10, width/between, 20);
textField.setBounds(width/between\*2, 10, width/between\*3, 20);
label\_1.setBounds(width/between\*5, 10, width/between, 20);
textField\_1.setBounds(width/between\*6, 10, width/between\*3, 20);
btnNewButton.setBounds(width/between\*9, 10, width/between\*2, 20);
btn.setBounds(width/between\*11, 10, width/between\*2, 20);
}
});
}
}
界面部分介绍完成,接下来介绍工具类 JavaSwing 船只停靠管理可视化(四)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章