응용 SoftWare/JAVA

JSplitPane 예제2

Hyun CHO 2016. 12. 21. 17:35

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JSplitPane;

import javax.swing.JTextArea;


public class JSplitPaneTest2 extends JFrame{

JSplitPane sp1, sp2;

public JSplitPaneTest2() {

sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new CalculatorT(), new SwingCalendar());

sp2.setDividerLocation(385);

sp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new Grimpan(), sp2);

sp1.setDividerLocation(300);

add(sp1, "Center");

setSize(800, 600);

setVisible(true);

}


public static void main(String[] args) {

new JSplitPaneTest2();


}


}

=====================================================================

그림판

import java.awt.BorderLayout;

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class Grimpan extends JPanel implements ActionListener {

JPanel p = new JPanel(new GridLayout(1, 0));

JButton penTool = new JButton("펜");

JButton lineTool = new JButton("선");

JButton ovalTool = new JButton("원");

JButton rectTool = new JButton("사각형");

MyCanvas mc = new MyCanvas();

JPanel p2 = new JPanel(new GridLayout(1, 0));

JButton red = new JButton("Red");

JButton green = new JButton("Green");

JButton blue = new JButton("Blue");

JButton yellow = new JButton("Yellow");

String tool = "선", color = "Blue";

public Grimpan() {

setLayout(new BorderLayout());

p.add(penTool); p.add(lineTool); p.add(ovalTool); p.add(rectTool);

add(p, "North");

add(mc, "Center");

red.setBackground(Color.red); green.setBackground(Color.GREEN); blue.setBackground(Color.BLUE); blue.setForeground(Color.WHITE); yellow.setBackground(Color.YELLOW);

p2.add(red); p2.add(green); p2.add(blue); p2.add(yellow);

add(p2, "South");

setSize(500, 500);

setVisible(true);

penTool.addActionListener(this);

lineTool.addActionListener(this);

ovalTool.addActionListener(this);

rectTool.addActionListener(this);

red.addActionListener(this);

green.addActionListener(this);

blue.addActionListener(this);

yellow.addActionListener(this);

}

class MyCanvas extends Canvas implements MouseListener, MouseMotionListener {

int firstX, firstY, lastX, lastY;

int x, y;

MyCanvas() {

addMouseListener(this);

addMouseMotionListener(this);

}

public void paint(Graphics g) {

// color

if (color.equals("Red"))

g.setColor(Color.RED);

else if (color.equalsIgnoreCase("Green"))

g.setColor(Color.GREEN);

else if (color.equals("Blue"))

g.setColor(Color.BLUE);

else if (color.equals("Yellow"))

g.setColor(Color.YELLOW);

// type of drawing tool

if (tool.equals("선"))

g.drawLine(firstX, firstY, lastX, lastY);

else if (tool.equalsIgnoreCase("원"))

g.drawOval(firstX, firstY, (lastX - firstX), (lastY - firstY));

else if (tool.equals("사각형"))

g.drawRect(firstX, firstY, lastX - firstX, lastY - firstY);

else if (tool.equals("펜")) {

g.drawLine(x, y, lastX, lastY);

x = lastX;

y = lastY;

}

}


public void update(Graphics g) {

paint(g);

}

@Override

public void mouseClicked(MouseEvent me) {};

@Override

public void mouseEntered(MouseEvent me) {};

@Override

public void mouseExited(MouseEvent me) {};

@Override

public void mousePressed(MouseEvent me) {

// System.out.println("pressed");

firstX = me.getX();

firstY = me.getY();

x = firstX;

y = firstY;

}

@Override

public void mouseReleased(MouseEvent me) {

// System.out.println("Released");

lastX = me.getX();

lastY = me.getY();

repaint();

}

// MouseMotionListener

@Override

public void mouseDragged(MouseEvent me) {

if (tool.equals("펜")) {

lastX = me.getX();

lastY = me.getY();

repaint();

}

}

@Override

public void mouseMoved(MouseEvent me) {};

}


@Override

public void actionPerformed(ActionEvent ae) {

String eventStr = ae.getActionCommand();

if (eventStr.equals("펜") || eventStr.equals("선") || eventStr.equals("원") || eventStr.equals("사각형"))

tool = eventStr;

else

color = eventStr;

// System.out.println("tool = " + tool + ", " + "color = " + color);

}


public static void main(String[] args) {

new Grimpan();

}

}

=====================================================================
달력
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwingCalendar extends JPanel implements ActionListener{
//North
JPanel topPane = new JPanel();
JButton prevBtn = new JButton("◀");
JButton nextBtn = new JButton("▶");
JLabel yearLbl = new JLabel("년");
JLabel monthLbl = new JLabel("월");
JComboBox<Integer> yearCombo = new JComboBox<Integer>();
DefaultComboBoxModel<Integer> yearModel = new DefaultComboBoxModel<Integer>();
JComboBox<Integer> monthCombo = new JComboBox<Integer>();
DefaultComboBoxModel<Integer> monthModel = new DefaultComboBoxModel<Integer>();
//Center
JPanel centerPane = new JPanel(new BorderLayout());
JPanel titlePane = new JPanel(new GridLayout(1, 7));
String titleStr[] = {"일", "월", "화", "수", "목", "금", "토"};
JPanel datePane = new JPanel(new GridLayout(0, 7));
Calendar now;
int year, month, date;
public SwingCalendar() {
setLayout(new BorderLayout());
//setDefaultCloseOperation(DISPOSE_ON_CLOSE); //자원 해제 후 종료
now = Calendar.getInstance(); //현재 날짜
year = now.get(Calendar.YEAR);
month = now.get(Calendar.MONTH)+1;
date = now.get(Calendar.DATE);
topPane.add(prevBtn);
for(int i=year-100; i<=year+50; i++){
yearModel.addElement(i);
}
yearCombo.setModel(yearModel);
yearCombo.setSelectedItem(year); //현재 년도 선택
topPane.add(yearCombo);
topPane.add(yearLbl);
for(int i=1; i<=12; i++){
monthModel.addElement(i);
}
monthCombo.setModel(monthModel);
monthCombo.setSelectedItem(month); //현재 월 선택
topPane.add(monthCombo);
topPane.add(monthLbl);
topPane.add(nextBtn);
topPane.setBackground(new Color(100, 200, 200));
add(topPane, "North");
//Center
titlePane.setBackground(Color.white);
for(int i=0; i<titleStr.length; i++){
JLabel lbl = new JLabel(titleStr[i], JLabel.CENTER);
if(i == 0){
lbl.setForeground(Color.red);
}else if(i == 6){
lbl.setForeground(Color.blue);
}
titlePane.add(lbl);
}
centerPane.add(titlePane, "North");
//날짜 출력
dayPrint(year, month);
centerPane.add(datePane, "Center");
add(centerPane, "Center");
setSize(400, 300);
setVisible(true);
prevBtn.addActionListener(this);
nextBtn.addActionListener(this);
yearCombo.addActionListener(this);
monthCombo.addActionListener(this);
}
//Overring
public void actionPerformed(ActionEvent ae){
Object obj = ae.getSource();
if(obj instanceof JButton){
JButton eventBtn = (JButton)obj;
int yy = (Integer)yearCombo.getSelectedItem();
int mm = (Integer)monthCombo.getSelectedItem();
if(eventBtn.equals(prevBtn)){ //전달
if(mm==1){
yy--; mm=12;
}else{
mm--;
}
}else if(eventBtn.equals(nextBtn)){ //다음달
if(mm==12){
yy++; mm=1;
}else{
mm++;
}
}
yearCombo.setSelectedItem(yy);
monthCombo.setSelectedItem(mm);
}else if(obj instanceof JComboBox){ //콤보박스 이벤트 발생시
createDayStart();
}
}
public void createDayStart(){
datePane.setVisible(false); //패널 숨기기
datePane.removeAll(); //날짜 출력한 라벨 지우기
dayPrint((Integer)yearCombo.getSelectedItem(), (Integer)monthCombo.getSelectedItem());
datePane.setVisible(true); //패널 재출력
}
public void dayPrint(int y, int m){
Calendar cal = Calendar.getInstance();
cal.set(y, m-1, 1); //출력할 첫날의 객체 만든다.
int week = cal.get(Calendar.DAY_OF_WEEK); //1일에 대한 요일 일요일 : 0
int lastDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //그 달의 마지막 날
for(int i=1; i<week; i++){ //날짜 출력 전까지의 공백 출력
datePane.add(new JLabel(" "));
}
for(int i=1; i<=lastDate; i++){
JLabel lbl = new JLabel(String.valueOf(i), JLabel.CENTER);
cal.set(y, m-1, i);
int outWeek = cal.get(Calendar.DAY_OF_WEEK);
if(outWeek==1){
lbl.setForeground(Color.red);
}else if(outWeek==7){
lbl.setForeground(Color.BLUE);
}
datePane.add(lbl);
}
}
//public static void main(String[] args) {
// new SwingCalendar();

//}

}
=====================================================================
계산기
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalculatorT extends JPanel implements ActionListener {
JFrame jf = new JFrame("계산기");
JLabel jl = new JLabel("0.0", JLabel.RIGHT);
//버튼틀
JPanel jp = new JPanel();
String butValue[] = {"Clear","BackSpace","End","","7","8","9","+","4","5","6","-",
"1","2","3","*","0",".","=","/"};
JButton jb[] = new JButton[20];
public CalculatorT() {
//라벨 추가
//add(jl, "North"); //BorderLayout.NORTH
setLayout(new BorderLayout());
//버튼
jp.setLayout(new GridLayout(5, 4));
for(int i=0; i<butValue.length; i++){ //버튼 생성, 패널 추가
jb[i] = new JButton(butValue[i]);
jp.add(jb[i]); //패널추가
jb[i].addActionListener(this); //이벤트 등록
}
add(jp, BorderLayout.CENTER);
//jf.setSize(400, 400);
//jf.setVisible(true);
}
//오버라이딩
public void actionPerformed(ActionEvent ae){}

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

}