文本編輯器源碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.imageio.*;
import java.io.*;
public class EditPanel extends JPanel
{
?public EditPanel()
?{??
??JPanel FontPanel = new JPanel();
??JPanel BackColorPanel = new JPanel();
??JPanel TextAreaPanel = new JPanel();
??ImagePanel picturePanel = new ImagePanel();
??
??JtextArea = new JTextArea(20,40);
??jscrollPane = new JScrollPane(JtextArea);
??
??JPopupMenu jpopupMenu = new JPopupMenu();
??JMenuItem CopyItem = new JMenuItem("復制",new ImageIcon("copy.gif"));
??JMenuItem PastItem = new JMenuItem("粘貼",new ImageIcon("paste.gif"));
??JMenuItem CutItem = new JMenuItem("剪切",new ImageIcon("cut.gif"));
??
??Action pictureAction = new PictureAction();??
??CopyItem.addActionListener(pictureAction);
??PastItem.addActionListener(pictureAction);
??CutItem.addActionListener(pictureAction);
??
??CopyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
??CopyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
??CopyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
??jpopupMenu.add(CopyItem).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
??jpopupMenu.add(PastItem).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
??jpopupMenu.add(CutItem).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
??
??JtextArea.setComponentPopupMenu(jpopupMenu);
??font = new Font("宋體",Font.PLAIN,12);??
??JLabel TypeLabel = new JLabel("類型:");
??JLabel SizeLabel = new JLabel("大小:");
??JLabel ModelLabel = new JLabel("樣式:");
??
??ColorSlider1 = new JSlider(0,255,0);
??ColorSlider2 = new JSlider(0,255,0);
??ColorSlider3 = new JSlider(0,255,0);
??ColorSlider1.setPaintTicks(true);
??ColorSlider2.setPaintTicks(true);
??ColorSlider3.setPaintTicks(true);
??ColorSlider1.setMajorTickSpacing(51);
??ColorSlider2.setMajorTickSpacing(51);
??ColorSlider3.setMajorTickSpacing(51);
??ColorSlider1.setMinorTickSpacing(3);
??ColorSlider2.setMinorTickSpacing(3);
??ColorSlider3.setMinorTickSpacing(3);
??ColorSlider1.setPaintTicks(true);
??ColorSlider2.setPaintTicks(true);
??ColorSlider3.setPaintTicks(true);
??ColorSlider1.setPaintLabels(true);
??ColorSlider2.setPaintLabels(true);
??ColorSlider3.setPaintLabels(true);
??
??String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().
??getAvailableFontFamilyNames();
??ModelComboBox = new JComboBox(fonts);
??SizeSlider = new JSlider(0,100,12);
??SizeSlider.setMajorTickSpacing(20);
??SizeSlider.setMinorTickSpacing(2);
??SizeSlider.setPaintTicks(true);
??SizeSlider.setPaintLabels(true);
??TypeCheckBox1 = new JCheckBox("常規");
??TypeCheckBox2 = new JCheckBox("傾斜");
??TypeCheckBox3 = new JCheckBox("加粗");
??
??BackColorListener ColorListener = new BackColorListener();
??ActionListener listener = new FontListener();
??TypeCheckBox1.addActionListener(listener);
??TypeCheckBox2.addActionListener(listener);
??TypeCheckBox3.addActionListener(listener);
??ModelComboBox.addActionListener(listener);
??ColorSlider1.addChangeListener(ColorListener);
??ColorSlider2.addChangeListener(ColorListener);
??ColorSlider3.addChangeListener(ColorListener);
??SizeSlider.addChangeListener(new ChangeListener()
??{
???public void stateChanged(ChangeEvent event)
???{
????JtextArea.setFont(new Font((String)ModelComboBox.getSelectedItem(),font.getStyle(),SizeSlider.getValue()));????
???}???
??});
??
??setLayout(new GridBagLayout());
??FontPanel.setLayout(new GridBagLayout());
??BackColorPanel.setLayout(new GridBagLayout());
??TextAreaPanel.setLayout(new BorderLayout());
??add(FontPanel,new View(0,0).setAnchor(View.CENTER).setInsets(5,5,0,5).setIpad(2, 2).setFill(View.HORIZONTAL).setWeight(0, 0));
??add(BackColorPanel,new View(0,1).setAnchor(View.CENTER).setFill(View.HORIZONTAL).setInsets(5).setWeight(0, 0));
??add(picturePanel,new View(0,2).setAnchor(View.CENTER).setFill(View.BOTH).setInsets(5).setWeight(0,100));
??add(TextAreaPanel,new View(1,0,1,4).setWeight(100, 100).setFill(View.BOTH));
??
??TextAreaPanel.add(jscrollPane,BorderLayout.CENTER);
??
??FontPanel.add(TypeLabel,new View(0,0));
??FontPanel.add(SizeLabel,new View(0,1));
??FontPanel.add(ModelLabel,new View(0,2));
??FontPanel.add(TypeCheckBox1,new View(1,0).setAnchor(View.WEST).setInsets(10));
??FontPanel.add(TypeCheckBox2,new View(2,0).setAnchor(View.WEST).setInsets(10));
??FontPanel.add(TypeCheckBox3,new View(3,0).setAnchor(View.WEST).setInsets(10));
??FontPanel.add(SizeSlider,new View(1,1,3,1).setAnchor(View.WEST).setFill(View.HORIZONTAL).setInsets(5));
??FontPanel.add(ModelComboBox,new View(1,2,3,1).setInsets(10));
??
??BackColorPanel.add(ColorSlider1,new View(0,0).setInsets(5).setFill(View.BOTH).setWeight(100, 100).setIpad(5, 5));
??BackColorPanel.add(ColorSlider2,new View(0,1).setInsets(5).setFill(View.BOTH).setWeight(100, 100).setIpad(5, 5));
??BackColorPanel.add(ColorSlider3,new View(0,2).setInsets(5).setFill(View.BOTH).setWeight(100, 100).setIpad(5, 5));
??Border LowerBorder = BorderFactory.createLoweredBevelBorder();
??Border AreaTextTitle = BorderFactory.createTitledBorder(LowerBorder, "文本框");
??jscrollPane.setBorder(AreaTextTitle);
??
??Border EtchedBorder = BorderFactory.createEtchedBorder();
??Border FontTitle = BorderFactory.createTitledBorder(EtchedBorder, "字體");
??FontPanel.setBorder(FontTitle);
??
??Border BackColorTitle = BorderFactory.createTitledBorder(EtchedBorder, "背景");
??BackColorPanel.setBorder(BackColorTitle);
?}
?
?public JTextArea getTextArea ()
?{
??return JtextArea;??
?}
?private class FontListener implements ActionListener
?{
??public void actionPerformed(ActionEvent event)
??{
???int map = 0;
???if(TypeCheckBox1.isSelected())
???{
????map = Font.PLAIN;
????TypeCheckBox2.setSelected(false);
????TypeCheckBox3.setSelected(false);
????font = font.deriveFont(map);
???}
???if(TypeCheckBox2.isSelected())
???{
????map +=Font.ITALIC;
????TypeCheckBox1.setSelected(false);
????font = font.deriveFont(map);
???}
???if(TypeCheckBox3.isSelected())
???{
????map +=Font.BOLD;
????TypeCheckBox1.setSelected(false);?
????font = font.deriveFont(map);
???}
???JtextArea.setFont(new Font((String)ModelComboBox.getSelectedItem(),font.getStyle(),SizeSlider.getValue()));
??}??
?}
?private class BackColorListener implements ChangeListener
?{
??public void stateChanged(ChangeEvent event)
??{
???int a = ColorSlider1.getValue();
???int b = ColorSlider2.getValue();
???int c = ColorSlider3.getValue();
???JtextArea.setBackground(new Color(a,b,c));
??}??
?}
?private class PictureAction extends AbstractAction
?{
??public void actionPerformed(ActionEvent event)
??{
???System.out.println(getValue(Action.NAME) + "按鈕被選擇了!");
??}
?}
???? class ImagePanel extends JPanel
?{?
??? public ImagePanel()
??? {?
?????? // acquire the image
?????? try
?????? {
????????? image = ImageIO.read(new File("heihei.gif"));
?????? }
?????? catch (IOException e)
?????? {
????????? e.printStackTrace();
?????? }
??? }
???
??? public void paintComponent(Graphics g)
??? {?
?????? super.paintComponent(g);
?????? if (image == null) return;
???
?????? int imageWidth = image.getWidth(this);
?????? int imageHeight = image.getHeight(this);
?????? // draw the image in the upper-left corner
??????
?????? g.drawImage(image, 0, 0 ,this.getWidth(),this.getHeight(),null);
?????? // tile the image across the panel
??? }
?}
?private JTextArea JtextArea;
?private Font font;
?private JCheckBox TypeCheckBox1;
?private JCheckBox TypeCheckBox2;
?private JCheckBox TypeCheckBox3;
?private JSlider SizeSlider;
?private JComboBox ModelComboBox;
?private JSlider ColorSlider1;
?private JSlider ColorSlider2;
?private JSlider ColorSlider3;
?private JScrollPane jscrollPane;
?private Image image;
}
非常好我支持^.^
(0) 0%
不好我反對
(0) 0%