たなしょのメモ

日々勉強していることをつらつらと

Java Swingをやってみた メモ1

Javaの現場なので勉強してみました。

JTextField text1 = new JTextField("東京都");

テキストフィールドを作成する。文字列を入れられたり、テキストフィールドの大きさを指定できる。

 

text3.setText("住所を入力してください。");

setTextで文字列を新たに挿入できる。

 

text2.setForeground(Color.WHITE);

text2.setBackground(Color.BLACK);

setForegroundで文字の色を。

setBackgoundで文字の背景色を変更。

 

text2.setFont(new Font("MSゴシック", Font.PLAIN, 8));

フォントをしてする。第一引数がフォントの種類、第二引数が?、第三引数は文字の大きさ。

 

text1.setPreferredSize(new Dimension(200, 15));

テキストフィールドの大きさを調整する。

 

text2.setMargin(new Insets(20, 40, 20, 40));

テキストフィールドの間隔を調整する。

 

text4.setBorder(new LineBorder(Color.BLUE, 4, true));

ボーダーの設定を変更する。

本ソースではボーダーの色を変更している。

 

text1.setHorizontalAlignment(JTextField.LEFT);

テキストフィールド内の文字の位置を変更する。

 

text.setCaretPosition(text.getText().length());

キャレットの位置を変更。

 

text.setCaretColor(Color.RED);

キャレットの色を変更。

 

public void actionPerformed(ActionEvent e) {
label.setText(text.getSelectedText());

}

入力したテキストをlabelに渡す。

 

text.requestFocusInWindow();
text.selectAll();

ボタンをクリックすると全ての文字を選択するようになる。

 

JTextField text1 = new JTextField("東京都港区赤坂", 20);
JTextField text2 = new JTextField("東京都港区赤坂", 20);
text2.setEditable(false);
JTextField text3 = new JTextField("東京都港区赤坂", 20);
text3.setEnabled(false);

setEditableはコピーはできるが入力はできない。

setEnabledはコピーも入力もできない。

 

JButton buttonCut = new JButton("CUT");
buttonCut.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
text.cut();
}
}
);

JButton buttonCopy = new JButton("COPY");
buttonCopy.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
text.copy();
}
}
);

JButton buttonPaste = new JButton("PASTE");
buttonPaste.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
text.paste();
}
}
);

cut()、copy()、paste()でカット、コピー、ペーストができる。

text.setToolTipText("テキストを入力してください。");

ツールチップを表示させる。

 

JavaファイルをJarファイルにする。

qiita.com

 

githubに芝が生えなかったので読んだ。

qiita.com