- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 구글 로그인
- sql
- 프로그래밍 입문
- C언어
- 안드로이드 스튜디오
- 정렬
- til
- 배열
- Java
- Firebase
- 알고리즘
- 연결리스트
- python
- C++
- oauth
- 파이썬
- 공유대학
- 자바
- 동적할당
- android studio
- 프로그래머스
- 안드로이드
- 자료구조
- 코딩테스트
- firebase google
- 비주얼 베이직
- 백준
- 로그인
- 컴퓨터공학과
Archives
코딩하는 해달이
[JAVA] Swing을 이용한 프로그램 예제 (1) 본문
학교 수업때 들은 Swing을 이용해서 성적관리 프로그램을 만들어 보았다.
//FinalTest.java
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class FinalTest extends JFrame {
private FinalTest owner;
private JMenuBar jMenuBar;
private JMenu jMenu;
private JMenu jMenuProgramInfo;
private JMenuItem jMenuItemAverage = new JMenuItem("점수통계보기");
private JMenuItem jMenuItemExit = new JMenuItem("종료");
private JMenuItem jMenuItemExplane = new JMenuItem("설명");
private JPanel inputPane;
private JTextField tbName = new JTextField(7);
private ButtonGroup rbGroup = new ButtonGroup();
private JRadioButton rbFemale = new JRadioButton("여", true);
private JRadioButton rbMale = new JRadioButton("남");
private String[] cbGradeContent = {"1학년", "2학년", "3학년", "4학년"};
private JComboBox cbGrade = new JComboBox(cbGradeContent);
private String[] cbScoreContent = {"100", "90", "80", "70", "60", "50"};
private JComboBox cbMathScore = new JComboBox(cbScoreContent);
private JComboBox cbScienceScore = new JComboBox(cbScoreContent);
private JButton btnAdd = new JButton("추가하기");
private static ArrayList<StudentStatus> studentList = new ArrayList<>();
private JScrollPane listScrollPane;
private DefaultListModel listContent = new DefaultListModel();
private JList jList = new JList(listContent);
FinalTest() {
owner = this;
this.setTitle("FinalTest");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
this.setJMenuBar(getJMenuBar());
c.add(getInputPane(), BorderLayout.NORTH);
c.add(getListScrollPane(), BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
}
//메뉴 바 설정========================================================================================================
@Override
public JMenuBar getJMenuBar() { //메뉴 바
if(jMenuBar == null) {
jMenuBar = new JMenuBar();
jMenuBar.add(getJMenu());
jMenuBar.add(getJMenuProgramInfo());
}
return jMenuBar;
}
public JMenu getJMenu() { //메뉴 - 메뉴
if(jMenu == null) {
jMenu = new JMenu("메뉴");
jMenu.add(jMenuItemAverage);
jMenu.add(jMenuItemExit);
jMenuItemAverage.addActionListener(menuActionListener);
jMenuItemExit.addActionListener(menuActionListener);
}
return jMenu;
}
public JMenu getJMenuProgramInfo() { //메뉴 - 프로그램 정보
if(jMenuProgramInfo == null) {
jMenuProgramInfo = new JMenu("프로그램정보");
jMenuProgramInfo.add(jMenuItemExplane);
jMenuItemExplane.addActionListener(menuActionListener);
}
return jMenuProgramInfo;
}
private ActionListener menuActionListener = new ActionListener() { //메뉴 클릭 리스너
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jMenuItemExit) {
System.exit(0);
} else if (e.getSource() == jMenuItemAverage) {
MenuAverageDialog menuAverageDialog = new MenuAverageDialog(calcAverage());
} else if (e.getSource() == jMenuItemExplane) {
JOptionPane.showMessageDialog(null, "이 프로그램은 Swing을 이용한 성적관리 프로그램입니다.", "프로그램설명", JOptionPane.INFORMATION_MESSAGE);
}
}
};
private double[] calcAverage() {
double mSum = 0;
double sSum = 0;
double[] result = new double[2];
result[0] = 0;
result[1] = 0;
for(int i = 0; i < studentList.size(); i++) {
mSum += studentList.get(i).getMathScore();
sSum += studentList.get(i).getScienceScore();
}
result[0] = mSum / studentList.size();
result[1] = sSum / studentList.size();
return result;
}
//입력 팬 설정========================================================================================================
public JPanel getInputPane() { //입력 팬 구성
if (inputPane == null) {
inputPane = new JPanel();
GridLayout gridLayout = new GridLayout(2,3);
inputPane.setLayout(gridLayout);
JPanel inputNamePanel = new JPanel();
inputNamePanel.add(new JLabel("성명"));
inputNamePanel.add(tbName);
JPanel inputSexPanel = new JPanel();
rbGroup.add(rbFemale);
rbGroup.add(rbMale);
inputSexPanel.add(rbFemale, BorderLayout.WEST);inputSexPanel.add(rbMale, BorderLayout.WEST);
JPanel inputGradePanel = new JPanel();
inputGradePanel.add(new JLabel("학년"));
inputGradePanel.add(cbGrade);
JPanel inputMathScorePanel = new JPanel();
inputMathScorePanel.add(new JLabel("수학점수"));
cbMathScore.setSelectedIndex(1);
inputMathScorePanel.add(cbMathScore);
JPanel inputScienceScorePanel = new JPanel();
inputScienceScorePanel.add(new JLabel("과학점수"));
cbScienceScore.setSelectedIndex(1);
inputScienceScorePanel.add(cbScienceScore);
inputPane.add(inputNamePanel);
inputPane.add(inputSexPanel);
inputPane.add(inputGradePanel);
inputPane.add(inputMathScorePanel);
inputPane.add(inputScienceScorePanel);
inputPane.add(btnAdd);
btnAdd.setSize(5,3);
btnAdd.addActionListener(btnActionListener);
}
return inputPane;
}
public ActionListener btnActionListener = new ActionListener() { //버튼 클릭 리스너
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(tbName.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "성명을 입력하세요", "오류", JOptionPane.ERROR_MESSAGE);
} else {
String sex;
if (rbMale.isSelected()) {
sex = "남";
} else {
sex = "여";
}
StudentStatus student = new StudentStatus(tbName.getText(), cbGrade.getSelectedItem().toString(), sex, Integer.valueOf(cbMathScore.getSelectedItem().toString()), Integer.valueOf(cbScienceScore.getSelectedItem().toString()));
studentList.add(student);
listContent.addElement(student.getContent());
getContentPane().repaint();
}
}
};
// 리스트 팬 설정 ====================================================================================================
public JScrollPane getListScrollPane() {
if(listScrollPane == null) {
listScrollPane = new JScrollPane(jList);
jList.addListSelectionListener(listSelectionListener);
listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
listScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
return listScrollPane;
}
public ListSelectionListener listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(jList.getSelectedIndex() == -1) {
return;
}
JListClickDialog jDialog = new JListClickDialog(owner, studentList, jList, listContent);
jDialog.setVisible(true);
getListScrollPane();
}
};
public static void main(String[] args) {
FinalTest JFrame = new FinalTest();
}
}
//StudentStatus.java
public class StudentStatus {
private String name;
private String grade;
private String sex;
private int mathScore;
private int scienceScore;
private String content;
StudentStatus(String name, String grade, String sex, int mathScore , int scienceScore) {
this.name = name;
this.grade = grade;
this.sex = sex;
this.mathScore = mathScore;
this.scienceScore = scienceScore;
this.content = name + "(" + grade + "," + sex + ")" + "수학=" + mathScore + ",과학=" + scienceScore;
}
public String getName() {
return name;
}
public String getGrade() {
return grade;
}
public String getSex() {
return sex;
}
public int getMathScore() {
return mathScore;
}
public int getScienceScore() {
return scienceScore;
}
public String getContent() { return content; }
}
//JListClickDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class JListClickDialog extends JDialog {
private ArrayList<StudentStatus> studentList;
private StudentStatus student;
private JList jList;
private int index;
private DefaultListModel listContent;
private JPanel statusPane;
private JTextField tbName = new JTextField(7);
private String[] cbScoreContent = {"100", "90", "80", "70", "60", "50"};
private JComboBox cbMathScore = new JComboBox(cbScoreContent);
private JComboBox cbScienceScore = new JComboBox(cbScoreContent);
private JPanel buttonPane;
private JButton btnCancel;
private JButton btnDelete;
private JButton btnRetouch;
private JFrame owner;
public JListClickDialog(JFrame owner, ArrayList<StudentStatus> studentList, JList jList, DefaultListModel listContent) {
this.owner = owner;
this.studentList = studentList;
this.jList = jList;
this.index = jList.getSelectedIndex();
this.student = studentList.get(index);
this.listContent = listContent;
setTitle("항목 수정, 삭제");
setSize(400,150);
setModal(true);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.add(getStatusPane(), BorderLayout.NORTH);
c.add(getButtonPane(),BorderLayout.SOUTH);
setLocation(
owner.getLocationOnScreen().x + (owner.getWidth()-getWidth())/2,
owner.getLocationOnScreen().y + (owner.getHeight()-getHeight())/2
);
}
public JPanel getStatusPane() {
if (statusPane == null) {
statusPane = new JPanel();
statusPane.add(new JLabel("성명"));
tbName.setEnabled(false);
statusPane.add(tbName);
tbName.setText(student.getName());
statusPane.add(new JLabel("수학점수"));
statusPane.add(cbMathScore);
cbMathScore.getModel().setSelectedItem(student.getMathScore());
statusPane.add(new JLabel("과학점수"));
statusPane.add(cbScienceScore);
cbScienceScore.getModel().setSelectedItem(student.getScienceScore());
}
return statusPane;
}
public JPanel getButtonPane() {
if (buttonPane == null) {
buttonPane = new JPanel();
btnCancel = new JButton("취소");
btnDelete = new JButton("삭제");
btnRetouch = new JButton("수정");
buttonPane.add(btnCancel);
btnCancel.addActionListener(buttonListener);
buttonPane.add(btnDelete);
btnDelete.addActionListener(buttonListener);
buttonPane.add(btnRetouch);
btnRetouch.addActionListener(buttonListener);
}
return buttonPane;
}
public ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCancel) {
dispose();
} else if (e.getSource() == btnDelete) {
dispose();
studentList.remove(index);
listContent = new DefaultListModel();
for(int i = 0; i < studentList.size(); i++) {
listContent.addElement(studentList.get(i).getContent());
}
jList.removeAll();
jList.setModel(listContent);
} else if (e.getSource() == btnRetouch) {
}
}
};
}
//MenuAverageDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuAverageDialog extends JDialog {
double mAverage;
double sAverage;
private Graph averageGraph;
private final JButton okButton = new JButton("확인");
MenuAverageDialog(double[] averages) {
this.mAverage = averages[0];
this.sAverage = averages[1];
setTitle("점수 통계 보기");
setSize(400,300);
Container c = getContentPane();
c.add(drawGraph(),BorderLayout.CENTER);
setVisible(true);
}
private Graph drawGraph() {
if(averageGraph == null) {
averageGraph = new Graph();
}
return averageGraph;
}
private class Graph extends JPanel {
public Graph() {
setLayout(null);
okButton.setSize(100,30);
okButton.addActionListener(btnListener);
add(okButton);
okButton.setLocation(150, 220);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(40,190,360,190); //x축
g.drawLine(60,20,60,210); //y축
g.drawLine(58,160,62,160); //구분선 50
g.drawLine(58,130,62,130); //~
g.drawLine(58,100,62,100); //~
g.drawLine(58,70,62,70); //~
g.drawLine(58,40,62,40); //구분선 100
g.drawString("50",42,190);
g.drawString("60",42,160);
g.drawString("70",42,130);
g.drawString("80",42,100);
g.drawString("90",42,70);
g.drawString("100",36,40);
g.setColor(Color.BLUE);
g.drawString("수학",150,210); //범례 수학
g.drawString(String.valueOf((int)mAverage), 150,20); //값 수학
g.fillRect(150, ((int)mAverage * -3 + 340), 22, 190 - ((int)mAverage * -3 + 340)); //그래프 수학
g.setColor(Color.GREEN);
g.drawString("과학",250,210); //범례 과학
g.drawString(String.valueOf((int)sAverage), 250,20); //값 과학
g.fillRect(250, ((int)sAverage * -3 + 340), 22, 190 - ((int)sAverage * -3 + 340)); //그래프 과학
}
}
private ActionListener btnListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
};
}
반응형
Comments