0

我想在我的代码下方显示类中textbox的字符串vpsfame,但我无法显示它。你能给我建议这个字符串在new8代码中的类中显示吗?串行类给我来自gps的字符串继续。它还在运行时通过 System.out.println 显示它,但不通过textboxofvpsfame类显示它

包vpspro;

import javax.comm.*;
import java.util.*;
import java.io.*;


public class Serial
{
public void com() throws UnsupportedCommOperationException, IOException, TooManyListenersException
{
int c=1;

String wantedPortName="COM6";

Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();

CommPortIdentifier portId = null;  
while(portIdentifiers.hasMoreElements())
{
    CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
    if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
       pid.getName().equals(wantedPortName)) 
    {
        portId = pid;
        break;
    }
}
if(portId == null)
{
    System.err.println("Could not find serial port " + wantedPortName);
    System.exit(1);
}
else
{
    System.out.println("system find gps reciever");
}
SerialPort port = null;
try {
    port = (SerialPort) portId.open(
        "RMC", 
        1);
    System.out.println("all are ok"); 
} catch(PortInUseException e) {
    System.err.println("Port already in use: " + e);
    System.exit(1);
}

port.setSerialPortParams(
    4800,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);



BufferedReader is = null;  


try {
  is = new BufferedReader(new InputStreamReader(port.getInputStream()));
  System.out.println("data is ok");
} catch (IOException e) {
  System.err.println("Can't open input stream: write-only");
  is = null;
}

String pt=null;
while(true)
{

String st = is.readLine();


        System.out.print("(" + c + ")");
        c++;
        new8 obj1 = new new8();

        obj1.decode(st);
        System.out.println(st);
        st = st.replace(st, "");


}

if (is != null) is.close();
/*if (os != null) os.close();*/
if (port != null) port.close();


}
}

此代码用于开始与串行类通信

package vpspro;

import java.io.IOException;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;


public class getcon extends javax.swing.JFrame {


    public getcon() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sc = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        br = new javax.swing.JComboBox();
        help = new javax.swing.JButton();
        ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("select comport");

        sc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12" }));
        sc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scActionPerformed(evt);
            }
        });
        sc.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                scInputMethodTextChanged(evt);
            }
        });

        jLabel2.setText("select buad rate");

        br.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "4800", "1900", "150000" }));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        help.setText("help");

        ok.setText("ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(105, 105, 105))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(171, Short.MAX_VALUE)
                        .addComponent(ok)
                        .addGap(28, 28, 28)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(help)
                        .addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(help)
                    .addComponent(ok))
                .addContainerGap(118, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void brActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        if(evt.getSource()==br)
                br.addItem("4800");
                br.addItem("9600");
    }                                  

    private void scActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        if(evt.getSource()==sc)
            sc.addItem("COM6");
        sc.addItem("COM7");
    }                                  

    private void scInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {                                          
        // TODO add your handling code here:
        if(evt.getSource()==sc)
            sc.addItem("COM6");
        sc.addItem("COM7");
    }                                         

    private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        if(evt.getSource()==ok)
        {
            try {
                new Serial().com();
            } catch (UnsupportedCommOperationException ex) {
                Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TooManyListenersException ex) {
                Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
            }
            new new8();

        }
    }                                  


    public void getcom() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new getcon().setVisible(true);
            }
        });
    }                    
    private javax.swing.JComboBox br;

    private javax.swing.JButton help;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    public javax.swing.JButton ok;
    private javax.swing.JComboBox sc;

}

此代码解码我的 gps 字符串并显示和听到问题,当我朗姆酒程序文本框为空时,我无法在文本框中的字符串下方显示。

package vpa.io.*;
import vpspro.vpsfame;

public class new8
{
static String[] a2;
static String a1[];
static String c;
public static String[] t;

public void decode(String st)



{


    if(st.isEmpty())
    {
        System.out.println("System has proble");
        vpsfame obj=new vpsfame();
        obj.ab.setText("ok");
    }
else
    {

 a1=st.split("\\$",0);

a2=a1[1].split("\\*",0);
checksum();


if(a2[1].equalsIgnoreCase(c))
{


t=st.split(",",0);



switch (t[0].charAt(0))
{
case '$':

String s=t[0].substring(3,6);


String s3="RMC";



if (s.compareTo(s3)==0)                                                    

{


System.out.println("                    NMEA 0183 Details.....");
System.out.println("System identity:                            "+t[0].substring(1,3));
System.out.println("NMEA Formate:                               "+t[0].substring(3,6));
System.out.println("(UTC)Time:                                  "+t[1].substring(1,2));
new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude:                                  "+t[3]);


System.out.println("Direction of Lattitude:                     "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude:                                  "+t[5]);
System.out.println("Direction of Longitude:                     "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground:                          "+t[7]);
new vpsfame().cd.setText(t[7]);
/*System.out.println("Track mode:                                 "+t[8]);
System.out.println(st.isEmpty());
System.out.println("DATE:                                       "+t[9]);
System.out.println("adjustment declination                      "+t[10]);
System.out.println("Direction of Magnetic variation:            "+t[11].charAt(0));*/





}


}

else
        System.out.println("                                    Invalid Formate...");
        break;
        default:
        System.out.println("                                    Invalid DATA....");
}
}
else
System.out.println("                                            no match checksum");

}
}





public static void checksum()
{


int l=a2[0].length();
char[] a=a2[0].toCharArray();
int b=0;

for(int i=0;i<l;i++)
{

b=b^a[i];

}

 c=Integer.toHexString(b);

}

}

这是我的主要课程

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * vpsfame.java
 *
 * Created on Jan 26, 2013, 11:02:32 PM
 */
package vpspro;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextField;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;



/**
 *
 * @author DHARMA
 */
public class vpsfame extends javax.swing.JFrame {
    public vpsfame()
    {
            initComponents();
    }

    /** Creates new form vpsfame */

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        ab = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        bc = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        ac = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cd = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        gc = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        lm = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ab.setEditable(false);
        ab.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abActionPerformed(evt);
            }
        });

        jLabel1.setText("latitude");

        jLabel2.setText("longitude");

        bc.setText(" ");
        bc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bcActionPerformed(evt);
            }
        });

        jLabel4.setText("UTC");

        ac.setText(" ");
        ac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acActionPerformed(evt);
            }
        });

        jLabel5.setText("Speed of vessel");

        cd.setText(" ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 621, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 368, Short.MAX_VALUE)
        );

        jMenu1.setText("File");

        gc.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        gc.setIcon(new javax.swing.ImageIcon("C:\\Users\\DHARMA\\Desktop\\png\\20x20\\55.png")); // NOI18N
        gc.setText("get connection");
        gc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gcActionPerformed(evt);
            }
        });
        jMenu1.add(gc);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("track");
        jMenu1.add(jMenuItem2);

        lm.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
        lm.setText("load map");
        lm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lmActionPerformed(evt);
            }
        });
        jMenu1.add(lm);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("data save");
        jMenu1.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.META_MASK));
        jMenuItem5.setText("set time zone");
        jMenu1.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("quit");
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("view");

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem7.setText("saved data");
        jMenu2.add(jMenuItem7);

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem8.setText("about us");
        jMenu2.add(jMenuItem8);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(19, 19, 19))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35))
                    .addComponent(ab, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(ac, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(bc, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addGap(16, 16, 16)
                        .addComponent(bc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(ac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void gcActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        if(evt.getSource()==gc)
        {
            getcon obj3=new getcon();
            obj3.setVisible(true);
        }

    }                                  

    private void lmActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void abActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:

    }                                  

    private void acActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void bcActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:

    }                                  

    /**
     * @param args the command line arguments
     */ 

    public static void main(String args[]) throws UnsupportedCommOperationException, IOException, TooManyListenersException {





    /*public vpsfame() 
    {


        initComponents();*/
        new new8();





        java.awt.EventQueue.invokeLater(new Runnable() {


            public void run() {



                /*URL url=getClass().getResource("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");*/
                vpsfame obj4=new vpsfame();

                new vpsfame().setBackground(Color.yellow);
                obj4.setSize(1000, 
                        500);


                ImageIcon image1=new ImageIcon("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");
                System.out.println("jjdfhj");
                obj4.setIconImage(image1.getImage());
                obj4.setTitle("VPS");
                obj4.setVisible(true);
                new new8();




            }
        });

}
    /*public vpsfame()
    {
       fm=new JFrame("VPS");
        fm.setSize(1000, 1000);
        fm.setVisible(true);
        fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }*/
    // Variables declaration - do not modify
    public static javax.swing.JTextField ab;
    public javax.swing.JTextField ac;
    public static javax.swing.JTextField bc;
    public javax.swing.JTextField cd;
    private javax.swing.JMenuItem gc;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JMenuItem lm;
    // End of variables declaration
}
4

1 回答 1

0

我会认真建议您花点时间,并重构您的所有代码。忽略在类new8中,在方法中decode替换此代码:

new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude:                                  "+t[3]);


System.out.println("Direction of Lattitude:                     "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude:                                  "+t[5]);
System.out.println("Direction of Longitude:                     "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground:                          "+t[7]);
new vpsfame().cd.setText(t[7]);

有了这个:

//use one object, dont just create new frames
vpsfame myFrame = new vpsfame();

myFrame.ac.setText(t[1].substring(1,2));
System.out.println("Lattitude:                                  "+t[3]);
System.out.println("Direction of Lattitude:                     "+t[4]);

myFrame.ab.setText(t[3].concat(t[4]));
System.out.println("Longitude:                                  "+t[5]);
System.out.println("Direction of Longitude:                     "+t[6]);

myFrame.bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground:                          "+t[7]);
myFrame.cd.setText(t[7]);

//Also set the frame to visible, so it can be displayed once you've done all this work.
myFrame.setVisible(true);

问题是:

  1. 首先,您正在创建多个 vpsframe 对象,而您应该只有一个。创建 5 个帧,每个帧都有 4 个空字段是没有意义的。创建一个对象并初始化它的所有字段,而不是使用 5 个不同的对象。
  2. 您没有跟踪您正在创建的对象,垃圾收集器只会摆脱它们,因此创建一个对象,将其分配给一个变量并使用它。
  3. 将 vpsfame 对象设置为可见,否则您将永远看不到它。

您的代码也需要重构,您应该使用 camelCase 命名约定,并且类应该始终以大写字母开头。

于 2013-02-10T10:39:10.000 回答