java swing 等待框_java – 如何在组件绘制时创建“请等待”Swing对话框

此博客展示了如何在Java Swing应用程序中创建一个'请等待'对话框,特别是在组件绘制或JFreeChart加载时,以防止GUI冻结。通过使用SwingWorker,可以实现后台任务处理并提供更好的用户反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个示例和/但它使用SwingWorker.你应该认真考虑使用它,因为如果操作系统以某种方式使你的帧无效并且你的JFreeChart的加载是在EDT(事件调度线程)上完成的,那么你的GUI将会被冻结.

它还允许您在处理数据时提供更好的用户反馈. (对不起,如果代码有点长,但大多数有趣的代码都在initUI和SwingWorker中).

注意:您可以使用JLayer(如果使用Java 7)而不是对话框,但在我的示例中这是不必要的.

/**

* This code was directly taken from: http://www.vogella.com/articles/JFreeChart/article.html

* All credits goes to him for this code.

*

* Thanks to him.

*/

import java.util.List;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.SwingWorker;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PiePlot3D;

import org.jfree.data.general.DefaultPieDataset;

import org.jfree.data.general.PieDataset;

import org.jfree.util.Rotation;

public class PieChart extends JFrame {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

initUI();

}

});

}

protected static void initUI() {

// First we create the frame and make it visible

final PieChart demo = new PieChart("Comparison");

demo.setSize(500, 270);

demo.setVisible(true);

// Then we display the dialog on that frame

final JDialog dialog = new JDialog(demo);

dialog.setUndecorated(true);

JPanel panel = new JPanel();

final JLabel label = new JLabel("Please wait...");

panel.add(label);

dialog.add(panel);

dialog.pack();

// Public method to center the dialog after calling pack()

dialog.setLocationRelativeTo(demo);

// allowing the frame and the dialog to be displayed and, later, refreshed

SwingWorker worker = new SwingWorker() {

@Override

protected JFreeChart doInBackground() throws Exception {

publish("Loading dataset");

// simulating the loading of the Dataset

try {

System.out.println("Loading dataset");

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

// This will create the dataset

PieDataset dataset = demo.createDataset();

publish("Loading JFreeChart");

// simulating the loading of the JFreeChart

try {

System.out.println("Loading JFreeChart");

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

// based on the dataset we create the chart

JFreeChart chart = demo.createChart(dataset, "Which operating system are you using?");

// we put the chart into a panel

return chart;

}

@Override

protected void process(List chunks) {

label.setText(chunks.get(0));

dialog.pack();

dialog.setLocationRelativeTo(demo);

dialog.repaint();

}

@Override

protected void done() {

try {

// Retrieve the created chart and put it in a ChartPanel

ChartPanel chartPanel = new ChartPanel(this.get());

// add it to our frame

demo.setContentPane(chartPanel);

// Dispose the dialog.

dialog.dispose();

// We revalidate to trigger the layout

demo.revalidate();

// Repaint, just to be sure

demo.repaint();

} catch (Exception e) {

e.printStackTrace();

}

}

};

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

worker.execute();

}

});

dialog.setVisible(true);

}

public PieChart(String applicationTitle) {

super(applicationTitle);

}

/** * Creates a sample dataset */

private PieDataset createDataset() {

DefaultPieDataset result = new DefaultPieDataset();

result.setValue("Linux", 29);

result.setValue("Mac", 20);

result.setValue("Windows", 51);

return result;

}

/** * Creates a chart */

private JFreeChart createChart(PieDataset dataset, String title) {

JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title

dataset, // data

true, // include legend

true, false);

PiePlot3D plot = (PiePlot3D) chart.getPlot();

plot.setStartAngle(290);

plot.setDirection(Rotation.CLOCKWISE);

plot.setForegroundAlpha(0.5f);

return chart;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值