问题
There is the following code for making jzy3d chart in JFrame:
public class SurfaceViewerFrame extends IconFrame {
public SurfaceViewerFrame() {
setResizable(false);
//System.loadLibrary("lib/jogl2-rc10/gluegen-rt.jar");
Settings.getInstance().setHardwareAccelerated(true);
FormLayout layout=new FormLayout("10px, 300px, 10px", "30px, 10px, 20px, 300px, 10px");
CellConstraints сс=new CellConstraints();
JLabel title=new JLabel("Выходная поверхность");
Mapper mapper = new Mapper() {
public double f(double x, double y) {
return x * Math.sin(x * y);
}
};
// Define range and precision for the function to plot
Range range = new Range(-300, 300);
int steps = 80;
// Create the object to represent the function over the given range.
final Shape surface = Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), mapper);
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
surface.setFaceDisplayed(true);
surface.setWireframeDisplayed(false);
// Create a chart
Chart chart = new Chart(Quality.Advanced, "awt");
chart.getScene().getGraph().add(surface);
chart.addController(new CameraKeyController());
// ChartLauncher.openChart(chart, new Rectangle(0, 0, 100, 100), "122");
JPanel panel=new JPanel();
panel.add(title, сс.xy(1, 1));
panel.add((Component)chart.getCanvas(), CC.xy(1, 3));
add(panel);
setSize(320, 370);
setVisible(true);
}
}
But I see nothing if I don't recomment openChart() method. If I do it, that there will be Chart in my JFrame and a new empty JFrame; I don't want to use it. Please, tell me, how can I fix it? I need to show graphic in my JFrame without making a new one.
UPDATE: Sorry, jzy3d is library for making 3d surfaces. And this code works, I don't need other LayoutManager, please, read my question again.
回答1:
A CardLayout is well suited to this use. See How to Use CardLayout for details and working examples.
Other strategies for combining data can be seen in/linked from this answer to "The Use of Multiple JFrames, Good/Bad Practice?".
Update
To flesh out that idea.
- Don't extend frame, simply keep a reference to a panel.
- Make the panel a
GridLayout
orBorderLayout
(any single component added to either with no constraint, will be stretched to the available width & height). - Fill that panel much as you are doing in the code snippet above.
- Add that panel to a card of a
CardLayout
in the main (and only) frame. - (If necessary) flip to that card.
来源:https://stackoverflow.com/questions/12948842/how-to-add-jzy3d-chart-to-jframe-without-making-a-new-one