主要代码如下:public class GUI extends JFrame implements UpdateControl {
    
    public GUI(MutiNodes mNodeValue){
        
        this.mNode = mNodeValue;
        
        self = this;
        saveFileDialog = new JFileChooser(this.getClass().getResource("/").toString().substring(6));
        
        runt = Runtime.getRuntime();
        
        
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setResizable(false);
        setJMenuBar(menuBar);
        setTitle("SULS 1.0");
        setLocation(250,50);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        store = new JButton("Store");
        store.setMnemonic('S');
        store.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {
                cal   =   Calendar.getInstance();
                d = cal.getTime();
                
                String name = null;
                int i =0;
                try{
                    database.InitData();
                    while(i < mNode.getNameList().size()){
                        name = (String)mNode.getNameList().get(i);                      
                        database.StoreData(mNode.getNode(name),d.toLocaleString());
                        i++;
                    }
                }catch(Exception ea){
                     ea.printStackTrace();
                }
                
                ScreenCapture scp = new ScreenCapture(getX()+182,getY()+55,756,607);
                paint();
                scp.captureScreen();
                
                try {
                    String command = "re.exe";
                    Process process = runt.exec(command);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
             }
            });
    
        update = new JButton("Update");
        update.setMnemonic('U');
        update.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {
                paint();
                nodeTree = new JTree(GetTreeNode.getTree(mNode));
                nodeTree.expandRow(1);
                nodeTreePane.getViewport().add(nodeTree);
            }
        });  
        saveDataItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int val = saveFileDialog.showSaveDialog(self);
                File f = saveFileDialog.getSelectedFile();
                if(f!= null){
                    try {
                        OutputStream os = new FileOutputStream(f);
                        os.write(text.getText().getBytes());
                    } catch (IOException e1) {
                        // TODO 自动生成 catch 块
                        e1.printStackTrace();
                    }               
                }
            }
        });
        
        saveImgItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int val = saveFileDialog.showSaveDialog(self);
                File f = saveFileDialog.getSelectedFile();
                if(f!= null){
                    ScreenCapture scp = new ScreenCapture(getX()+182,getY()+55,756,607);  
                    paint();  
                    scp.captureScreen(f); 
                }        
            }         
        });
      
        exitItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_X,InputEvent.CTRL_MASK));
        exitItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(optionPane.showConfirmDialog(self,"Sure to quit ?","Confirmation",JOptionPane.WARNING_MESSAGE)==0){
                    System.exit(0);
                }
            } 
        });
        
        setMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_P,InputEvent.CTRL_MASK));
        setMenuItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String comm = optionPane.showInputDialog(self , "Set coordinate of node ( Named: new1 Coordinate:0,0,0 ).\n" +
                    "e.g. P PC new1 0 0 0","Setting",JOptionPane.QUESTION_MESSAGE);
                if(comm != null){
                    try {
                        serialOut.write(new String(comm+"\r\n").getBytes());
                        paint();
                    } catch (IOException e1) {
                        // TODO 自动生成 catch 块
                        e1.printStackTrace();
                    }
                }
            }
        });
        
        getDataItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_D,InputEvent.CTRL_MASK));;
        getDataItem.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成方法存根
                
                isGetData = !isGetData;
                
            }});
        
        helpItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    String dir = this.getClass().getResource("/").toString().substring(6);
                    Process process = runt.exec("notepad "+dir+"readme.txt");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }     
        });
        
        aboutItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                optionPane.showMessageDialog(self , "Self-Update Localization System\n" +
                 "Edition:1.0\n" +
                 "(c) 2008 Northwestern Polytechnical University WSN Lab","About",JOptionPane.INFORMATION_MESSAGE);
                self.repaint();
            }
        });
        
        zoomInItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_I,InputEvent.CTRL_MASK));;
        zoomInItem.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成方法存根                        
                zoom +=0.5;
                if(zoom>2.0)
                    zoom=2.0;
            }
            
        });
        
        zoomOutItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_O,InputEvent.CTRL_MASK));;
        zoomOutItem.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成方法存根
                zoom -=0.5;
                if(zoom <1.0)
                    zoom=1.0;
            }
            
        });
        
       

解决方案 »

  1.   

    上面代码没有贴完,接下来是:
             setMenuItem.setBackground(Color.WHITE);
            getDataItem.setBackground(Color.WHITE);
            startMenu.add(setMenuItem);
            startMenu.addSeparator();
            startMenu.add(getDataItem);
            startMenu.setMnemonic('T');           saveDataItem.setBackground(Color.WHITE);
            saveImgItem.setBackground(Color.WHITE);
            exitItem.setBackground(Color.WHITE);
            fileMenu.add(saveDataItem);
            fileMenu.add(saveImgItem);
            fileMenu.addSeparator();
            fileMenu.add(exitItem);
            fileMenu.setMnemonic('F');
            
            zoomInItem.setBackground(Color.WHITE);
            zoomOutItem.setBackground(Color.WHITE);
            viewMenu.add(zoomInItem);
            viewMenu.add(zoomOutItem);
            viewMenu.setMnemonic('V');
            
            helpItem.setBackground(Color.WHITE);
            aboutItem.setBackground(Color.WHITE);
            helpMenu.add(helpItem);
            helpMenu.addSeparator();
            helpMenu.add(aboutItem);
            helpMenu.setMnemonic('H');
            
            menuBar.add(fileMenu);
            menuBar.add(startMenu);
            menuBar.add(viewMenu);
            menuBar.add(helpMenu);
            
            text = new JTextArea("--- " Please check the nodes first and search the detail here .      \n" +
             "-----\n", 6, 68);
            text.setBackground(Color.PINK);
            text.setEditable(false);            nodeTree = new JTree(GetTreeNode.getTree(mNode));
            nodeTreePane = new JScrollPane();
            nodeTreePane.getViewport().add(nodeTree);        knownListener = new JTextField("",6);
            knownBeacon = new JTextField("",6);
            beacon = new JLabel("Beacon ");
            listener = new JLabel("Listener ");        buttonPane = new JPanel();
            buttonPane.setLayout(new GridLayout(3,2,20,10));
            buttonPane.add(beacon);
            buttonPane.add(knownBeacon);
            buttonPane.add(listener);
            buttonPane.add(knownListener);
            buttonPane.add(update);
            buttonPane.add(store);
            buttonPane.setBackground(Color.LIGHT_GRAY);
            
            drawpane = new JPanel();
            drawpane.setSize(760,400);
            drawpane.setBackground(Color.WHITE);
            paneBack.setIcon(new ImageIcon("img.jpg"));
            drawpane.setBorder(new LineBorder(Color.LIGHT_GRAY,5));
            pane = new JScrollPane(text);
            pane.setBorder(new LineBorder(Color.GRAY,2));
            
            infopane = new JPanel();
            infopane.setBackground(Color.LIGHT_GRAY);
            infopane.setBorder(new LineBorder(Color.GRAY,0));
            infopane.add(pane,BorderLayout.CENTER);
            infopane.add(buttonPane,BorderLayout.EAST);
            
            Container contentPanel = getContentPane();
            contentPanel.setBackground(Color.LIGHT_GRAY);
            contentPanel.add(nodeTreePane,BorderLayout.WEST);
            contentPanel.add(drawpane, BorderLayout.CENTER);
            contentPanel.add(infopane, BorderLayout.SOUTH);
            
            this.setVisible(true);
                }
        
          public void newMsg(SignNode signNode) {
            if(signNode.getMode().equalsIgnoreCase("L")){
                text.append("Node : ID="+signNode.getSpaceID()+" , MD="+signNode.getMode()
                        +" , CD=("+signNode.getX()+","
                        +signNode.getY()+","
                        +signNode.getZ()+")"+" , "
                        //+signNode.getData().toString()
                        +"\n");
                text.setCaretPosition(text.getText().length());
            }
            
            mNode.storeNode(signNode);
                 paint();
            
        }
        
        private void paint(){
            
            int beaconNum = mNode.getModeNum("B");
            int listenerNum = mNode.getModeNum("L");
            double radius = 7;
            
            knownBeacon.setText(new Integer(beaconNum).toString());
            knownListener.setText(new Integer(listenerNum).toString());
            
            Graphics2D g = (Graphics2D) drawpane.getGraphics();
            Rectangle2D rect = new Rectangle2D.Double(5, 5, 750, 600);
            g.setPaint(Color.GRAY);
            g.draw(rect);

            Rectangle2D recttem = new Rectangle2D.Double(5, 5, 750, 600);
            g.setPaint(Color.WHITE);
            g.fill(recttem);
            g.setPaint(Color.GRAY);
            Line2D line = new Line2D.Double();
            
            for(int row = 0;row<605;){
                line.setLine(5,50+row,755,50+row);
                g.draw(line);
                row+=100;
            }
            for(int lin = 0;lin<755;){

                line.setLine(25+lin,5,25+lin,605);
                g.draw(line);
                lin+=100;
            }

            
            if(null != mNode){
                Ellipse2D circle = new Ellipse2D.Double();
                SignNode temp = null;
                String nn;
                int i =0;
                while(i < mNode.getNameList().size()){
                
                    nn = (String)mNode.getNameList().get(i);
                    temp = mNode.getNode(nn);
                    
                    if(temp.getMode().equalsIgnoreCase("B")){
                        
                        g.setPaint(Color.BLUE);
                    }
                    else{
                        
                        g.setPaint(Color.RED);
                    }
                                       circle.setFrameFromCenter(125+zoom*temp.getX(), 550-zoom*temp.getY(), 125+zoom*temp.getX()+ radius, 550-zoom*temp.getY()+ radius);
                        
                    g.fill(circle);
                    
                    g.setPaint(Color.DARK_GRAY);
                    g.drawString(temp.getSpaceID(),125+(int)(zoom*temp.getX()), 520-(int)(zoom*temp.getY()));
                   
                    if(isGetData){
                    
                        g.setPaint(Color.BLUE);
                        
                        g.drawString(temp.getData().toString(),125+(int)(zoom*temp.getX()), 540-(int)(zoom*temp.getY()));
      g.drawRect(160+(int)(zoom*temp.getX()), 533-(int)(zoom*temp.getY()), 20, 5);
                        
                        int n = (temp.getData().getData()*20)/temp.getData().maxValue;
                        
                        g.setPaint(Color.BLUE);
                        
                        g.fillRect(160+(int)(zoom*temp.getX()), 533-(int)(zoom*temp.getY()), n, 5);
                        
                    }
                
                    i++;
           
                }
            }
        }
        
        public void setSerialOut(OutputStream serialOut) {
            
            this.serialOut = serialOut;
            
        }
        
        private MutiNodes mNode = null;
        
        private JTextArea text;
        
        private JTextField knownListener;
        
        private JTextField knownBeacon;
        
        private JLabel listener;
        
        private JLabel beacon;
        
        private JButton update;
        
        private JButton store;
        
        private JPanel buttonPane;
        
        private JPanel drawpane;
        
        private JPanel infopane;
        
        private JScrollPane pane;
        
        private JScrollPane nodeTreePane;
        
        private JFrame self;
        
        private JTree nodeTree;
        
        private JLabel paneBack = new JLabel();
        
        private JOptionPane optionPane = new JOptionPane();
        
        private JMenuBar menuBar = new JMenuBar();
        
        private JMenu fileMenu = new JMenu("File");
        
        private JMenu startMenu = new JMenu("Tools");
        
        private JMenu helpMenu = new JMenu("Help");
        
        private JMenu viewMenu = new JMenu("View");
        
        private JMenuItem setMenuItem = new JMenuItem("Setting",new ImageIcon("icon/set.png"));
        
        private JMenuItem getDataItem = new JMenuItem("Get Data",new ImageIcon("icon/get.png"));
        
        private JMenuItem saveDataItem = new JMenuItem("Save Data",new ImageIcon("icon/data.png"));
        
        private JMenuItem saveImgItem = new JMenuItem("Save Image",new ImageIcon("icon/image.png"));
        
        private JMenuItem exitItem = new JMenuItem("Exit",new ImageIcon("icon/exit.png"));
        
        private JMenuItem aboutItem = new JMenuItem("About",new ImageIcon("icon/about.png"));
        
        private JMenuItem helpItem = new JMenuItem("Help",new ImageIcon("icon/help.png"));
        
        private JMenuItem zoomInItem = new JMenuItem("Zoom In" , new ImageIcon("icon/zoomin.png"));
        
        private JMenuItem zoomOutItem = new JMenuItem("Zoom Out",new ImageIcon("icon/zoomout.png"));
        
        private JFileChooser saveFileDialog ;
        
        private Runtime runt  =null;
        
        private DataBase database = new DataBase();
        
        private Calendar   cal=null;
        
        private Date d = null;
        
        private OutputStream serialOut = null;
        
        private double zoom = 1.0;
        
        private boolean isGetData=false;
        
        public final int DEFAULT_WIDTH = 943;
        
        public final int DEFAULT_HEIGHT = 790;}