用户界面(Here's our GUI)


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;            
import java.awt.event.*;      
import java.io.*;
import java.io.IOException;
import java.sql.*;
import java.rmi.*;

public class GUI  implements ActionListener, WindowListener{

        protected JFrame frame;
       
        protected Color steelBlue=new Color(130,202,255);
        protected Color golden=new Color(240,176,23);
        protected Font f1=new Font("Times New Roman", Font.BOLD,20);
        protected Font f2=new Font("Calibri",Font.PLAIN,16);

        protected JButton log=null,exit=null;
        protected JTextPane textPane=null;

        private File file=new File("D://CPE207//Systemlog.txt");

        protected PreparedStatement psmnt=null;

        PrintStream p=null;

       // protected JTable table=null;
       


    public GUI(PrintStream p, JEditorPane editorPane,JTable table) throws FileNotFoundException{
        this.p=p;
       createGUI(p,editorPane,table);
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        p.println(textPane.getText());
        p.close();
       
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==exit){
            p.println(textPane.getText());
            p.close();
           
            System.exit(0);
        }
        else if(e.getSource()==log){
            p.println(textPane.getText());
            p.close();
        }
       
    }
  
    public void createGUI(PrintStream p,JEditorPane editorPane,JTable table){
        JPanel gui=new JPanel();

        gui.setLayout(new BorderLayout());
        gui.setBackground(Color.RED);

        //put the logo at the right corner
        final ImageIcon img= new ImageIcon("mcdonald.jpg");
        final JPanel logoField = new JPanel(){
            public void paintComponent(Graphics g) {
                g.drawImage(img.getImage(), 10, 10, 200, 200, null);
            }
        };

        logoField.setFont(f1);
        logoField.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("MacDonald Software Inc. Database-PBSS"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
       //show table Bus.
        JTextField Bus = new JTextField(10);
        Bus.setEditable(false);
        Bus.setText("100");

        //show table Map
        JTextField Map = new JTextField(10);
        Map.setEditable(false);

        //show table Route
        JTextField Route = new JTextField(10);
        Route.setEditable(false);

        //show number of tables
        JTextField numTables=new JTextField(10);
        numTables.setEditable(false);

        //names for the tables.
        JLabel BusLabel = new JLabel("Bus: ");
        BusLabel.setFont(f2);
        BusLabel.setLabelFor(Bus);
        JLabel MapLabel = new JLabel("Map: ");
        MapLabel.setFont(f2);
        MapLabel.setLabelFor(Map);
        JLabel RouteLabel = new JLabel("Route: ");
        RouteLabel.setFont(f2);
        RouteLabel.setLabelFor(Route);

        //a label to show the total number of tables
        JLabel numTablesLabel = new JLabel("Total number of tables: ");
        numTablesLabel.setFont(f2);
        numTablesLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
        numTablesLabel.setLabelFor(numTables);

        //Lay out the text controls and the labels.
        JPanel tablesField = new JPanel();
        tablesField.setBackground(steelBlue);
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        tablesField.setLayout(gridbag);

        c.gridx=0;
        c.gridy=0;
        c.ipadx=10;
        c.ipady=10;
        c.fill=GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 1.0;
        c.weighty=10;
        JPanel BusTemp=new JPanel();
        BusTemp.add(BusLabel);
        BusTemp.add(Bus);

        //tablesField.add(BusTemp,c);

        c.gridy=1;
        JPanel MapTemp=new JPanel();
        MapTemp.add(MapLabel);
        MapTemp.add(Map);

        //tablesField.add(MapTemp,c);

        c.gridy=2;
        JPanel RouteTemp=new JPanel();
        RouteTemp.add(RouteLabel);
        RouteTemp.add(Route);

        //tablesField.add(RouteTemp,c);

        c.gridy=4;
        c.anchor = GridBagConstraints.PAGE_END;
        JPanel numTablesTemp=new JPanel();
        numTablesTemp.add(numTablesLabel);
        numTablesTemp.add(numTables);

        tablesField.add(table);
        tablesField.setFont(f1);
        tablesField.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Tables in Database"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));

    //create Detail field
        //create textField for entites---Route 1
        JTextField BusIDDetail=new JTextField(10);
        BusIDDetail.setEditable(false);
        BusIDDetail.setBackground(Color.WHITE);
        BusIDDetail.setFont(f2);

        JTextField CurPassDetail=new JTextField(10);
        CurPassDetail.setEditable(false);
        CurPassDetail.setBackground(Color.WHITE);
        CurPassDetail.setFont(f2);

        JTextField CurXDetail=new JTextField(10);
        CurXDetail.setEditable(false);
        CurXDetail.setBackground(Color.WHITE);
        CurXDetail.setFont(f2);

        JTextField CurYDetail=new JTextField(10);
        CurYDetail.setEditable(false);
        CurYDetail.setBackground(Color.WHITE);
        CurYDetail.setFont(f2);

        JTextField CurZDetail=new JTextField(10);
        CurZDetail.setEditable(false);
        CurZDetail.setBackground(Color.WHITE);
        CurZDetail.setFont(f2);

        //create labels for entities
        JLabel BusIDLabelDetail=new JLabel("Bus ID: ");
        BusIDLabelDetail.setLabelFor(BusIDDetail);
        BusIDLabelDetail.setFont(f2);

        JLabel CurPassLabelDetail=new JLabel("Number of current passengers: ");
        CurPassLabelDetail.setLabelFor(CurPassDetail);
        CurPassLabelDetail.setFont(f2);

        JLabel CurXLabelDetail=new JLabel("     X: ");
        CurXLabelDetail.setLabelFor(CurXDetail);
        CurXLabelDetail.setFont(f2);

        JLabel CurYLabelDetail=new JLabel("                           Y: ");
        CurYLabelDetail.setLabelFor(CurYDetail);
        CurYLabelDetail.setFont(f2);

        JLabel CurZLabelDetail=new JLabel("Z: ");
        CurZLabelDetail.setLabelFor(CurZDetail);
        CurZLabelDetail.setFont(f2);

        JPanel Route1Detail=new JPanel();
        Route1Detail.setBackground(Color.WHITE);
        Route1Detail.setLayout(new GridBagLayout());

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx=0;
        c2.gridy=0;
        c2.ipadx=0;
        c2.ipady=0;
        c2.gridwidth=1;
        c2.fill=GridBagConstraints.HORIZONTAL;
        c2.anchor = GridBagConstraints.LINE_END;
        c2.weightx = 0;
        c2.weighty=5;

        Route1Detail.add(BusIDLabelDetail,c2);
        c2.gridx=1;
        c2.anchor = GridBagConstraints.LINE_START;
        Route1Detail.add(BusIDDetail,c2);
        c2.gridx=5;
        c2.anchor = GridBagConstraints.LINE_END;
        c2.gridwidth=2;
        Route1Detail.add(CurPassLabelDetail,c2);
        c2.gridx=7;
        c2.gridwidth=1;
        c2.anchor = GridBagConstraints.LINE_START;
        Route1Detail.add(CurPassDetail,c2);

        c2.gridy=1;
        c2.gridx=0;
        c2.anchor = GridBagConstraints.LINE_END;
        Route1Detail.add(CurXLabelDetail,c2);
        c2.gridx=1;
        c2.anchor = GridBagConstraints.LINE_START;
        Route1Detail.add(CurXDetail,c2);
        c2.gridx=5;
        c2.anchor = GridBagConstraints.LINE_END;
        Route1Detail.add(CurYLabelDetail,c2);
        c2.gridx=6;
        c2.anchor = GridBagConstraints.LINE_START;
        Route1Detail.add(CurYDetail,c2);
        c2.gridx=10;
        c2.anchor = GridBagConstraints.LINE_END;
        Route1Detail.add(CurZLabelDetail,c2);
        c2.gridx=11;
        c2.anchor = GridBagConstraints.LINE_START;
        Route1Detail.add(CurZDetail,c2);
        Route1Detail.setFont(f2);
        Route1Detail.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Route 1: "),
                                BorderFactory.createEmptyBorder(5,5,5,5)));

        JTextField BusIDDetail2=new JTextField(10);
        BusIDDetail2.setEditable(false);
        BusIDDetail2.setBackground(Color.white);
        BusIDDetail2.setFont(f2);

        JTextField CurPassDetail2=new JTextField(10);
        CurPassDetail2.setEditable(false);
        CurPassDetail2.setBackground(Color.WHITE);
        CurPassDetail2.setFont(f2);

        JTextField CurXDetail2=new JTextField(10);
        CurXDetail2.setEditable(false);
        CurXDetail2.setBackground(Color.WHITE);
        CurXDetail2.setFont(f2);

        JTextField CurYDetail2=new JTextField(10);
        CurYDetail2.setEditable(false);
        CurYDetail2.setBackground(Color.WHITE);
        CurYDetail2.setFont(f2);

        JTextField CurZDetail2=new JTextField(10);
        CurZDetail2.setEditable(false);
        CurZDetail2.setBackground(Color.WHITE);
        CurZDetail2.setFont(f2);

        //create labels for entities
        JLabel BusIDLabelDetail2=new JLabel("Bus ID: ");
        BusIDLabelDetail2.setLabelFor(BusIDDetail2);
        BusIDLabelDetail2.setFont(f2);

        JLabel CurPassLabelDetail2=new JLabel("Number of current passengers: ");
        CurPassLabelDetail2.setLabelFor(CurPassDetail2);
        CurPassLabelDetail2.setFont(f2);

        JLabel CurXLabelDetail2=new JLabel("     X: ");
        CurXLabelDetail2.setLabelFor(CurXDetail2);
        CurXLabelDetail2.setFont(f2);

        JLabel CurYLabelDetail2=new JLabel("                           Y: ");
        CurYLabelDetail2.setLabelFor(CurYDetail2);
        CurYLabelDetail2.setFont(f2);

        JLabel CurZLabelDetail2=new JLabel("Z: ");
        CurZLabelDetail2.setLabelFor(CurZDetail2);
        CurZLabelDetail2.setFont(f2);

        JPanel Route2Detail=new JPanel();
        Route2Detail.setBackground(Color.WHITE);
        Route2Detail.setLayout(new GridBagLayout());

        GridBagConstraints c2_2 = new GridBagConstraints();
        c2_2.gridx=0;
        c2_2.gridy=0;
        c2_2.ipadx=0;
        c2_2.ipady=0;
        c2_2.gridwidth=1;
        c2_2.fill=GridBagConstraints.HORIZONTAL;
        c2_2.anchor = GridBagConstraints.LINE_END;
        c2_2.weightx = 0;
        c2_2.weighty=5;

        Route2Detail.add(BusIDLabelDetail2,c2_2);
        c2_2.gridx=1;
        c2_2.anchor = GridBagConstraints.LINE_START;
        Route2Detail.add(BusIDDetail2,c2_2);
        c2_2.gridx=5;
        c2_2.anchor = GridBagConstraints.LINE_END;
        c2_2.gridwidth=2;
        Route2Detail.add(CurPassLabelDetail2,c2_2);
        c2_2.gridx=7;
        c2_2.gridwidth=1;
        c2_2.anchor = GridBagConstraints.LINE_START;
        Route2Detail.add(CurPassDetail2,c2_2);

        c2_2.gridy=1;
        c2_2.gridx=0;
        c2_2.anchor = GridBagConstraints.LINE_END;
        Route2Detail.add(CurXLabelDetail2,c2_2);
        c2_2.gridx=1;
        c2_2.anchor = GridBagConstraints.LINE_START;
        Route2Detail.add(CurXDetail2,c2_2);
        c2_2.gridx=5;
        c2_2.anchor = GridBagConstraints.LINE_END;
        Route2Detail.add(CurYLabelDetail2,c2_2);
        c2_2.gridx=6;
        c2_2.anchor = GridBagConstraints.LINE_START;
        Route2Detail.add(CurYDetail2,c2_2);
        c2_2.gridx=10;
        c2_2.anchor = GridBagConstraints.LINE_END;
        Route2Detail.add(CurZLabelDetail2,c2_2);
        c2_2.gridx=11;
        c2_2.anchor = GridBagConstraints.LINE_START;
        Route2Detail.add(CurZDetail2,c2_2);

        Route2Detail.setFont(f2);
        Route2Detail.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Route 2: "),
                                BorderFactory.createEmptyBorder(5,5,5,5)));


        editorPane.setFont(f2);
        editorPane.setBackground(Color.WHITE);
        editorPane.setText("Welcome to MacDonald Software Inc.--PBSS Database:/nDatabase Updating...");
        editorPane.setEditable(false);
        JScrollPane editorScrollPane = new JScrollPane(editorPane);
        editorScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));

        //Create a text pane.
        textPane = new JTextPane();
        textPane.setBackground(Color.WHITE);
        textPane.setEditable(true);
        textPane.setText("User remarks for log:");
        Font f3=new Font("Calibri",Font.ITALIC,18);
        textPane.setFont(f3);

        JScrollPane paneScrollPane = new JScrollPane(textPane);
        paneScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        paneScrollPane.setPreferredSize(new Dimension(250, 155));
        paneScrollPane.setMinimumSize(new Dimension(10, 10));

        //Put the editor pane and the text pane in a split pane.
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                              editorScrollPane,
                                              paneScrollPane);
        splitPane.setOneTouchExpandable(true);
        splitPane.setResizeWeight(0.9);

        log=new JButton("Log");
        log.addActionListener(this);
        log.setBackground(golden);
        log.setFont(f2);
        exit=new JButton("Exit");
        exit.addActionListener(this);
        exit.setBackground(golden);
        exit.setFont(f2);
        JPanel bottom=new JPanel();
        bottom.setBackground(steelBlue);
        bottom.add(log);
        bottom.add(exit);

        JPanel logField=new JPanel();
        logField.setBackground(steelBlue);
        logField.setLayout(new BorderLayout());
        logField.add(splitPane,BorderLayout.CENTER);
        logField.add(bottom,BorderLayout.SOUTH);


        logField.setBorder(
        BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder("Log: "),
                        BorderFactory.createEmptyBorder(5,5,5,5)));
        logField.setFont(f2);

        JPanel rightPane = new JPanel(new BorderLayout());

        rightPane.add(logField,BorderLayout.CENTER);

        //Put everything together.
        JPanel leftPane = new JPanel(new GridBagLayout());
         GridBagConstraints c1 = new GridBagConstraints();
         c1.gridx=0;
         c1.gridy=0;
         c1.gridheight=5;
         c1.ipady = 120;      //make this component tall
         c1.fill = GridBagConstraints.BOTH;
         leftPane.add(logoField,c1);

         c1.fill = GridBagConstraints.BOTH;
         c1.ipady = 0;       //reset to default
         c1.weighty = 1.0;   //request any extra vertical space
         c1.anchor = GridBagConstraints.CENTER; //bottom of space

         c1.gridx=0;
         c1.gridy=5;
         c1.gridwidth = 1;

        leftPane.add(tablesField,c1);

        gui.add(leftPane, BorderLayout.WEST);
        gui.add(rightPane, BorderLayout.CENTER);

        //Create and set up the window.
        frame = new JFrame("PBSS-Database Team");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(gui);

        frame.addWindowListener(this);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值