JApplet 不显示带有图像的 JPanel

发布于 2024-12-21 03:16:31 字数 2180 浏览 1 评论 0原文

您好,我使用 JApplet 来显示 Jpanel,其中包含从网络获取的图像,但小程序正在加载但未显示图像。

代码:

package com.ntenosot;

import java.io.*;
import java.net.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.applet.*;

public class Cliente extends JApplet {

JTextField tf;
Lienzo lienzo;
Socket con;
ObjectOutputStream salida;
ObjectInputStream entrada;

public void init(){
    System.out.println("initializing");
    tf = new JTextField(10);
    lienzo=new Lienzo();    
    setSize(1000,1000);
    lienzo.setSize(900,900);
    lienzo.setVisible(true);
    setContentPane(lienzo);

    Container container = getContentPane();
    container.setBackground(Color.pink);
    container.setLayout(new FlowLayout());

    // Text area 1
    String string = "Some text in here, Some text in here, Some text in here";
    JTextArea textArea1 = new JTextArea(string, 10, 15);
    container.add(new JScrollPane(textArea1));
}

public void start() {
    ejecutar();
}

void ejecutar(){
    System.out.println("executing1");

    try{

        con = new Socket("127.0.0.1",5700);
        salida = new ObjectOutputStream(con.getOutputStream());
        salida.flush();
        entrada = new ObjectInputStream(con.getInputStream());
        System.out.println("executing");
        procesar();


    }
    catch(IOException e){
        System.out.println("error");
    }

}   

void procesar() throws IOException {
    System.out.println("processing");

    try{

        while(true){
            ImageIcon img = (ImageIcon) entrada.readObject();
            escribir(img);
        }
    }catch(ClassNotFoundException e){}

}

void escribir(final ImageIcon img){

    SwingUtilities.invokeLater(
            new Runnable(){
                public void run(){
                    lienzo.pinta(img);
                }

            }
            );

}

class Lienzo extends JPanel{

    ImageIcon img=null ;

    public void pinta(ImageIcon img){
        this.img=img;
        repaint();
    }

    public void paintComponent(Graphics g){

        super.paintComponent(g);

        if(img!=null)   
            img.paintIcon(this,g,10,10);

    }


}   

}

Hello I use a JApplet to show a Jpanel with an image that I get from the network but the applet is loading and not showing the image.

Code:

package com.ntenisot;

import java.io.*;
import java.net.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.applet.*;

public class Cliente extends JApplet {

JTextField tf;
Lienzo lienzo;
Socket con;
ObjectOutputStream salida;
ObjectInputStream entrada;

public void init(){
    System.out.println("initializing");
    tf = new JTextField(10);
    lienzo=new Lienzo();    
    setSize(1000,1000);
    lienzo.setSize(900,900);
    lienzo.setVisible(true);
    setContentPane(lienzo);

    Container container = getContentPane();
    container.setBackground(Color.pink);
    container.setLayout(new FlowLayout());

    // Text area 1
    String string = "Some text in here, Some text in here, Some text in here";
    JTextArea textArea1 = new JTextArea(string, 10, 15);
    container.add(new JScrollPane(textArea1));
}

public void start() {
    ejecutar();
}

void ejecutar(){
    System.out.println("executing1");

    try{

        con = new Socket("127.0.0.1",5700);
        salida = new ObjectOutputStream(con.getOutputStream());
        salida.flush();
        entrada = new ObjectInputStream(con.getInputStream());
        System.out.println("executing");
        procesar();


    }
    catch(IOException e){
        System.out.println("error");
    }

}   

void procesar() throws IOException {
    System.out.println("processing");

    try{

        while(true){
            ImageIcon img = (ImageIcon) entrada.readObject();
            escribir(img);
        }
    }catch(ClassNotFoundException e){}

}

void escribir(final ImageIcon img){

    SwingUtilities.invokeLater(
            new Runnable(){
                public void run(){
                    lienzo.pinta(img);
                }

            }
            );

}

class Lienzo extends JPanel{

    ImageIcon img=null ;

    public void pinta(ImageIcon img){
        this.img=img;
        repaint();
    }

    public void paintComponent(Graphics g){

        super.paintComponent(g);

        if(img!=null)   
            img.paintIcon(this,g,10,10);

    }


}   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-12-28 03:16:31

我没有测试你的代码,但似乎方法 procesar 无法终止。因此,init方法也不会终止。我认为你可以删除周围的 while (true) 循环,因为设置图像一次就足够了。

I did not test your code, but it seems method procesar cannot terminate. Therefore, the method init will also not terminate. I think you could remove the surrounding while (true) loop, as setting the image once is enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文