Java swing, awt 기본적인 GUI2

실행결과


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Problem_Swing {

public static void main(String[] args) {
new Frame();
}

}

class Frame extends JFrame implements ActionListener {
JLabel name;
JTextField name_t;
JCheckBox ch1,ch2,ch3,ch4;
JTextArea area;
JButton input,print;
ArrayList<Mydata> mydatas = new ArrayList<>();
public Frame() {
super("문제");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 250, 450);
this.setLayout(new FlowLayout());

JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();

name = new JLabel("이름");
name_t = new JTextField(10);
ch1 = new JCheckBox("운동");
ch2 = new JCheckBox("음악");
ch3 = new JCheckBox("독서");
ch4 = new JCheckBox("게임");
area = new JTextArea(10, 20);
input = new JButton("입력");
print = new JButton("전체 출력");
input.addActionListener(this);
print.addActionListener(this);
jp1.setLayout(new FlowLayout());
jp2.setLayout(new FlowLayout());
jp3.setLayout(new FlowLayout());
jp4.setLayout(new FlowLayout());

jp1.add(name);
jp1.add(name_t);
jp2.add(ch1);
jp2.add(ch2);
jp2.add(ch3);
jp2.add(ch4);
jp3.add(area);
jp4.add(input);
jp4.add(print);


this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
String name="";
String str="";
String print_t="";
if(e.getSource()==input){
name = name_t.getText();
name_t.setText("");
if(ch1.isSelected()){
str += ch1.getText()+"\n";
ch1.setSelected(false);
}
if(ch2.isSelected()){
str += ch2.getText()+"\n";
ch2.setSelected(false);
}
if(ch3.isSelected()){
str += ch3.getText()+"\n";
ch3.setSelected(false);
}
if(ch4.isSelected()){
str += ch4.getText()+"\n";
ch4.setSelected(false);
}
Mydata d = new Mydata(name,str);
mydatas.add(d);
}
if(e.getSource()==print){
for(int i=0;i<mydatas.size();i++){
print_t += "이름 : "+mydatas.get(i).name+"\n";
print_t += "취미 : "+mydatas.get(i).hobby+"\n";
area.setText(print_t);
}

}

}

}
class Mydata {
String name="";
String hobby="";

public Mydata(String name, String hobby){
this.name = name;
this.hobby = hobby;
}
public void setHobby(String h){
this.hobby += h;
}
}


댓글

이 블로그의 인기 게시물

About JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

Spring Boot Actuator readiness, liveness probes on k8s

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째