在另一个JFrame窗体中获取一个JFframe窗体的基元布尔类型的值



我使用java swing和java谷歌文本到语音api设计了这个示例聊天应用程序,它允许您翻译语言。我试图在这个演示程序中实现的是点击选择按钮。选择按钮的文本必须首先更新为英语,然后更新为HINDI,并相应地将标志变量I设置为0或1。然后在服务器程序中使用这个i值,我想弄清楚应该显示印地语消息还是英语消息。但我做不到。我处理事件以实现这一目标的方式有错吗???如果是,请给出解决方案。

此外,我面临的另一个问题是,一旦我从客户端向服务器发送消息,在重新运行程序之前,我无法发送更多消息。我该如何解决这个问题???

请在这方面帮忙,如果你需要更多的解释,请告诉我。。。。

谢谢:)

CLIENT.JAVA

package serverappdemo;
import java.io.DataOutputStream;
import java.net.Socket;
import com.gtranslate.Translator;
import com.gtranslate.Language;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client extends javax.swing.JFrame {
    Socket s;
    DataOutputStream dout;
    String ogmsg;
    public Client() {
        initComponents();
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try {
            s = new Socket("localhost", 6666);
        }catch(java.io.IOException e) {}
    }                                        
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        System.exit(0);
    }                                        
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            // TODO add your handling code here:
            ogmsg = jTextField1.getText();
            dout = new DataOutputStream(s.getOutputStream()) ;
            dout.writeUTF(ogmsg);
            dout.flush();
            dout.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }                                        
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jButton3.setText("Eng");
        callLang();
    }                                        
    public int callLang() {
        int i = 0;
        if(jButton3.getText().equals("Eng")) {
            i = 0;
        } else if(jButton3.getText().equals("Hin")) {
    //        jButton3.setText("Eng");
    //        Translator tr = Translator.getInstance();
    //        ogmsg = tr.translate(ogmsg, Language.ENGLISH, Language.HINDI);
              i = 1;
        }
        return i;
    }
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Client().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}

服务器.JAVA

//I removed the editor folds that contained form design code.
package serverappdemo;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.IOException;
import com.gtranslate.Language; // API
import com.gtranslate.Translator; //API
public class Server extends javax.swing.JFrame {
    ServerSocket ss;
    Socket s;
    DataInputStream dis;
    Client c;
    String str;
    Translator tr = Translator.getInstance();
//    int i = c.callLang();
public Server() {
    this.c = new Client();
    initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        server();
    }catch(java.io.IOException e) {}
}                                        
public void server() throws java.io.IOException {
    ss = new ServerSocket(6666);
    s = ss.accept();
    readData();
    dis.close();
    s.close();
}
public void readData() throws IOException {
    dis = new DataInputStream(s.getInputStream());
    str = (String) dis.readUTF();
    if( c.callLang() == 1 ) {
        Translator tr = Translator.getInstance();
        str = tr.translate(str, Language.ENGLISH, Language.HINDI);
        jTextArea1.setText(str);
    } else if(c.callLang() == 0) {
        jTextArea1.setText(str);
    }
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.exit(0);
}                                        

public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Server().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration                   

}

它很简单,使用JcomboBoxes来选择一种语言,并将该值传递给其他表单,然后设置首选语言。不过,在打包程序时,一定要记住访问修饰符的概念。

为什么不尝试使用JComboBox选择一种语言,然后在第二帧中使用该类的实例传递该选择索引:)

相关内容

  • 没有找到相关文章

最新更新