显示图像 [英] To display an image

查看:65
本文介绍了显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过单击按钮在Web上将图像显示到另一个JFrame中的面板.每当我单击按钮时,都会首先加载图像;在此期间,当前表单可能会冻结.加载图像后,表单将与图像一起显示.我该如何避免表单很烦人而冻结的情况.在我的代码中:

I want to display an image from the web to a panel in another JFrame at the click of a button. Whenever I click the button, first the image loads; and during this time, the current form potentially freezes. Once the image has loaded, the form is displayed with the image. How can I avoid the situation where my form freezes since it is very irritating. Among my codes:

我当前的课程:

private void btn_TrackbusActionPerformed(java.awt.event.ActionEvent evt) {                                             
        try {
            sendMessage("Query,map,$,start,211,Arsenal,!");
            System.out.println(receiveMessage());
        } catch (UnknownHostException ex) {
            Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception ex) {
            Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
        }
        client_trackedbus nextform=new client_trackedbus(planform,connection,packet_receive,packet_send);
        this.setVisible(false);
        this.dispose();
        nextform.setVisible(true);
        // TODO add your handling code here:
    }                                            

我的下一个显示图像的课程:

My next class that displays the image:

public class client_trackedbus extends javax.swing.JFrame {
    client_planform planform=null;
    DatagramSocket connection=null;
    DatagramPacket packet_receive=null;
    DatagramPacket packet_send=null;
    JLabel label=null;
    /** Creates new form client_trackedbus */
    public client_trackedbus(client_planform planform,DatagramSocket connection,DatagramPacket packet_receive,DatagramPacket packet_send) {
        initComponents();
        this.planform=planform;
        this.connection=connection;
        this.packet_receive=packet_receive;
        this.packet_send=packet_send;
        try {
            displayMap("http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg", jPanel1, new JLabel());
        } catch (MalformedURLException ex) {
            Logger.getLogger(client_trackedbus.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    private void displayMap(String url,JPanel panel,JLabel label) throws MalformedURLException{
        URL imageurl=new URL(url);
        Image image=(Toolkit.getDefaultToolkit().createImage(imageurl));
        ImageIcon icon = new ImageIcon(image);
        label.setIcon(icon);
        panel.add(label);
       // System.out.println(panel.getSize().width);
        this.getContentPane().add(panel);
    }


    /** 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btn_Exit = new javax.swing.JButton();
        btn_Plan = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Public Transport Journey Planner");

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

        jLabel1.setFont(new java.awt.Font("Arial", 1, 18));
        jLabel1.setText("Your tracked bus");

        btn_Exit.setText("Exit");
        btn_Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ExitActionPerformed(evt);
            }
        });

        btn_Plan.setText("Plan journey");
        btn_Plan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_PlanActionPerformed(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)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(btn_Plan)
                        .addGap(65, 65, 65)
                        .addComponent(btn_Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Exit)
                    .addComponent(btn_Plan))
                .addContainerGap(12, Short.MAX_VALUE))
        );

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

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

    private void btn_PlanActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        this.setVisible(false);
        this.dispose();
         this.planform.setVisible(true);
    }                                        


    private void Exitform(){
        this.setVisible(false);
        this.dispose();

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               // new client_trackedbus().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btn_Exit;
    private javax.swing.JButton btn_Plan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   

}

推荐答案

public class SSBTest extends javax.swing.JFrame {

    /** Creates new form worker1 */
    public SSBTest() {
        initComponents();

    }

    /** 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() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("jLabel1");

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap(196, Short.MAX_VALUE).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(178, 178, 178)).addGroup(layout.createSequentialGroup().addGap(86, 86, 86).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(122, Short.MAX_VALUE)));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE).addComponent(jLabel1).addGap(36, 36, 36)));

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

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
             final SSBTest ssbTest = new SSBTest();
             ssbTest.setVisible(true);
             ssbTest.execute();
            }
        });
    }

 private void execute() {
  (new MeaningOfLifeFinder(jLabel1, jPanel1)).execute();
 }

 // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}

class MeaningOfLifeFinder extends SwingWorker<Icon, Object> {

    JLabel label = null;
    JPanel panel;

    MeaningOfLifeFinder(JLabel label, JPanel jpanel) {
        this.label = label;
        this.panel = jpanel;
    }

    protected Icon doInBackground() throws IOException {
        URL imageurl;
        Image image = null;
        System.out.println("image loading");
        imageurl = new URL("http://maps.google.com/maps/api/staticmap"
            + "?zoom=14&size=512x512&maptype=roadmap"
            + "&markers=color:green|label:21|-15.0,-150.0&sensor=false");
        //image = (Toolkit.getDefaultToolkit().createImage(imageurl));
        image = ImageIO.read(imageurl);
     ImageIcon icon = new ImageIcon(image);
        System.out.println("image loaded...");
        return icon;

    }

    @Override
    protected void done() {
        try {
            System.out.println("image adding to label...");
            label.setIcon(get());
            //panel.add(label);
            System.out.println("image loaded to label...");
        } catch (Exception ignore) {
        }
    }
    // System.out.println(panel.getSize().width);
}

这篇关于显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆