java 显示图片_java – 显示图像

我想通过单击按钮将图像从Web显示到另一个JFrame中的面板.每当我点击按钮时,首先加载图像;在此期间,当前形式可能会冻结.加载图像后,表格随图像一起显示.我怎么能避免我的表格冻结的情况,因为它非常刺激.在我的代码中:

我目前的课程:

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:

}

我的下一个班级显示图像:

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")

//

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();

}//

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值