图表上的 setFont 问题 (JFreeChart)

发布于 2024-12-26 03:06:26 字数 2423 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2025-01-02 03:06:26

你的代码在我的实现上运行良好。您可以尝试此变体,看看使用现有字体是否有帮助。正如 @camickr 所建议的,EDT 上不需要 invokeLater()

public void setButton(final JFreeChart chart, JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TextTitle title = chart.getTitle();
            Font font = title.getFont();
            float size = font.getSize();
            title.setFont(font.deriveFont(size + 2));
        }
    });
}

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.

public void setButton(final JFreeChart chart, JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TextTitle title = chart.getTitle();
            Font font = title.getFont();
            float size = font.getSize();
            title.setFont(font.deriveFont(size + 2));
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文