JavaのJTextPaneとJEditorPaneの違いは何ですか?
JTextPane JEditorPaneの拡張です フォント、テキストスタイル、色などのワードプロセッシング機能を提供します など。高負荷のテキスト処理を行う必要がある場合は、このクラスを使用できますが、 JEditorPane HTMLの表示/編集をサポートします およびRTF コンテンツ 独自のEditorKitを作成することで拡張できます 。
JTextPane
- JTextPane JEditorPaneのサブクラスです 。
- JTextPane 埋め込みのスタイル付きドキュメントに使用されます 画像 およびコンポーネント。
- JTextPane は、グラフィカルに表現された属性でマークアップできるテキストコンポーネントであり、 DefaultStyledDocumentを使用できます。 デフォルトモデルとして。
- JTextPaneの重要なメソッドは、 addStyle()、getCharacterAttributes()、getStyledDocument()、setDocument()、setEditorKit()、setStyledDocument()です。 など
例
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class JTextPaneTest {
public static void main(String args[]) throws BadLocationException {
JFrame frame = new JFrame("JTextPane Test");
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setBold(set, true);
pane.setCharacterAttributes(set, true);
pane.setText("Welcome to");
set = new SimpleAttributeSet();
StyleConstants.setItalic(set, true);
StyleConstants.setForeground(set, Color.blue);
Document doc = pane.getStyledDocument();
doc.insertString(doc.getLength(), " Tutorials ", set);
set = new SimpleAttributeSet();
StyleConstants.setFontSize(set, 20);
doc.insertString(doc.getLength(), " Point", set);
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(375, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} 出力
JEditorPane
- JEditorPane は、さまざまなテキスト形式を表示できる一種のテキスト領域です
- デフォルトでは、 JEditorPane HTMLをサポート およびRTF(リッチテキスト形式) 、特定のコンテンツタイプを処理するための独自のエディタキットを作成できます。
- setContentType()を使用できます 表示するドキュメントを選択するメソッドとsetEditorKit() JEditorPaneのカスタムエディタを設定するメソッド 明示的に。
例
import javax.swing.*;
public class JEditorPaneTest extends JFrame {
public JEditorPaneTest() {
setTitle("JEditorPane Test");
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<h1>Java</h1><p>is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.</p>");
setSize(350, 275);
setContentPane(editorPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] a) {
new JEditorPaneTest();
}
} 出力
-
JavaのJFrameとJDialogの違いは何ですか?
JFrame フレームに追加されたコンポーネントはそのコンテンツと呼ばれ、 contentPaneによって管理されます。 。 JFrameにコンポーネントを追加するには 、 contentPaneを使用する必要があります 代わりに。 JFrame タイトルのウィンドウが含まれています 、境界線 、(オプション)メニュー バー およびユーザー -指定 コンポーネント 。 JFrame 移動できます 、サイズ変更 、アイコン化 JComponentのサブクラスではありません 。 デフォルトでは、JFrameは左上の角に表示されます 画面のr。指定した場所にフレームを表示するには
-
JavaのGridLayoutとGridBagLayoutの違いは何ですか?
GridLayout すべてのコンポーネントを長方形のグリッドに配置し、同じサイズの長方形に分割します 各コンポーネントは長方形の中に配置されますが、 GridBagLayout 柔軟です レイアウト マネージャー コンポーネントを垂直方向と水平方向に整列させます コンポーネントが同じサイズである必要はありません。各GridBagLayout オブジェクトは、動的な長方形のセルのグリッドを維持します 各コンポーネントがコンポーネントと呼ばれる1つ以上のセルを占有します 表示 エリア 。 GridLayout GridLayout コンポーネントを長方形のグリッドに配置します。セル