항상 설정
contentPane-Layout-absolute
button이벤트
Add event handle-mouse-mouseClicled
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class JFrameEx01 extends JFrame {
private JPanel contentPane;
private JLabel lbl1;
private JLabel lbl2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameEx01 frame = new JFrameEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameEx01() {
getContentPane().setLayout(null);
setTitle("새로운 프레임");
System.out.println(getTitle());
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBackground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lbl1 = new JLabel("New label");
lbl1.setBounds(39, 20, 157, 16);
contentPane.add(lbl1);
JButton btn = new JButton("내용 복사");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//lbl2.setText(lbl1.getText());
}
});
btn.setBounds(26, 51, 170, 22);
contentPane.add(btn);
lbl2 = new JLabel("New label");
lbl2.setBounds(34, 84, 162, 22);
contentPane.add(lbl2);
}
}
JLabel / JButton
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class JTextFieldEx01 extends JFrame {
private JPanel contentPane;
private JTextField tf;
private JPasswordField pf;
private JButton btn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextFieldEx01 frame = new JTextFieldEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextFieldEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf = new JTextField();
tf.setText("HELLO");
tf.setBounds(28, 16, 200, 32);
contentPane.add(tf);
tf.setColumns(10);
pf = new JPasswordField();
pf.setEchoChar('*');
pf.setBounds(28, 53, 200, 32);
contentPane.add(pf);
btn = new JButton("내용 보기");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(tf.getText());
//System.out.println(pf.getText()); deprecated
System.out.println(new String (pf.getPassword()));
}
});
btn.setBounds(28, 88, 200, 32);
contentPane.add(btn);
}
}
JLabel 대신 TextField - edirable(false)
PasswordField - 암호화는 아님 텍스트를 안보이게 함
echoChar 에서 모양 변경 가능
코드를 입력하세요import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class JTextFieldEx02 extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JButton btn;
private JTextField tf2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextFieldEx02 frame = new JTextFieldEx02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextFieldEx02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf1 = new JTextField();
tf1.setBounds(31, 6, 130, 26);
contentPane.add(tf1);
tf1.setColumns(10);
JButton btn = new JButton("대문자로 변경");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
/* System.out.println(tf1.getText());
String[] names = tf1.getText().split(" ");
for(int i=0 ; i<names.length ; i++) {
names[i] = names[i].substring(0,1).toUpperCase()
+names[i].substring(1);
//System.out.println(names[i]);
}
String ename = String.join(" ", names);
//System.out.println(ename);
tf2.setText(ename);
tf1.setText(""); */
tf2.setText(capitalize(tf1.getText()));
}
});
btn.setBounds(31, 44, 130, 26);
contentPane.add(btn);
tf2 = new JTextField();
tf2.setEditable(false);
tf2.setBounds(31, 81, 130, 26);
contentPane.add(tf2);
tf2.setColumns(10);
}
//단어의 첫글자만 대문자화하는 메서드
public String capitalize(String name) {
String[] names = tf1.getText().split(" ");
for(int i=0 ; i<names.length ; i++) {
names[i] = names[i].substring(0,1).toUpperCase()
+names[i].substring(1);
}
return String.join("", names);
}
}
첫 글자만 소문자를 대문자로 변경
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
public class JTextAreaEx01 extends JFrame {
private JPanel contentPane;
private JTextArea textArea;
private JScrollPane scrollPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextAreaEx01 frame = new JTextAreaEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextAreaEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btn = new JButton("New button");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//System.out.println(textArea.getText()); //입력불러오기
//overwriting
//textArea.setText("Hello TextArea\nHello TextArea\nHello TextArea\n"); //여러줄 ///editable-false 더이상 작성
//추가
textArea.append("Hello TextArea Hello TextAreaHello TextAreaHello TextArea \nHello TextArea\nHello TextArea\n");
}
});
btn.setBounds(26, 16, 117, 29);
contentPane.add(btn);
scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(36, 57, 262, 223);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
textArea.setEditable(false);
}
}
스크롤 만들기
Surround with - javax.swing.JScrollPane
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class JTextAreaEx02 extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JTextField tf2;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextAreaEx02 frame = new JTextAreaEx02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextAreaEx02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lbl1 = new JLabel("시작단");
lbl1.setBounds(6, 6, 44, 24);
contentPane.add(lbl1);
tf1 = new JTextField();
tf1.setBounds(64, 5, 260, 26);
contentPane.add(tf1);
tf1.setColumns(10);
JLabel lbl2 = new JLabel("끝단");
lbl2.setBounds(6, 40, 61, 16);
contentPane.add(lbl2);
tf2 = new JTextField();
tf2.setBounds(64, 35, 260, 24);
contentPane.add(tf2);
tf2.setColumns(10);
JButton btn = new JButton("구구단");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//값에 입력검사
int startDan = Integer.parseInt(tf1.getText());
int endDan = Integer.parseInt(tf2.getText());
//System.out.println(startDan);
//System.out.println(endDan);
System.out.println(doGugudan(startDan, endDan));
}
});
btn.setBounds(375, 6, 103, 24);
contentPane.add(btn);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(16, 68, 611, 456);
contentPane.add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
}
public String doGugudan(int stratDan, int endDan) {
String result = "";
for(int i=stratDan; i<=endDan; i++) {
for(int j=1; j<=9 ;j++) {
result += i + "X" + j + "=" +(i*j) + "\t";
}
result += "\n";
}
return result;
}
}
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class JEditorPainEx01 extends JFrame {
private JPanel contentPane;
private JEditorPane editorPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JEditorPainEx01 frame = new JEditorPainEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JEditorPainEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(23, 105, 510, 187);
contentPane.add(scrollPane);
editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
JButton btnNewButton = new JButton("New button");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//JTestArea
//editorPane.setText("Hello JeditorPane")
//html형식으로 보여
editorPane.setContentType("text/html");
//editorPane.setText("<html><body>Hello<br/><font color='blue'>JEditorPane</font></body></html>");
editorPane.setText("<html><body>Hello<br/><font color='blue'>JEditorPane</font><br /><table border ='1'><tr><td>1</td><<td>1</td><<td>1</td></tr></table></body></html>");
}
});
btnNewButton.setBounds(23, 41, 510, 29);
contentPane.add(btnNewButton);
}
}
html 형식으로 보여줌
editorPane - contentType - text/plain->text/html
테이블 입력도 가능
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class JEditorPainEx02 extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JTextField tf2;
private JEditorPane editorPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JEditorPainEx02 frame = new JEditorPainEx02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JEditorPainEx02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lbl1 = new JLabel("시작단");
lbl1.setBounds(6, 6, 44, 24);
contentPane.add(lbl1);
tf1 = new JTextField();
tf1.setBounds(64, 5, 260, 26);
contentPane.add(tf1);
tf1.setColumns(10);
JLabel lbl2 = new JLabel("끝단");
lbl2.setBounds(6, 40, 61, 16);
contentPane.add(lbl2);
tf2 = new JTextField();
tf2.setBounds(64, 35, 260, 24);
contentPane.add(tf2);
tf2.setColumns(10);
JButton btn = new JButton("구구단");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//값에 입력검사
int startDan = Integer.parseInt(tf1.getText());
int endDan = Integer.parseInt(tf2.getText());
//System.out.println(startDan);
//System.out.println(endDan);
//System.out.println(doGugudan(startDan, endDan));
editorPane.setText(doGugudan(startDan, endDan));
}
});
btn.setBounds(375, 6, 103, 24);
contentPane.add(btn);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(35, 94, 493, 374);
contentPane.add(scrollPane);
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
scrollPane.setViewportView(editorPane);
}
public String doGugudan(int stratDan, int endDan) {
String result = "<table border ='1' width='600' cellspacing='0'>";
for(int i=stratDan; i<=endDan; i++) {
result += "<tr>";
for(int j=1; j<=9 ;j++) {
result += i + "X" + j + "=" +(i*j) + "</td>";
}
result += "</tr>";
}
result += "</table>";
return result;
}
}
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class JOptionPaneEx01 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JOptionPaneEx01 frame = new JOptionPaneEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JOptionPaneEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btn1 = new JButton("메세지 다이얼로그");
btn1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//JOptionPane.showMessageDialog(JOptionPaneEx01.this, "메세지"); //경고창 static 메서
JOptionPane.showMessageDialog(JOptionPaneEx01.this, "메세지","타이틀", JOptionPane.WARNING_MESSAGE);
}
});
btn1.setBounds(31, 19, 157, 29);
contentPane.add(btn1);
JButton btn2 = new JButton("컴펌 다이얼로그");
btn2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//JOptionPane.showConfirmDialog(JOptionPaneEx01.this, "메세지");
int result = JOptionPane.showConfirmDialog(JOptionPaneEx01.this, "메세지","타이",JOptionPane.OK_CANCEL_OPTION);
//JOptionPane.showConfirmDialog(JOptionPaneEx01.this, "메세지","타이",JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.OK_OPTION ) {
System.out.println("OK 클릭");
}else if(result == JOptionPane.CANCEL_OPTION) {
System.out.println("Cancel 클릭");
}else {
System.out.println("기타 옵션");
}
}
});
btn2.setBounds(31, 58, 157, 29);
contentPane.add(btn2);
JButton btn3 = new JButton("입력 다이얼로그");
btn3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//JOptionPane.showInputDialog("메세지");
String msg = JOptionPane.showInputDialog("메시지","내용");
if(msg != null) {
System.out.println("확인 클릭");
}else {
System.out.println("기타 클릭");
}
}
});
btn3.setBounds(31, 99, 157, 29);
contentPane.add(btn3);
}
}
주의표시
확인, 취소, 닫기 별 입력값도 함께 받음
입력내용 받음
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
public class JRadioButtonEx01 extends JFrame {
private JPanel contentPane;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JRadioButtonEx01 frame = new JRadioButtonEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JRadioButtonEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JRadioButton rdbtn1 = new JRadioButton("사과");
buttonGroup.add(rdbtn1);
rdbtn1.setBounds(20, 6, 141, 23);
contentPane.add(rdbtn1);
JRadioButton rdbtn2 = new JRadioButton("딸기");
buttonGroup.add(rdbtn2);
rdbtn2.setBounds(20, 41, 141, 23);
contentPane.add(rdbtn2);
JRadioButton rdbtn3 = new JRadioButton("수박");
buttonGroup.add(rdbtn3);
rdbtn3.setBounds(20, 76, 141, 23);
contentPane.add(rdbtn3);
JButton btnNewButton = new JButton("New button");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String result = "";
if(rdbtn1.isSelected()) {
result = rdbtn1.getText();
}else if(rdbtn2.isSelected()){
result = rdbtn2.getText();
}else if(rdbtn3.isSelected()){
result = rdbtn3.getText();
}
System.out.println(result);
}
});
btnNewButton.setBounds(6, 110, 117, 29);
contentPane.add(btnNewButton);
}
}
단일선택
라디오 버튼 전체 선택 - 우클릭 - set ButtonGroup - New standard -버튼 항목별 selected(false)
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class JCheckboxEx01 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JCheckboxEx01 frame = new JCheckboxEx01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JCheckboxEx01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JCheckBox ch1 = new JCheckBox("사과");
ch1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("체크박스 클릭");
}
});
ch1.setSelected(true);
ch1.setBounds(27, 6, 128, 23);
contentPane.add(ch1);
JCheckBox ch2 = new JCheckBox("참외");
ch2.setBounds(27, 30, 128, 23);
contentPane.add(ch2);
JCheckBox ch3 = new JCheckBox("수박");
ch3.setBounds(27, 54, 128, 23);
contentPane.add(ch3);
JButton btn = new JButton("New button");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
/* System.out.println(ch1.isSelected());
System.out.println(ch2.isSelected());
System.out.println(ch3.isSelected());
System.out.println(ch1.getText());
System.out.println(ch2.getText());
System.out.println(ch3.getText()); */
String result = "";
if(ch1.isSelected() ) {
result += ch1.getText() + " ";
}
if(ch2.isSelected() ) {
result += ch2.getText() + " ";
}
if(ch3.isSelected() ) {
result += ch3.getText() + " ";
}
System.out.println(result);
}
});
btn.setBounds(27, 89, 117, 29);
contentPane.add(btn);
JButton btn2 = new JButton("전체선택");
btn2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(btn2.getText().equals("전체선택")) {
ch1.setSelected(true);
ch2.setSelected(true);
ch3.setSelected(true);
btn2.setText("전체해제");
}else{
ch1.setSelected(false);
ch2.setSelected(false);
ch3.setSelected(false);
btn2.setText("전체선택");
}
}
});
btn2.setBounds(27, 130, 117, 29);
contentPane.add(btn2);
}
}