2013-08-19 61 views
1

注意:这个问题是从this question后续的问题。如何把JFrame与jmathplot图放入JPanel

我也是刚刚从我的最后一个问题如下:

JPanelFLowLayout(!与相同输出从NullLayout上调整大小),只接受PreferredSize,孩子不调整大小与它的容器,需要使用BorderLayout/GridLayout简单的图形

这个原理的演示也给予:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import org.math.plot.Plot2DPanel; 
import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import org.math.plot.Plot2DPanel; 

public class MyPlot { 

    private JFrame frame = new JFrame("JMathPlot library in a swing application."); 
    private JPanel panel = new JPanel(); 

    public MyPlot() { 
     double[] x = new double[]{0, 1, 2, 3, 4, 5}; 
     double[] y = new double[]{10, 11, 12, 14, 15, 16}; 
     Plot2DPanel plot = new Plot2DPanel() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 200); 
      } 
     }; 
     plot.addLinePlot("my plot", x, y); // add a line plot to the PlotPanel 
     panel.setLayout(new BorderLayout()); 
     panel.add(plot); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocation(150, 150); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MyPlot(); 
      } 
     }); 
    } 
} 

现在我想实现的是将图形集成到parent JPanel中,以便在我的swing应用程序中使用它。

这方面的一个可视化将是:

enter image description here

如何实现我的“父母”的JPanel的图形不违反约束使其正常工作?

(我看着JInternalFrame但未能拿出一个正确的实现我的问题)

如果是JInternalFrame确实是解决方案,我应该如何使用它? (请提供代码)

+0

为什么不看看[JInternalFrame的( http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html),为此任务...?+1,因为你从前一篇文章中学到了很多东西,毫无疑问,天才本人很好地回答了这些问题.-) –

+0

@nIcEcOw你的评论是无法理解的。你是说JInternalFrame的确有解决方案吗? –

+1

是的,我猜是这样,虽然我从来没有碰过'JInternalFrame'到目前为止,虽然当你描述你的问题时,他们觉得我适合这种任务。这就是为什么我发布评论而不是答案:-) –

回答

1

我不知道这是否就是你要找什么,但我会给你这个源代码,也许它会帮助你:

public class dz extends JFrame { 

private JPanel contentPane; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       dz frame = new dz(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public dz() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 917, 788); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    SpringLayout sl_contentPane = new SpringLayout(); 
    contentPane.setLayout(sl_contentPane); 

    // define your data 
    double[] x = { 0, 1, 2, 3, 4, 5 }; 
    double[] y = { 45, 89, 6, 32, 63, 12 }; 

    // create your PlotPanel (you can use it as a JPanel) 
    Plot2DPanel plot = new Plot2DPanel(); 

    // define the legend position 
    plot.addLegend("SOUTH"); 

    // add a line plot to the PlotPanel 
    plot.addLinePlot("my plot", x, y); 




    sl_contentPane.putConstraint(SpringLayout.EAST, plot, 600, SpringLayout.WEST, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.NORTH, plot, 15, SpringLayout.NORTH, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.WEST, plot, 15, SpringLayout.WEST, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.SOUTH, plot, 600, SpringLayout.NORTH, contentPane); 

/* panel.setLayout(new GridBagLayout()); 
    panel.add(plot); 
    plot.validate(); 
    plot.repaint(); 
    plot.setBounds(50,50,100,100);*/ 

    contentPane.add(plot); 

    JPanel panel = new JPanel(); 
    panel.setBackground(Color.BLACK); 
    sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 20, SpringLayout.NORTH, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.WEST, panel, 16, SpringLayout.EAST, plot); 
    sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 408, SpringLayout.NORTH, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.EAST, panel, 251, SpringLayout.EAST, plot); 
    contentPane.add(panel); 


} 

}

2

bacis的东西,没什么特别的,大多数的应用程序窗口是similair desingned

  • 的JFrame控股两家JPanels

    1. 1日的JPanel(BorderLayout这在API)(网格布局)带有两个组件(JFrame.WEST/EAST)

      • JScrollPane中的JTree/JList用于显示荷兰国际集团保存在util.List或树/地图

      • 有将被放置randomPanel

    2. 的JPanel用图(JFrame.CENTER)仿制药的选择

  • 你可以使用JSplitPane用于隐藏,禁用或标准Divider的两个JPanel(am)

+0

如果JFrame必须具有绝对布局,该怎么办? –

+0

那么你必须计算放置在JFrame中的JComponents的内部区域,假定它的基本代数为 – mKorbel

+0

好吧,假设我有10个'随机JPanel's',就像我的图片一样,父JPanel具有绝对(XY)布局。现在我将如何使用JComponents? (我不明白你的意思与基本代数) –