設定されたテキストをコピーできる JTextArea を作成するにはどうすればよいでしょうか。
単純な Cntl-C または右クリックによるコピーで問題ありません。
コードは私が取り組んでいるタイル ゲーム用であり、TextArea に情報を出力します。テキストをコピーできないことを除けば、うまく機能します。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TileHelper{
static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
public Tile previous, current;
static final String[] intString = {"4","5","6","7","8","9","10"};
public void buildGUI() {
//build our test gui & components
frame = new JFrame("TileHelper");
frame.setSize(680, 240);
manual = new JCheckBox("Get Manually");
getTile = new JButton("Get Tile");
end = new JButton("End");
start = new JButton("Start");
numList = new JComboBox(intString);
numList.setSelectedIndex(0);
field = new JTextField(10);
field.setText("Enter Name");
area = new JTextArea(670, 175);
area.setEditable(true);
area.setText("Press Start below after selection.");
//set font
area.setFont(new Font("Serif", Font.PLAIN, 16));
scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane = frame.getContentPane();
pane.setLayout(null);
pane.add(manual);
pane.add(getTile);
pane.add(end);
pane.add(field);
pane.add(start);
pane.add(scroller);
pane.add(numList);
insets = pane.getInsets();
scroller.setBounds(insets.left + 5, insets.top + 5, 670, 175);
start.setBounds(insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
getTile.setBounds(insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
end.setBounds(insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, end.getPreferredSize().width, end.getPreferredSize().height);
numList.setBounds(insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, numList.getPreferredSize().width + 6, numList.getPreferredSize().height);
field.setBounds(insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, field.getPreferredSize().width, field.getPreferredSize().height);
manual.setBounds(insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width, manual.getPreferredSize().height);
getTile.addActionListener(new getListener());
end.addActionListener(new endListener());
start.addActionListener((new startListener()));
frame.setResizable(false);
frame.setVisible(true);
}
public static class startListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (!manual.isSelected()) {
startLoop = true;
}
if (field.getText().equals("Enter Name")) {
area.setText("You must name your array");
} else area.setText("Tile " + field.getText() ");
}
}
public static class endListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
area.append(" };");
area.selectAll();
area.copy();
}
}
public static class getListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
if (firstTime) {
newTile = String.format("new Tile" );
numTiles++;
firstTime = false;
} else {
if (++numTiles % 3 == 0){
newTile = String.format(",\n\t new Tile" );
} else newTile = String.format(", new Tile" );
}
area.append(newTile);
}
}