JAVA

10일차

구자룡 2021. 4. 7. 19:54

//텍스트필드와 textarea에 대한 GUI예제.

import java.awt.*;

import javax.swing.JFrame;
public class Exam31 extends JFrame{
	private Label title_lb = new Label("제목",Label.RIGHT);
	private TextField title_tf = new TextField();
	private TextArea contents_ta = new TextArea();
	private Button register_bt = new Button("글등록");
	
	private BorderLayout bl = new BorderLayout(10,10);
	
	//North부분에 대해..배치는 
	private Panel p = new Panel();
	private BorderLayout bl1 = new BorderLayout();
	
	//South부분에 배치..
	private Panel p1 = new Panel();
	private FlowLayout fl = new FlowLayout(FlowLayout.RIGHT);
	
	public Exam31(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		super.setSize(500,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.init();
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);		
	}
	public void init() {
		this.setLayout(bl);
		p.setLayout(bl1);
		p.add("West",title_lb);
		p.add("Center",title_tf);
		this.add("North",p);
		this.add("Center",contents_ta);
		
		p1.setLayout(fl);
		p1.add(register_bt);
		this.add("South",p1);
	}
	
	public static void main(String[] ar) {
		new Exam31("제목");
		
	}

}

//다이아로그 GUI구성 프로그램.

import java.awt.*;
public class Exam32 extends Frame{
	private Dialog dlg = new Dialog(this,"경고",true);//이 true는 프레임에 귀속된다는 뜻임.
	private Label msg_lb = new Label("경고입니다.",Label.CENTER);
	private Button ok_bt = new Button("OK");
	
	
	private BorderLayout bl = new BorderLayout();
	private Panel p = new Panel();
	private GridBagLayout gbl = new GridBagLayout();
	
	public Exam32(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		super.setSize(500,300);
		
		this.init();
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);
		
		dlg.setLocation(300,400);
		dlg.setSize(100,80);
		dlg.setVisible(true);
	}
	public void init(){
		dlg.setLayout(bl);//경고창의 레이아웃이 됨.
		dlg.add("Center",msg_lb);
		
		p.setLayout(gbl);//중앙에 배치하기 위함.
		p.add(ok_bt);
		dlg.add("South",p);
	}
	
	public static void main(String[] ar) {
		new Exam32("제목");
		
	}

}

//File Dialog에 대한 예제임.

import java.awt.*;
public class Exam33 extends Frame{
//	private FileDialog fdlg = new FileDialog(this,"저장",FileDialog.SAVE);
	private FileDialog fdlg = new FileDialog(this,"열기",FileDialog.LOAD);
	public Exam33(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		super.setSize(500,300);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);
		
		fdlg.setLocation(200,300);
		fdlg.setVisible(true);
	}
	
	public static void main(String[] ar) {
		new Exam33("제목");
		
	}

}

//메뉴와 메뉴아이템에 대한 GUI구성 프로그램.

import java.awt.*;
public class Exam34 extends Frame{
	private MenuBar mb = new MenuBar();
	private Menu file = new Menu("FILE");
	private MenuItem fnew = new MenuItem("NEW");
	private MenuItem fsave = new MenuItem("SAVE");
	private MenuItem fopen = new MenuItem("OPEN");
	private MenuItem fexit = new MenuItem("EXIT");
	
	private Menu edit = new Menu("EDIT");
	private Menu ecolor = new Menu("COLOR");
	//메뉴의 메뉴를 사용하면 화살표가 나오는데 체크박스아이템을 사용한다.
	private CheckboxMenuItem red_cb = new CheckboxMenuItem("RED");
	private CheckboxMenuItem green_cb = new CheckboxMenuItem("GREEN");
	private CheckboxMenuItem blue_cb = new CheckboxMenuItem("BLUE");
	private Menu esize = new Menu("ESIZE");
	
	private Menu help = new Menu("HELP");
	private MenuItem hver = new MenuItem("VERSION");
	
	public Exam34(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		super.setSize(500,300);
		
		this.init();
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);		
	}
	public void init() {
		file.add(fnew);
		file.addSeparator();
		file.add(fsave);file.add(fopen);file.add(fexit);
		mb.add(file);
		
		ecolor.add(red_cb);
		ecolor.add(green_cb);
		ecolor.add(blue_cb);
		
		edit.add(ecolor);//메뉴에 메뉴를 사용하여 화살표가 생긴경우임.
		edit.add(esize);
		edit.addSeparator();
		edit.add(ecolor);
		mb.add(edit);
		
		help.add(hver);
		mb.add(help);
		this.setMenuBar(mb);
	}
	
	public static void main(String[] ar) {
		new Exam34("제목");
		
	}

}

import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Exam38 extends JFrame
{
 JTextField inputText;
 String names[] = {"C", "/", "*", "-" ,"7", "8", "9", "+", "4", "5", "6", "=", "1", "2", "3", "0"};
 
 public Exam38()
 {
  super("자바 계산기");
  
  BorderLayout layout = new BorderLayout(2, 2); 
  setLayout(layout); 
    
  inputText = new JTextField("", SwingConstants.CENTER); 
  inputText.setHorizontalAlignment(JTextField.RIGHT); 
  inputText.setFont(new Font(Font.SERIF, Font.BOLD, 40));
  inputText.setBounds(0,0,450,50);

  JPanel pad = new JPanel(new GridLayout(4, 4));
  JButton buttons[] = new JButton[names.length];

  for(int count = 0; count < names.length; count++)
  {
    buttons[count] = new JButton(names[count]);
    pad.add(buttons[count]);
  }  
  
  add(inputText, BorderLayout.NORTH);
  add(pad, BorderLayout.CENTER);
 }
 
 public static void main(String args[])
 {
    Exam38 cal = new Exam38(); 
  
  cal.setSize(300, 400); 
  cal.setResizable(false);
  cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  cal.setVisible(true);
 }
} 

'JAVA' 카테고리의 다른 글

12일차  (0) 2021.04.08
11일차  (0) 2021.04.08
9일차  (0) 2021.04.07
8일차  (0) 2021.04.05
7일차  (0) 2021.03.12