gpt4 book ai didi

java - SWT Shell 中的嵌入式 JEditorPane

转载 作者:行者123 更新时间:2023-12-01 14:55:12 28 4
gpt4 key购买 nike

我试图将 JEditorPane 嵌入到 SWT Shell 中,但没有成功。 JEdi​​torPane 没有出现,我不明白为什么。

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.rtf.RTFEditorKit;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class EmbeddedRtf {

public static void main(String args[]) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Rtf editor");

Menu mb = new Menu(shell, SWT.BAR);
MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
fileItem.setText("&File");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(fileMenu);
MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
exitItem.setText("&Exit\tCtrl+X");
exitItem.setAccelerator(SWT.CONTROL + 'X');
MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
aboutItem.setText("&About\tCtrl+A");
aboutItem.setAccelerator(SWT.CONTROL + 'A');
shell.setMenuBar(mb);

Composite rtfComposite = new Composite(shell, SWT.EMBEDDED);

java.awt.Frame frame = SWT_AWT.new_Frame(rtfComposite);
java.awt.Panel panel = new java.awt.Panel(new java.awt.FlowLayout());
frame.add(panel);

// Create an RTF editor window
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit(rtf);
editor.setBackground(Color.white);

// This text could be big so add a scroll pane
JScrollPane scroller = new JScrollPane();
scroller.getViewport().add(editor);
panel.add(scroller);

// Load an RTF file into the editor
try {
FileInputStream fi = new FileInputStream("test.rtf");
rtf.read(fi, editor.getDocument(), 0);
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("I/O error");
} catch (BadLocationException e) {
}

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

我还制作了一个仅使用 swing 组件的小程序,并且出现了 JEditorPane,但是当我将其嵌入到 shell 中时,它没有出现。谢谢。

最佳答案

您需要在 shell 上设置布局才能显示组合。使用这样的东西:

shell.setLayout(new FillLayout());

关于java - SWT Shell 中的嵌入式 JEditorPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14366853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com