图表上的 setFont 问题 (JFreeChart)
我是java新手。我正在尝试创建一个带有 swing 按钮的简单 GUI,它可以更改 JFreeChart 库中图表的字体。然而,当我使用chart.setFont时,它会使GUI冻结几秒钟。我读到我应该在其他线程中使用 setFont 来避免这个问题,但它似乎对我不起作用 - 可能我做错了。我把我的代码放在下面:
package javaapplication2;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public final class Main{
Main(){
JFrame frame=new JFrame();
DefaultCategoryDataset data=new DefaultCategoryDataset();
JFreeChart chart=ChartFactory.createBarChart("Title", "Name", "Grade", data, PlotOrientation.VERTICAL,
true, false, false);
ChartPanel chartPanel=new ChartPanel(chart);
JPanel subPanel=new JPanel();
JPanel panel=new JPanel();
JButton button=new JButton("Click");
setButton(chart, button);
setChart(data);
setSubPanel(subPanel, chartPanel);
setPanel(panel, button, subPanel);
setFrame(frame);
frame.setContentPane(panel);
frame.validate();
frame.setVisible(true);
}
public void setFrame(JFrame frame){
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
}
public void setPanel(JPanel panel, JButton button, JPanel subPanel){
panel.add(button);
panel.add(subPanel);
}
public void setSubPanel(JPanel subPanel, ChartPanel chartPanel){
subPanel.add(chartPanel);
}
public void setButton(final JFreeChart chart, JButton button){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chart.getTitle().setFont(new Font("Sans-Serif", Font.PLAIN, 18));
}
}
);
}
});
}
public void setChart(DefaultCategoryDataset data){
data.addValue(1, "abc", "");
data.addValue(2, "def", "");
data.addValue(3, "ghi", "");
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Main frame=new Main();
}
});
}
}
任何人都可以帮我解决这个问题或者给出任何提示在这种情况下我应该如何使用线程吗?
I'm new to java. I'm trying to create a simple GUI with swing with button, which change the font of chart from JFreeChart library. However when I use chart.setFont, it makes a GUI freezed for a couple of seconds. I read that I should use setFont in other thread to avoid the problem but it doesn't seem work for me - probably I'm doing it wrong. I put my code below:
package javaapplication2;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public final class Main{
Main(){
JFrame frame=new JFrame();
DefaultCategoryDataset data=new DefaultCategoryDataset();
JFreeChart chart=ChartFactory.createBarChart("Title", "Name", "Grade", data, PlotOrientation.VERTICAL,
true, false, false);
ChartPanel chartPanel=new ChartPanel(chart);
JPanel subPanel=new JPanel();
JPanel panel=new JPanel();
JButton button=new JButton("Click");
setButton(chart, button);
setChart(data);
setSubPanel(subPanel, chartPanel);
setPanel(panel, button, subPanel);
setFrame(frame);
frame.setContentPane(panel);
frame.validate();
frame.setVisible(true);
}
public void setFrame(JFrame frame){
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
}
public void setPanel(JPanel panel, JButton button, JPanel subPanel){
panel.add(button);
panel.add(subPanel);
}
public void setSubPanel(JPanel subPanel, ChartPanel chartPanel){
subPanel.add(chartPanel);
}
public void setButton(final JFreeChart chart, JButton button){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chart.getTitle().setFont(new Font("Sans-Serif", Font.PLAIN, 18));
}
}
);
}
});
}
public void setChart(DefaultCategoryDataset data){
data.addValue(1, "abc", "");
data.addValue(2, "def", "");
data.addValue(3, "ghi", "");
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Main frame=new Main();
}
});
}
}
Could anyone help me to solve it or give any tip how should I use thread in that case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码在我的实现上运行良好。您可以尝试此变体,看看使用现有字体是否有帮助。正如 @camickr 所建议的,EDT 上不需要
invokeLater()
。Your code works fine on my implementation. You might try this variation to see if using the existing font helps. As suggested by @camickr, the
invokeLater()
is not needed on the EDT.