问题
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:
- Whether it is possible to add two canvases to a single
JFrame
. - 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