0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaで「テキストフィールドに対してクリップボードを経由でコピーやペーストをプログラムから行う」の動作を確認してみた。

Posted at

概要

Javaで「テキストフィールドに対してクリップボードを経由でコピーやペーストをプログラムから行う」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample13_1.java
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class JSample13_1 extends JFrame implements ActionListener{
  JTextField text1;
  JTextField text2;

  public static void main(String args[]){
    JSample13_1 frame = new JSample13_1("MyTitle");
    frame.setVisible(true);
  }

  JSample13_1(String title){
    setTitle(title);
    setBounds(100, 100, 600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text1 = new JTextField("APPLE", 40);

    JButton button1_1 = new JButton("Copy");
    button1_1.addActionListener(this);
    button1_1.setActionCommand("Text1_Copy");
    JButton button1_2 = new JButton("Cut");
    button1_2.addActionListener(this);
    button1_2.setActionCommand("Text1_Cut");
    JButton button1_3 = new JButton("Paste");
    button1_3.addActionListener(this);
    button1_3.setActionCommand("Text1_Paste");

    JPanel button_panel_1 = new JPanel();
    button_panel_1.add(button1_1);
    button_panel_1.add(button1_2);
    button_panel_1.add(button1_3);

    text2 = new JTextField("orange", 40);

    JButton button2_1 = new JButton("Copy");
    button2_1.addActionListener(this);
    button2_1.setActionCommand("Text2_Copy");
    JButton button2_2 = new JButton("Cut");
    button2_2.addActionListener(this);
    button2_2.setActionCommand("Text2_Cut");
    JButton button2_3 = new JButton("Paste");
    button2_3.addActionListener(this);
    button2_3.setActionCommand("Text2_Paste");

    JPanel button_panel_2 = new JPanel();
    button_panel_2.add(button2_1);
    button_panel_2.add(button2_2);
    button_panel_2.add(button2_3);

    JPanel p = new JPanel();
    p.add(text1);
    p.add(button_panel_1);
    p.add(text2);
    p.add(button_panel_2);

    Container contentPane = getContentPane();
    contentPane.add(p, BorderLayout.CENTER);
  }

  public void actionPerformed(ActionEvent e){
    String cmd = e.getActionCommand();

    if (cmd.equals("Text1_Copy")){
      text1.copy();
    }else if (cmd.equals("Text1_Cut")){
      text1.cut();
    }else if (cmd.equals("Text1_Paste")){
      text1.paste();
    }else if (cmd.equals("Text2_Copy")){
      text2.copy();
    }else if (cmd.equals("Text2_Cut")){
      text2.cut();
    }else if (cmd.equals("Text2_Paste")){
      text2.paste();
    }
  }
}

以下のコマンドを実行しました。

$ javac JSample13_1.java 
$ java JSample13_1

実行結果は以下の通りでした。

Screenshot from 2025-01-28 03-15-12.png

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?