gpt4 book ai didi

java - 当单击按钮从网络加载图像时,如何防止表单卡住?

转载 作者:行者123 更新时间:2023-12-02 00:47:32 25 4
gpt4 key购买 nike

我想通过单击按钮将图像从网络显示到另一个 Jframe 中的面板,但是每当我单击按钮时,首先会加载图像,在此期间当前表单可能会卡住,并且一旦图像加载了表格与图像一起显示..我怎样才能避免我的表格卡住的情况,因为它非常烦人

我的代码::

我现在的类(class)

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:
}

我的下一个显示图像的类(class)

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

}

最佳答案

你肯定需要按照trashgod的建议使用swingworker。为了清楚地说明这种联系,这是我强烈建议您尝试的一个例子。检查此链接中的“更复杂的图像图标示例”,并理解代码。它完美地解决了你的问题。 http://download.oracle.com/javase/tutorial/uiswing/components/icon.html

特别要注意这段代码:

 public IconDemoApp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Icon Demo: Please Select an Image");

// A label for displaying the pictures
photographLabel.setVerticalTextPosition(JLabel.BOTTOM);
photographLabel.setHorizontalTextPosition(JLabel.CENTER);
photographLabel.setHorizontalAlignment(JLabel.CENTER);
photographLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

// We add two glue components. Later in process() we will add thumbnail buttons
// to the toolbar inbetween thease glue compoents. This will center the
// buttons in the toolbar.
buttonBar.add(Box.createGlue());
buttonBar.add(Box.createGlue());

add(buttonBar, BorderLayout.SOUTH);
add(photographLabel, BorderLayout.CENTER);

setSize(400, 300);

// this centers the frame on the screen
setLocationRelativeTo(null);

// start the image loading SwingWorker in a background thread
loadimages.execute();
}

您可以在此构造函数中看到事件分派(dispatch)如何与 loadimages.execute() 分开发生,后者使用 swingworker 在后台加载图像。我很乐意澄清任何疑问。

关于java - 当单击按钮从网络加载图像时,如何防止表单卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4530642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com