gpt4 book ai didi

java - 我的 JFrame 不显示表盘的输出

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

这是我的 DialPlot 类:

public class Demo {

private DefaultValueDataset dataset = new DefaultValueDataset(0);

private ChartPanel buildDialPlot(int minimumValue=0,int maximumValue,int majorTickGap) {

DialPlot plot = new DialPlot(dataset);
plot.setDialFrame(new StandardDialFrame());
plot.addLayer(new DialValueIndicator(0));
plot.addLayer(new DialPointer.Pointer());
int redLine =maximumValue / 5 + 10;
plot.addLayer(new StandardDialRange(maximumValue, redLine, Color.blue));
plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

StandardDialScale scale = new StandardDialScale(minimumValue,
maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
scale.setTickRadius(0.88);
scale.setTickLabelOffset(0.20);
scale.setTickLabelsVisible(false);
plot.addScale(0, scale);

return new ChartPanel(new JFreeChart(plot)) {

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
}
}

这是我的 JFrame GUI 类

public class NewJFrame extends javax.swing.JFrame {
Demo d=new Demo();
int minimumValue=0;
int maximumValue=100;
int majorTickGap=1;

public NewJFrame() {
initComponents();
d.buildDialPlot(minimumValue,maximumValue,majorTickGap);
this.pack();
this.setLocationRelativeTo(null);
}

我还在 JFrame GUI 类中添加了它,如下所示:

this.add(d.buildDialPlot(minimumValue,maximumValue,majorTickGap));

它显示相同的结果空JFrame。有人知道我的错误是什么吗?

The image below shows what my example code currently produces

最佳答案

至少,您需要将 buildDialPlot() 返回的 ChartPanel add() 添加到 JFrame >。显示了完整的示例 here .

public NewJFrame() {
initComponents();
ChartPanel cp = d.buildDialPlot(minimumValue, maximumValue, majorTickGap);
this.add(cp);
this.pack();
this.setLocationRelativeTo(null);
}

附录:使用建议的方法here ,下面的示例将您的 Demo 添加到本地声明的 JFrame

image

经测试:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;

/**
* @see https://stackoverflow.com/a/38906326/230513
*/
public class Test {

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Demo d = new Demo();
ChartPanel cp = d.buildDialPlot(0, 100, 10);
f.add(cp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static class Demo {

private DefaultValueDataset dataset = new DefaultValueDataset(0);

private ChartPanel buildDialPlot(int minimumValue, int maximumValue, int majorTickGap) {

DialPlot plot = new DialPlot(dataset);
plot.setDialFrame(new StandardDialFrame());
plot.addLayer(new DialValueIndicator(0));
plot.addLayer(new DialPointer.Pointer());
int redLine = maximumValue / 5 + 10;
plot.addLayer(new StandardDialRange(maximumValue, redLine, Color.blue));
plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

StandardDialScale scale = new StandardDialScale(minimumValue,
maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
scale.setTickRadius(0.88);
scale.setTickLabelOffset(0.20);
scale.setTickLabelsVisible(false);
plot.addScale(0, scale);

return new ChartPanel(new JFreeChart(plot)) {

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}

关于java - 我的 JFrame 不显示表盘的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38906255/

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