모델
CountButtonModel.java
import java.awt.*;
import javax.swing.*;
public class CountButtonModel extends DefaultButtonModel {
//ButtonModel 인터페이스를 구현한 DefaultButtonModel 클래스
private int count;
private JButton btn;
public CountButtonModel(JButton btn) {
//오케이를 버튼으로 받음
this.btn = btn;
//모델객체에 있는 버튼에다가 새로들어온 버튼을 할당해줌
btn.setModel(this);
//디슷를 통해서 버튼이 필요한 데이터를 설정해줌.
//버튼이 필요한 데이터 로직은 같은 소스에 들어있음. 셋 모델에서 다른 객체를 준게 아니라 자기자신을줌
//자기 자신을 준 의미는 여기라는 의미.
}
public void setPressed(boolean b) {
if(b) {
count = ++count % 4;
switch(count) {
case 0:
btn.setBackground(Color.lightGray);
break;
case 1:
btn.setBackground(Color.green);
break;
case 2:
btn.setBackground(Color.red);
break;
case 3:
btn.setBackground(Color.yellow);
}
}
}
}
// 메인에 셋프레스드를 포함하지 않고 따로 관리하는 이유는 유지보수를 쉽게 하기 위해서
뷰
ButtonModelTest
import javax.swing.*;
public class ButtonModelTest extends JFrame {
JButton ok;
public ButtonModelTest() {
super("ButtonModel 테스트");
ImageIcon rai = new ImageIcon("rai.gif");
ok = new JButton("OK", rai);
ok.setRolloverIcon(rai);
ok.setPressedIcon(rai);
CountButtonModel model = new CountButtonModel(ok);
getContentPane().add("South", ok);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
}
public static void main(String args[]) {
new ButtonModelTest();
}
}
실행결과
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListComboBox extends JFrame implements ItemListener {
protected JList fruits;
protected JComboBox colors;
protected String items[] = {"apple", "orange", "banana", "pear"};
public JListComboBox() {
super("스윙 리스트와 콤보박스");
getContentPane().setLayout(new FlowLayout());
fruits = new JList(items);
fruits.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = fruits.locationToIndex(e.getPoint());
if (e.getClickCount() == 1) {
System.out.println(items[index]);
} else if(e.getClickCount() == 2) {
System.out.println("[" + items[index] + "]");
}
}
});
fruits.setVisibleRowCount(3);
JScrollPane sp = new JScrollPane(fruits);
colors = new JComboBox();
colors.addItem("white");
colors.addItem("blue");
colors.addItem("green");
colors.addItemListener(this);
getContentPane().add(sp);
getContentPane().add(colors);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
Object o = e.getSource();
if(o == colors) {
if(e.getStateChange() == ItemEvent.SELECTED) {
Object data = colors.getSelectedItem();
System.out.println(data.toString());
}
}
}
public static void main(String args[]) {
JListComboBox jlc = new JListComboBox();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListComboBox extends JFrame implements ItemListener {
protected JList fruits;
protected JButton btn;
protected JComboBox colors;
protected String items[] = {"apple", "orange", "banana", "pear"};
public JListComboBox() {
super("스윙 리스트와 콤보박스");
getContentPane().setLayout(new FlowLayout());
fruits = new JList(items);
fruits.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = fruits.locationToIndex(e.getPoint());
if (e.getClickCount() == 1) {
System.out.println(items[index]);
} else if(e.getClickCount() == 2) {
System.out.println("[" + items[index] + "]");
}
}
});
fruits.setVisibleRowCount(3);
JScrollPane sp = new JScrollPane(fruits);
colors = new JComboBox();
colors.addItem("white");
colors.addItem("blue");
colors.addItem("green");
colors.addItemListener(this);
getContentPane().add(sp);
getContentPane().add(colors);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
Object o = e.getSource();
if(o == colors) {
if(e.getStateChange() == ItemEvent.SELECTED) {
Object data = colors.getSelectedItem();
System.out.println(data.toString());
}
}
}
public static void main(String args[]) {
JListComboBox jlc = new JListComboBox();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListComboBox_1 extends JFrame implements ItemListener {
protected JList fruits;
protected JButton btn;
protected JComboBox colors;
protected String items[] = {"apple", "orange", "banana", "pear"};
public JListComboBox_1() {
super("스윙 리스트와 콤보박스");
getContentPane().setLayout(new FlowLayout());
fruits = new JList(items);
btn = new JButton("b");
fruits.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = fruits.locationToIndex(e.getPoint());
if (e.getClickCount() == 1) {
System.out.println(items[index]);
} else if(e.getClickCount() == 2) {
System.out.println("[" + items[index] + "]");
}
}
});
fruits.setVisibleRowCount(3);
JScrollPane sp = new JScrollPane(fruits);
colors = new JComboBox();
colors.addItem("white");
colors.addItem("blue");
colors.addItem("green");
colors.addItemListener(this);
getContentPane().add(sp);
getContentPane().add(colors);
getContentPane().add(btn);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
btn.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
System.out.println("Enter?");
}
public void mouseExited(MouseEvent e) {
System.out.println("Exit?");
}});
//JScrollPane sp = new JScrollPane(sp);
}
public void itemStateChanged(ItemEvent e) {
Object o = e.getSource();
if(o == colors) {
if(e.getStateChange() == ItemEvent.SELECTED) {
Object data = colors.getSelectedItem();
System.out.println(data.toString());
}
}
}
public static void main(String args[]) {
JListComboBox_1 jlc = new JListComboBox_1();
}
}
'Fundamental Notes > JAVA' 카테고리의 다른 글
DecimalFormat (0) | 2013.03.10 |
---|---|
Java에서 double형 연산 오류 (0) | 2012.09.27 |
java 실습 (0) | 2009.01.23 |
자바 초보자가 자주 범하는 실수들 (0) | 2009.01.23 |
Java Applet? (0) | 2009.01.23 |