1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //for layout managers and more
import java.awt.event.*; //for action events
import java.net.URL;
import java.io.IOException;
public class EditorPane extends JPanel{
private String newline = "\n";
private JPanel buttonPanel;
private JPanel textPanePanel;
private JEditorPane myEditorPane;
private JButton boldButton;
private JButton colorButton;
private JButton imgButton;
private JButton saveButton;
public EditorPane() {
createGUI();
}
private void createGUI() {
buttonPanel = new JPanel();
boldButton = new JButton("Bold");
boldButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
boldButtonActionPerformed(evt);
}
});
colorButton = new JButton("Color");
colorButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
colorButtonActionPerformed(evt);
}
});
imgButton = new JButton("Image");
imgButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
imgButtonActionPerformed(evt);
}
});
saveButton = new JButton("Save");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
buttonPanel.setLayout(new GridLayout(1, 4));
buttonPanel.add(boldButton);
buttonPanel.add(colorButton);
buttonPanel.add(imgButton);
buttonPanel.add(saveButton);
textPanePanel = new JPanel();
textPanePanel.setLayout(new BorderLayout());
myEditorPane = this.createEditorPane();
JScrollPane paneScrollPane = new JScrollPane(myEditorPane);
textPanePanel.add(paneScrollPane, BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(textPanePanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("TextSamplerDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditorPane newContentPane = new EditorPane();
//newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
}
});
}
private JEditorPane createEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(true);
java.net.URL helpURL = TextSamplerDemo.class.getResource(
"simpleHTMLPage.html");
if (helpURL != null) {
try {
editorPane.setPage(helpURL);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + helpURL);
}
} else {
System.err.println("Couldn't find file: simpleHTMLPage.html");
}
return editorPane;
}
private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("boldButtonNextActionPerformed");
//myEditorPane
// TODO add your handling code here:
}
private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("colorButtonActionPerformed");
// TODO add your handling code here:
}
private void imgButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("imgButtonActionPerformed");
// TODO add your handling code here:
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("saveButtonActionPerformed");
String newStr = new String("this is the new String");
//newStr.setFont(new java.awt.Font("Tahoma", 0, 14));
this.myEditorPane.replaceSelection("sdfsdfsd");
// TODO add your handling code here:
}
} |
Partager