가로세로 쭉 늘리기
this.setLayout(new FlowLayout());
클릭시 백그라운드 색상 변경
actionlistner 안에
컨테이너 변수.setBackground(Color.색상);
내부클래스로 actionListenr 추가
버튼변수.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cp.setBackground(Color.magenta);
}
});
public class SwingEvent_07 extends JFrame {
Container cp;
JButton btn1,btn2;
public SwingEvent_07(String title) {
super(title);
cp=this.getContentPane();
this.setBounds(200, 100, 300, 400);
cp.setBackground(new Color(255,255,100));
setDesign();
setVisible(true);
}
public void setDesign() {
this.setLayout(new FlowLayout());//가로세로 쭉 늘리는거
//버튼 1생성 이벤트
btn1=new JButton("배경색을 핫핑크로 변경");
this.add(btn1);
//직접 익명 내부클래스로 actionlistener 추가
btn1.addActionListener(new ActionListener() {
//버튼 클릭시 색변경
@Override
public void actionPerformed(ActionEvent e) {
cp.setBackground(Color.magenta);
}
});
//버튼 2생성 이벤트
btn2=new JButton("배경색을 그레이로 변경");
this.add(btn2);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cp.setBackground(Color.gray);
}
});
}
public static void main(String[] args) {
new SwingEvent_07("스윙이벤트_7");
}
}
버튼배열 갯수 선언
JButton btn[]=new JButton[6];
색상 라벨(이름) 배열
String btnLable[]= {"Red","Green","Gray","Cyan","Yellow","White"};
버튼 눌렀을 때 변경되는 색상 배열
Color btnColor[]= {Color.red,Color.green,Color.gray,Color.cyan,Color.yellow,Color.white};
panel 기본이 Flowlayout
JPanel panel=new JPanel();
panel.setBackground(색상);
프레임에 패널을 추가할땐 상단 하단 선택
프레임 위쪽 패널 추가 /조그맣게 나오지만 버튼 추가하면 커짐
this.add(panel,BorderLayout.방향);
for(int i=0;i<btn.length;i++)
{
btn[i]=new JButton(btnLable[i]);
//버튼색
btn[i].setBackground(btnColor[i]);
btn[i].setOpaque(true);
btn[i].setBorderPainted(false);
//panel에 추가
panel.add(btn[i]);
//버튼6개에 이벤트추가
btn[i].addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
for(int i=0;i<btn.length;i++)
{
if(ob==btn[i])
{
//색바꾸기
cp.setBackground(btnColor[i]);
//제목바꾸기
this.setTitle("color: "+btnLable[i]);
}
}
}
원문
public class SwingArrPanel_08 extends JFrame implements ActionListener {
Container cp;
JButton btn[]=new JButton[6];
String btnLable[]= {"Red","Green","Gray","Cyan","Yellow","White"};
Color btnColor[]= {Color.red,Color.green,Color.gray,Color.cyan,Color.yellow,Color.white};
public SwingArrPanel_08(String title) {
super(title);
cp=this.getContentPane();
this.setBounds(200, 100, 500, 400);
cp.setBackground(new Color(255,255,200));
setDesign();
setVisible(true);
}
public void setDesign()
{
//panel 기본이 Flowlayout
JPanel panel=new JPanel();
panel.setBackground(Color.orange);
//프레임에 패널을 추가할땐 상단 하단 선택
//프레임 위쪽 패널 추가 /조그맣게 나오지만 버튼 추가하면 커짐
this.add(panel,BorderLayout.NORTH);
//버튼생성
for(int i=0;i<btn.length;i++)
{
btn[i]=new JButton(btnLable[i]);
//버튼색
btn[i].setBackground(btnColor[i]);
btn[i].setOpaque(true);
btn[i].setBorderPainted(false);
//panel에 추가
panel.add(btn[i]);
//버튼6개에 이벤트추가
btn[i].addActionListener(this);
}
}
public static void main(String[] args) {
new SwingArrPanel_08("스윙 배열연습");
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
for(int i=0;i<btn.length;i++)
{
if(ob==btn[i])
{
//색바꾸기
cp.setBackground(btnColor[i]);
//제목바꾸기
this.setTitle("color: "+btnLable[i]);
}
}
}
}
단순히 이름 붙이는 것
결과 나오는 곳, 숫자1제목,숫자2제목
JLabel lblResult,lbl1,lbl2;
입력하는 곳
JTextField tfSu1,tfSu2;
이벤트 발생버튼
JButton btnAdd;
레이아웃 초기화
this.setLayout(null);
버튼에 이름 지정
lbl1=new JLabel("숫자1");
lbl2=new JLabel("숫자2");
숫자입력제목 지정 및 추가
lbl1.setBounds(20, 20, 50, 30);
this.add(lbl1);
lbl2.setBounds(20, 60, 50, 30);
this.add(lbl2);
숫자 입력 박스 크기 및 추가
tfSu1=new JTextField();
tfSu1.setBounds(70, 20, 60, 30);
this.add(tfSu1);
버튼추가
btnAdd=new JButton("숫자 더하기");
btnAdd.setBounds(30, 120, 150, 30);
this.add(btnAdd);
결과 값 나오는 곳
lblResult=new JLabel("결과 나오는 곳");
lblResult.setBounds(10, 180, 250, 60);
this.add(lblResult);
Text 값을 가져오는건 getText
입력값 가져와서 int로 변환
int su1=Integer.parseInt(tfSu1.getText());
int su2=Integer.parseInt(tfSu2.getText());
int 변수 생성후 값 지정
int sum=su1+su2;
값 지정 한 것 String 변수 선언 후 대입
//String s=String.valueOf(sum);
//String s=sum+"";
String s=su1+"+"+su2+"="+sum;
결과라벨지에 수정(set) text값을 String s로 수정
결과값 화면에 수정 후 출력
lblResult.setText(s);
//이벤트가 1개라 익명내부클래스로 만들기
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
if(ob==btnAdd)
{
/*
int sum;
sum=Integer.parseInt(tfSu1.getText())+Integer.parseInt(tfSu2.getText());
lblResult.setText(sum+"");
*/
//Text 값을 가져오는건 getText
int su1=Integer.parseInt(tfSu1.getText());
int su2=Integer.parseInt(tfSu2.getText());
int sum=su1+su2;
//String s=String.valueOf(sum);
//String s=sum+"";
String s=su1+"+"+su2+"="+sum;
//결과라벨지에 수정 text값을 String s로 수정
lblResult.setText(s);
}
}
});
public class SwingCalc_10 extends JFrame {
Container cp;
//단순히 이름 붙이는 것
JLabel lblResult,lbl1,lbl2; //결과 나오는 곳, 숫자1제목,숫자2제목
JTextField tfSu1,tfSu2; //입력하는 곳
JButton btnAdd; //이벤트 발생버튼
public SwingCalc_10(String title) {
super(title);
cp=this.getContentPane();
this.setBounds(200, 100, 300, 400);
cp.setBackground(new Color(255,255,200));
setDesign();
setVisible(true);
}
public void setDesign()
{
this.setLayout(null);
lbl1=new JLabel("숫자1");
lbl2=new JLabel("숫자2");
lbl1.setBounds(20, 20, 50, 30);
this.add(lbl1);
lbl2.setBounds(20, 60, 50, 30);
this.add(lbl2);
tfSu1=new JTextField();
tfSu1.setBounds(70, 20, 60, 30);
this.add(tfSu1);
tfSu2=new JTextField();
tfSu2.setBounds(70, 60, 60, 30);
this.add(tfSu2);
//버튼추가
btnAdd=new JButton("숫자 더하기");
btnAdd.setBounds(30, 120, 150, 30);
this.add(btnAdd);
//결과
lblResult=new JLabel("결과 나오는 곳");
lblResult.setBounds(10, 180, 250, 60);
this.add(lblResult);
//이벤트가 1개라 익명내부클래스로 만들기
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
if(ob==btnAdd)
{
/*
int sum;
sum=Integer.parseInt(tfSu1.getText())+Integer.parseInt(tfSu2.getText());
lblResult.setText(sum+"");
*/
//Text 값을 가져오는건 getText
int su1=Integer.parseInt(tfSu1.getText());
int su2=Integer.parseInt(tfSu2.getText());
int sum=su1+su2;
//String s=String.valueOf(sum);
//String s=sum+"";
String s=su1+"+"+su2+"="+sum;
//결과라벨지에 수정 text값을 String s로 수정
lblResult.setText(s);
}
}
});
}
public static void main(String[] args) {
new SwingCalc_10("간단계산 연습");
}
}
패널을 프레임의 센터에 추가
gridlayout으로 3행3열 생성
JPanel pCenter=new JPanel(new GridLayout(3,3));
this.add(pCenter,BorderLayout.CENTER);
for(int i=0;i<lblName.length;i++)
{
lblName[i]=new JLabel(str[i],JLabel.CENTER);
색상 추가
int r=(int)(Math.random()*256); //rgb컬러 0~255
int g=(int)(Math.random()*256); //rgb컬러 0~255
int b=(int)(Math.random()*256); //rgb컬러 0~255
lblName[i].setBackground(new Color(r,g,b));
//panel에 label추가
pCenter.add(lblName[i]);
lblName[i].setOpaque(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//Object ob=e.getSource();와 if(ob==버튼)은 버튼이 여러개일때 조건 넣어주려고한다
for(int i=0;i<lblName.length;i++)
{
int r=(int)(Math.random()*256);
int g=(int)(Math.random()*256);
int b=(int)(Math.random()*256);
lblName[i].setBackground(new Color(r,g,b));
}
}
public class SwingRandom_11 extends JFrame implements ActionListener{
Container cp;
JLabel lblName[]=new JLabel[9];//3행3열 생성
String str[]= {"라오스","프랑스","독일","미국","호주","뉴질랜드","오스트리아","동유럽","대만"};
JButton btn;
public SwingRandom_11(String title) {
super(title);
cp=this.getContentPane();
this.setBounds(200, 100, 500, 550);
cp.setBackground(new Color(255,255,200));
setDesign();
setVisible(true);
}
public void setDesign()
{
//패널을 프레임의 센터에 추가 //gridlayout으로 3행3열 생성
JPanel pCenter=new JPanel(new GridLayout(3,3));
this.add(pCenter,BorderLayout.CENTER);
//라벨을 패널에 추가
for(int i=0;i<lblName.length;i++)
{
lblName[i]=new JLabel(str[i],JLabel.CENTER);
int r=(int)(Math.random()*256); //rgb컬러 0~255
int g=(int)(Math.random()*256); //rgb컬러 0~255
int b=(int)(Math.random()*256); //rgb컬러 0~255
lblName[i].setBackground(new Color(r,g,b));
//panel에 label추가
pCenter.add(lblName[i]);
lblName[i].setOpaque(true);
}
//버튼생성
btn=new JButton("라벨색상변경");
this.add(btn,BorderLayout.SOUTH);
btn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//Object ob=e.getSource();와 if(ob==버튼)은 버튼이 여러개일때 조건 넣어주려고한다
for(int i=0;i<lblName.length;i++)
{
int r=(int)(Math.random()*256);
int g=(int)(Math.random()*256);
int b=(int)(Math.random()*256);
lblName[i].setBackground(new Color(r,g,b));
}
}
public static void main(String[] args) {
new SwingRandom_11("랜덤 색상 구하기");
}
}