총 20개의 과일 사진에서 하나를 누르면 해당하는 과일의 큰 사진이 나오게 하라.
- 메인 창 / 보조창 나누기
- 20개의 작은 과일들이 4열 5행으로 나눠져있음.
- 마우스를 갖다대면 클릭 손모양 / 초록색 테두리로 바뀜
- 설명말풍선 나타내기
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class FruitView extends JFrame {
private String[] fruits;
private JLabel[] labels;
private JPanel pnl1;
public FruitView() {
init();
setDisplay();
addListeners();
showFrime();
}
public String[] getFruits() {
return fruits;
}
private void init() {
fruits = new String[] { "apple", "asparagus", "banana", "broccoli", "cantaloupe", "carrot", "corn", "grapefruit",
"grapes", "kiwi", "onion", "peach", "pear", "pepper", "pickle", "pineapple", "raspberry", "strawberry",
"tomato", "watermelon" };
}
private void setDisplay() {
pnl1 = new JPanel(new GridLayout(5, 4));
labels = new JLabel[fruits.length];
for (int idx = 0; idx < fruits.length; idx++) {
ImageIcon icon = new ImageIcon(fruits[idx] + ".jpg");
labels[idx] = new JLabel(icon);
labels[idx].setToolTipText("Click the " + fruits[idx]);
pnl1.add(labels[idx]);
}
add(pnl1, BorderLayout.CENTER);
}
private void addListeners() {
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
for(int idx = 0; idx < labels.length; idx++) {
if((JLabel) e.getSource() == labels[idx]) {
setVisible(false);
new Fruit(FruitView.this, idx);
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
JLabel select = (JLabel)e.getSource();
for(JLabel lable : labels) {
if(select == lable) {
lable.setBorder(new LineBorder(Color.GREEN, 2));
}
}
pnl1.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(MouseEvent e) {
for(JLabel lable : labels) {
lable.setBorder(null);
}
}
};
for(JLabel lable : labels) {
lable.addMouseListener(mouseListener);
}
}
public ImageIcon getImage(int idx) {
Image img = ((ImageIcon)labels[idx].getIcon()).getImage();
Image newImage = img.getScaledInstance(500, 500, Image.SCALE_SMOOTH);
return new ImageIcon(newImage);
}
private void showFrime() {
setTitle("Viewer");
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new FruitView();
}
}
- 메인 창에서 선택한 과일의 큰 사진 나타내기.
- 오른쪽 클릭 시 다음 과일 사진, 왼쪽을 클릭 시 그 전의 과일 사진 나타내기.
- 마우스 갖다대면 초록 테두리 표시.
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
public class Fruit extends JDialog {
int currentFruit;
FruitView owner;
int maxFruitNum;
JLabel lblTitle;
JLabel lblImage;
JPopupMenu popMenu;
JMenuItem menuBack;
JMenuItem menuFront;
public Fruit(FruitView owner, int idx) {
super(owner, "Do u see ?", true);
this.owner = owner;
currentFruit = idx;
init();
setDisplay();
addListeners();
showFrame();
}
private void init() {
maxFruitNum = owner.getFruits().length;
lblTitle = new JLabel((currentFruit + 1) + " / " + maxFruitNum, JLabel.CENTER);
lblImage = new JLabel(owner.getImage(currentFruit));
popMenu = new JPopupMenu();
menuBack = new JMenuItem("뒤로");
menuFront = new JMenuItem("앞으로");
popMenu.add(menuBack);
popMenu.add(menuFront);
}
private void setDisplay() {
add(lblImage, BorderLayout.CENTER);
add(lblTitle, BorderLayout.NORTH);
}
private void addListeners () {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
dispose();
owner.setVisible(true);
}
});
lblImage.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popMenu.show(lblImage, e.getX(), e.getY());
} else {
int half = lblImage.getWidth() / 2;
if (e.getX() < half) {
back();
} else {
front();
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource() == lblImage) {
lblImage.setBorder(new LineBorder(Color.GREEN, 2));
}
}
@Override
public void mouseExited(MouseEvent e) {
lblImage.setBorder(null);
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == menuFront) {
front();
} else {
back();
}
}
};
menuFront.addActionListener(actionListener);
menuBack.addActionListener(actionListener);
}
private void back() {
currentFruit --;
if(currentFruit < 0) {
currentFruit += maxFruitNum;
}
setting();
}
private void front() {
currentFruit++;
if (currentFruit == maxFruitNum) {
currentFruit -= maxFruitNum;
}
setting();
}
private void setting() {
lblTitle.setText((currentFruit + 1) + " / " + maxFruitNum);
lblImage.setIcon(owner.getImage(currentFruit));
}
private void showFrame () {
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
}