gpt4 book ai didi

java - 应用程序窗口获得焦点并调整大小

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

我在使用 JFreeChart 时遇到两个问题。

我正在使用线 XYLineChart 并更新数据集,以便它定期刷新数据并添加更多项目。问题是,当我添加更多数据时,应用程序窗口会直接返回前台,因此如果我在另一个窗口中输入,该窗口就会失去焦点。

我遇到的第二个问题是,每次图表更新时,它都会调整自身大小,因此我无法调整大小和维护它。

请问有人可以帮忙解决这个问题吗?谢谢

public static void main (String[] args){
String val= args[0];
LineChart chart = new LineChart("Name " + val);

while(true){
Main main = new Main();
XMLData data = getMyData()
chart.addToDataSet(data);
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
LineChart.incremementxAxisTimePlot(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}



package com.chart.graph;


import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import com.graph.logfiledata.XMLData;


/**
* A simple demonstration application showing how to create a line chart using data from an
* {@link XYDataset}.
*
*/
public class LineChart extends ApplicationFrame {

private static int xAxisTimePlot = 5;
private XYSeriesCollection dataset = new XYSeriesCollection();

public LineChart(final String title) {

super(title);
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);

dataset.addSeries(series2);
dataset.addSeries(series3);
dataset.addSeries(series4);
dataset.addSeries(series5);
dataset.addSeries(series6);
dataset.addSeries(series7);
dataset.addSeries(series8);
dataset.addSeries(series9);
dataset.addSeries(series10);


}

public static void incremementxAxisTimePlot(boolean update){
if(update){
xAxisTimePlot+=5;
}
}
XYSeries series2 = new XYSeries("1");
XYSeries series3 = new XYSeries("2");
XYSeries series4 = new XYSeries("3");
XYSeries series5 = new XYSeries("4");
XYSeries series6 = new XYSeries("5");
XYSeries series7 = new XYSeries("6");
XYSeries series8 = new XYSeries("7");
XYSeries series9 = new XYSeries("8");
XYSeries series10 = new XYSeries("9");
public void addToDataSet(XMLData data){

series2.add(xAxisTimePlot, data.getOne());
series3.add(xAxisTimePlot, data.getTwo());
series4.add(xAxisTimePlot, data.getThree());
...

}

/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private JFreeChart createChart(final XYDataset dataset) {

// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart("Line Chart","X","Y", dataset, PlotOrientation.VERTICAL,true,true, false);
chart.setBackgroundPaint(Color.white);

// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);

final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);

// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

return chart;
}
}

最佳答案

  • while(true){没有 break; 就是无限循环这一切Objects在 5 秒内创建(在 while(true){ 内)

  • 特别是创建一堆新 GUI 的无限循环

代码含义

Main main = new Main();
XMLData data = getMyData()
chart.addToDataSet(data);
chart.pack();
  • 之前准备GUI,那么是否有真正的理由创建循环,而是定义 boolean 变量 instread while(canRun){while(true){break;

  • 为什么会有这样的理由Thread.sleep(5000); ,使用 Swing Timer 如果 GUI 基于 Swing / AWT container ,

  • 如果是基于 Swing / AWT 的容器那么你遇到了 Concurency in Swing 的问题,其中所有事件都必须在 EventDispatchTread 上完成,

  • 在(无限)循环结束之前,您的 GUI 永远不会刷新,因为 Thread.sleep(5000);导致 Swing GUI 闪烁卡住,导致锁定 EventDispatchTread

关于java - 应用程序窗口获得焦点并调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995536/

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