티스토리 뷰
반응형
✏️ 14장 실습문제 - 1번
package ex;
import javax.swing.*;
public class SolveProblem extends JFrame {
public SolveProblem(){
setTitle("메뉴 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
setSize(400,200);
setVisible(true);
}
private void createMenu(){
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("파일");
JMenu edit = new JMenu("편집");
JMenu menu = new JMenu("보기");
JMenu input = new JMenu("입력");
JMenuItem blow = new JMenuItem("화면확대");
JMenuItem outline = new JMenuItem("쪽윤곽");
menu.add(blow);
menu.add(outline);
menuBar.add(file);
menuBar.add(edit);
menuBar.add(menu);
menuBar.add(input);
setJMenuBar(menuBar);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 2번
package ex;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyPanel extends JPanel{
private ImageIcon imageIcon;
private Image image;
public MyPanel(String string) {
imageIcon = new ImageIcon(string);
image= imageIcon.getImage();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,this.getWidth(),this.getHeight(),this);
}
}
public class SolveProblem extends JFrame {
public SolveProblem(){
setTitle("메뉴로 배경 이미지 로딩하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
setSize(400,200);
setVisible(true);
}
private void createMenu(){
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("파일");
JMenuItem menuItem = new JMenuItem("열기");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images","jpg","gif");
fileChooser.setFileFilter(filter);
int ret = fileChooser.showOpenDialog(null);
if(ret!=JFileChooser.APPROVE_OPTION){
JOptionPane.showMessageDialog(null,"파일을 선택하지 않았습니다","경고",JOptionPane.WARNING_MESSAGE);
return;
}
String filePath = fileChooser.getSelectedFile().getPath();
MyPanel panel = new MyPanel(filePath);
setContentPane(panel);
}
});
menu.add(menuItem);
menuBar.add(menu);
setJMenuBar(menuBar);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 3번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private Container container;
public SolveProblem(){
setTitle("메뉴로 배경 이미지 로딩하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = getContentPane();
createMenu();
setSize(400,200);
setVisible(true);
}
private void createMenu(){
JToolBar toolBar = new JToolBar("툴바");
JButton button = new JButton("종료");
toolBar.setBackground(Color.LIGHT_GRAY);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null,"정말 종료하시겠습니까?","Confirm",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
setVisible(false);
else
setVisible(true);
}
});
toolBar.add(button);
container.add(toolBar,BorderLayout.NORTH);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 4번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private Container container;
public SolveProblem(){
setTitle("메뉴로 배경 이미지 로딩하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = getContentPane();
createMenu();
setSize(400,200);
setVisible(true);
}
private void createMenu(){
JToolBar toolBar = new JToolBar("툴바");
JLabel label = new JLabel("학번: ");
JTextField textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char code = e.getKeyChar();
if(code>=48&&code<=57)
return;
else{
JOptionPane.showMessageDialog(null,e.getKeyChar()+"는 숫자가 아닙니다.\n숫자를 입력하세요.","경고",JOptionPane.WARNING_MESSAGE);
e.consume();
}
}
});
toolBar.add(label);
toolBar.add(textField);
container.add(toolBar,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 5번
package ex;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SolveProblem extends JFrame {
Clip clip = null;
File audioFile = null;
private JLabel play,stop;
public SolveProblem(){
setTitle("메뉴로 배경 이미지 로딩하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
play = new JLabel("audio\\hiphop.wav 연주 계속");
stop = new JLabel("audio\\hiphop.wav 연주 일시 중단");
c.add(play);
c.add(stop);
play.setVisible(true);
stop.setVisible(false);
try{
clip = AudioSystem.getClip();
audioFile = new File("audio/애국가1절.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
clip.open(audioInputStream);
clip.start();
}
catch (Exception e){
e.printStackTrace();;
}
c.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if(!clip.isActive())
clip.start();
play.setVisible(true);
stop.setVisible(false);
}
@Override
public void mouseExited(MouseEvent e) {
clip.stop();
stop.setVisible(true);
play.setVisible(false);
}
});
setSize(400,200);
setVisible(true);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 6번
package ex;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SolveProblem extends JFrame {
private JButton calculate;
private JLabel label;
private MyModalDialog dialog;
private int res;
public SolveProblem() {
calculate = new JButton("calcuate");
label = new JLabel("계산 결과 출력");
label.setBackground(Color.GREEN);
setTitle("다이얼로그 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(calculate);
c.add(label);
calculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog = new MyModalDialog();
dialog.setVisible(true);
}
});
setVisible(true);
setSize(400, 200);
}
class MyModalDialog extends JDialog {
private JTextField num1, num2;
private JButton Add;
public MyModalDialog() {
num1 = new JTextField(10);
num2 = new JTextField(10);
Add = new JButton("add");
setLayout(new FlowLayout());
add(new JLabel("두 수를 더합니다"));
add(num1);
add(num2);
add(Add);
setVisible(true);
setSize(400,200);
setTitle("Calculation Dialog");
Add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
String text1 = num1.getText();
String text2 = num2.getText();
int res = Integer.parseInt(text1)+Integer.parseInt(text2);
label.setText(Integer.toString(res));
}
});
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 7번
package ex;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SolveProblem extends JFrame {
private JLabel label=new JLabel();
private Clip clip;
private File AudioFile;
private String filePath;
public SolveProblem(){
setTitle("오디오 파일을 찾아 연주/종료 제어");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.setText("오디오 파일을 선택하세요");
setLayout(new FlowLayout());
getContentPane().add(label);
createMenu();
setVisible(true);
setSize(400,200);
}
public void createMenu(){
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("오디오");
JMenuItem play = new JMenuItem("연주");
JMenuItem stop = new JMenuItem("종료");
menu.add(play);
menu.add(stop);
menuBar.add(menu);
setJMenuBar(menuBar);
play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("wav파일","wav");
fileChooser.setFileFilter(filter);
int ret = fileChooser.showOpenDialog(null);
if(ret!=JFileChooser.APPROVE_OPTION){
JOptionPane.showMessageDialog(null,"파일을 선택하지 않았습니다","warnning",JOptionPane.WARNING_MESSAGE);
return;
}
filePath = fileChooser.getSelectedFile().getPath();
playMusic(filePath);
label.setText(filePath+"를 연주하고 있습니다.");
}
});
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clip.stop();
label.setText("오디오를 선택하세요.");
}
});
}
public void playMusic(String string){
try{
clip = AudioSystem.getClip();
AudioFile = new File(string);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(AudioFile);
clip.open(inputStream);
clip.start();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 14장 실습문제 - 8번
package ex;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.HashMap;
import java.util.Vector;
public class SolveProblem extends JFrame {
JCheckBox [] checkBoxes = new JCheckBox[4];
private JLabel label = new JLabel("체크된 곡만 순서대로 한 번 연주합니다.");
private JLabel[] labels = new JLabel[4];
private String[] s= {"audio\\wolf.wav","audio\\dhol_drums.wav","audio\\sirenpolice.wav","audio\\hiphop.wav"};
private JButton playMusic, stopMusic;
File [] files = new File[4];
Clip clip = null;
Vector<String>v;
public SolveProblem(){
setTitle("오디오 연주");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new GridLayout(6,1,5,5));
label.setBackground(Color.GRAY);
c.add(label);
for(int i=0;i<s.length;i++){
files[i]=new File(s[i]);
checkBoxes[i] = new JCheckBox(files[i].getName());
c.add(checkBoxes[i]);
checkBoxes[i].setHorizontalAlignment(SwingConstants.CENTER);
}
JPanel panel = new JPanel();
playMusic = new JButton("연주시작");
stopMusic = new JButton("연주 끝");
panel.add(playMusic);
panel.add(stopMusic);
c.add(panel);
playMusic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setAudio();
}
});
stopMusic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clip.close();
}
});
setVisible(true);
setSize(400,200);
}
public void setAudio(){
v = new Vector<>();
for(int i=0;i<checkBoxes.length;i++){
if(checkBoxes[i].isSelected())
v.add(files[i].getPath());
}
if(v.size()<=0)
return;
playFile(v.get(0));
v.remove(0);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if(event.getType()==LineEvent.Type.STOP && v.size()!=0){
playFile(v.get(0));
v.remove(0);
}
}
});
}
public void playFile(String path){
File file = new File(path);
try{
clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
clip.open(inputStream);
clip.start();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
반응형
'PL > JAVA' 카테고리의 다른 글
명품 자바 프로그래밍 12장 실습 문제 (0) | 2024.06.25 |
---|---|
명품 자바 프로그래밍 11장 실습 문제 (0) | 2024.06.22 |
명품 자바 프로그래밍 10장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 9장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 8장 실습 문제 (0) | 2024.06.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- 유클리드 호제법
- 카운팅 정렬
- c++ string
- 자바
- html
- C++ Stack
- 우선순위 큐
- HTML5
- BFS
- 세그먼트 트리
- 스프링 부트 crud 게시판 구현
- CSS
- 자료구조
- C++
- 이분 매칭
- Do it!
- 유니온 파인드
- 알고리즘
- 알고리즘 공부
- 에라토스테네스의 체
- 투 포인터
- 자바스크립트
- 백준
- 스택
- DP
- DFS
- 반복문
- 백준 풀이
- js
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함