Salve, io ho un programma del genere che mi crea il mio frame col grafico dentro una finestra:
codice:
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Second;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple internal frame.
*
*/
public class InternalFrame extends ApplicationFrame {
/**
* Creates a new instance.
*
* @param title the title.
*/
public InternalFrame(String title) {
super(title);
JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame frame = createFrame();
desktopPane.add(frame);
frame.pack();
frame.setLocation(0, 0);
frame.setVisible(true);
getContentPane().add(frame);
}
/**
* Creates an internal frame.
*
* @return An internal frame.
*/
private JInternalFrame createFrame() {
XYDataset dataset = createDataset("Nome linea",new Second());
JFreeChart chart = ChartFactory.createTimeSeriesChart("Trend Cella 30","Tempo","Temperatura",dataset,true,true,true);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(800, 600));
JInternalFrame frame = new JInternalFrame("", true);
frame.getContentPane().add(chartPanel);
return frame;
}
/**
* Creates a sample dataset.
*
* @param name the dataset name.
* @param base the starting value.
* @param start the starting period.
*
* @return The dataset.
*/
private XYDataset createDataset(String name, RegularTimePeriod start) {
TimeSeries series = new TimeSeries("Temperatura ambiente", start.getClass());
TimeSeries series1 = new TimeSeries("Temperatura prodotto", start.getClass());
TimeSeries series2 = new TimeSeries("Set alta temperatura", start.getClass());
TimeSeries series3 = new TimeSeries("Set bassa temperatura", start.getClass());
TimeSeries series4 = new TimeSeries("Set temperatura media", start.getClass());
Connection con = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
}
catch (Exception e) {
System.out.println("Fallito il caricamento dei driver SQL.");
return (null);
}
try {
// Query SQL
String SQL= "SELECT * FROM Cella30Trend where substring(Tempo,9,2)>=01 and substring(Tempo,9,2)<=31 and substring(Tempo,6,2)>=01 and substring(Tempo,6,2)<=05 and substring(Tempo,1,4)=2006";
// Connessione col database in cui vengono specificati url, nome utente, password
con = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/DbFrigo", "**", "**");
Statement select = con.createStatement();
//caricamento query SQL
ResultSet result = select.executeQuery(SQL);
while (result.next()) {
String tempo = result.getString(2);
double temp_a = result.getDouble(3);
double temp_p = result.getDouble(4);
double temp_s_a = result.getDouble(5);
double temp_s_b = result.getDouble(6);
double temp_s_m = result.getDouble(7);
DateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd HH.mm.ss");
java.util.Date data=dateformat.parse(tempo);
series.addOrUpdate(new Second(data),temp_a);
series1.addOrUpdate(new Second(data),temp_p);
series2.addOrUpdate(new Second(data),temp_s_a);
series3.addOrUpdate(new Second(data),temp_s_b);
series4.addOrUpdate(new Second(data),temp_s_m);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
dataset.addSeries(series4);
return dataset;
}
/**
* The starting point.
*
* @param args ignored.
*/
public static void main( String[] args) {
InternalFrame iframe = new InternalFrame("Trend Frigo");
iframe.pack();
RefineryUtilities.centerFrameOnScreen(iframe);
iframe.setVisible(true);
}
}
Vorrei poter levare il frame dalla finestra e fare in modo che si apra direttamente il frame (con comando di chiusura, ingrandimento, etc).
Inoltre vorrei poter aggiungere in basso un piccolo menù in cui vengono indicati per ogni linea il valore alto, basso e medio....
è possibile fare tutto ciò???
Se sì, come??
Potete darmi gentilmente una mano?
GRAZIE