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
|
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 30-Sep-2006
* Time: 20:38:33
* To change this template use File | Settings | File Templates.
*/
public class JTextPaneTestFrame extends JFrame {
public JTextPaneTestFrame() {
JTextPane myTextPane = new JTextPane();
StyledDocument doc = myTextPane.getStyledDocument();
try {
doc.insertString(0, "The attacks were planned by the ISI and carried out by the Islamist militant group Lashkar-e-Toiba, based in Pakistan, Mumbai's police chief said.\n" +
"AN Roy said the Students' Islamic Movement of India had also assisted.\n" +
"Pakistan rejected the allegations and said India had given no evidence of Pakistani involvement in the attacks.\n" +
"\"We have solved the 11 July bombings case. The whole attack was planned by Pakistan's ISI and carried out by Lashkar-e-Toiba and their operatives in India,\" Mumbai (Bombay) police commissioner AN Roy told a news conference", null);
} catch (BadLocationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
JScrollPane myScrollPane = new JScrollPane();
myScrollPane.setPreferredSize(new Dimension(400, 300));
myScrollPane.setViewportView(myTextPane);
add(myScrollPane);
JPanel myButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
/**************************************************************************************************************/
JButton button = new JButton("<html><b>B</b></html>");
button.setRequestFocusEnabled(false);
Action action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
action.putValue(Action.SHORT_DESCRIPTION, "An action to ..."); // -> this will be the tooltip on a button ;-)
// action.putValue(..., ...); icon, accelerator...
InputMap inputMap = myTextPane.getInputMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.CTRL_MASK), action);
button.setAction(action);
myButtonPanel.add(button);
/**************************************************************************************************************/
/**************************************************************************************************************/
button = new JButton("<html><i>I</i></html>");
button.setRequestFocusEnabled(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StyledEditorKit.ItalicAction().actionPerformed(null);
}
});
myButtonPanel.add(button);
/**************************************************************************************************************/
add(myButtonPanel, BorderLayout.PAGE_END);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JTextPaneTestFrame myFrame = new JTextPaneTestFrame();
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
//To change body of implemented methods use File | Settings | File Templates.
}
});
}
} |
Partager