2013-08-19 64 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? (我不明白你的意思與基本代數) –