How to display two canvas in a JFrame java

这一生的挚爱 提交于 2021-01-29 03:50:56

问题


I recently have a requirement to display a word file within a JFrame. With this link I was able to achieve what I want (Open MS documents into JFrame). What i need is to display a word file and a pdf file side by side within a JFrame.

In the link mentioned above, the word file was displayed in a JFrame via a Canvas from SWT.

I would like to know:

  1. Whether it is possible to add two canvases to a single JFrame.
  2. If not, is it possible to display a word document or a PDF file in a JPanel (since I know that adding two panels to a frame is possible)?

回答1:


In the example you linked the canvas is added directly to the content pane of the JFrame. What you need to do is to insert a JPanel with a Layout to the JFrame first, and after that add one or many Canvas objects to the layout. A trivial example with the default layout FlowLayout is below, feel free to modify it to use a different layout manager or add a JScrollPane or JSplitPane depending on the layout you want.

JPanel panel = new JPanel(); //Default layout manager is FlowLayout
//You could change the layout here with panel.setLayout(new ..Layout);
frame.getContentPane().add(panel);
panel.add(canvas1);
panel.add(canvas2);

Here is a useful link to layout managers. Look for example into BorderLayout if you wish to add menus etc. to your frame.



来源:https://stackoverflow.com/questions/32345995/how-to-display-two-canvas-in-a-jframe-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!