Java GUI之JTextArea

public class JTextAreaDemo {
   public static void main(String[] args) {
      JFrame f = new JFrame("留言板");
      JTextArea message = new JTextArea(20, 20);
      f.add(new JLabel("请您留言"), "North");
      f.add(message, "Center");
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1, 6));
      String[] string = { "剪切", "复制", "粘贴", "删除", "提交", "清除" };
      String[] buttonActionCommands = { "CUT", "COPY", "PASTE", "DELETE",
            "SUBMIT", "RESET" };
      JButton button[] = new JButton[6];
      OuterMonitor om = new OuterMonitor(message, button);
      for (int i = 0; i < string.length; i++) {
         button[i] = new JButton(string[i]);
         button[i].setActionCommand(buttonActionCommands[i]);
         if (i < 4) {
            button[i].setEnabled(false);
         }
         button[i].addActionListener(om);
         p.add(button[i]);
      }
      message.addMouseMotionListener(om);
      f.add(p, "North");
      f.setSize(400, 150);
      f.setLocation(300, 100);
      f.setVisible(true);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

class OuterMonitor extends MouseMotionAdapter implements ActionListener {
   private JTextArea msg;
   private JButton buttons[];
   private String clipBoard = "";

   public OuterMonitor(JTextArea msg, JButton buttons[]) {
      this.msg = msg;
      this.buttons = buttons;
   }

   public void setClipBoard(String text) {
      this.clipBoard = text;
   }

   public void actionPerformed(ActionEvent e) {
      int position = msg.getSelectionStart();
      String s = e.getActionCommand();
      if (s.equals("RESET")) {
         msg.setText("");
      } else if (s.equals("SUBMIT")) {
         System.out.println("你的留言为:\n" + msg.getText());
      } else if (s.equals("COPY")) {
         clipBoard = msg.getSelectedText();
         msg.setSelectionStart(msg.getSelectionEnd());
      } else if (s.equals("CUT")) {
         clipBoard = msg.getSelectedText();
         this.delete();
      } else if (s.equals("PASTE")) {
         String content = msg.getText();
         String alter = content.substring(0, position) + clipBoard
               + content.substring(position);
         msg.setText(alter);
      } else if (s.equals("DELETE")) {
         this.delete();
      }
      this.switchButtons();
   }

   public void delete() {
      int start = msg.getSelectionStart();
      int end = msg.getSelectionEnd();
      String content = msg.getText();
      String alter = content.substring(0, start) + content.substring(end);
      msg.setText(alter);
   }

   public void switchButtons() {
      boolean cliped = (clipBoard != null) && (clipBoard.length() > 0);
      buttons[2].setEnabled(cliped);
      boolean selected = msg.getSelectionEnd() != msg.getSelectionStart();
      buttons[0].setEnabled(selected);
      buttons[1].setEnabled(selected);
      buttons[3].setEnabled(selected);
   }

   public void mouseDragged(MouseEvent m) {
      this.switchButtons();
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值