class Account {int balance = 1000; public void withdraw(int money) {if (balance >= money) {try {Thread.sleep(1000);} catch (Exception e) {}balance -= money;}}} class Bank extends Thread {static Account ac = new Account(); public Bank() {}; public Bank(String name) {super(name);}; public void run() {while (true) {//동기화를 하면 먼저 임계구역에 접근한 스레드가 구역을 점령한다.synchronized (ac) {int money = (int) (Math.rand..
public class DataModelTest { public static void main(String[] args) {// TODO Auto-generated method stub String rows[][] = { { "수곡동", "야구" }, { "모충동", "족구" }, { "개신동", "축구" } };String headers[] = { "이름", "종목" };String sports[] = { "야구", "축구", "족구", "기타" }; JComboBox comboBox = new JComboBox(sports);comboBox.setMaximumRowCount(4); TableCellEditor editor = new DefaultCellEditor(comboBox);JFrame fra..
public class JTableTest extends JFrame { JTableTest() {super("JTable Test");String title[] = { "번호", "이름", "나이" };String data[][] = { { "1", "김 구", "40" }, { "2", "강감찬", "42" },{ "3", "이순신", "41" } };JTable table = new JTable(data,title);//테이블객체 생성, 데이터와 타이틀을 인자로 가짐JScrollPane sp = new JScrollPane(table); //스크롤페인 붙임getContentPane().add(sp, BorderLayout.CENTER);//setSize(300,150);setVisible(true)..
public class BoxLayoutTest extends JFrame { public BoxLayoutTest() {super("Box Layout Test example");JRadioButton r1, r2; setLayout(new BorderLayout());Box left = Box.createVerticalBox();left.add(Box.createVerticalStrut(30)); ButtonGroup radioGroup = new ButtonGroup();radioGroup.add(r1 = new JRadioButton("야구"));left.add(r1); left.add(Box.createVerticalStrut(30));radioGroup.add(r2 = new JRadioBut..
public class ComboTest extends JFrame {JList jl;JComboBox com;String name[] = { "수곡동", "도곡동", "용암동", "분평동", "개신동" };String count[] = { "1", "2", "3", "4", "5" }; public ComboTest() {super("JComponent practice");Container c = getContentPane(); //Returns the contentPane object for this frame.c.setLayout(new FlowLayout()); //컨테이너의 레이아웃 설정 com = new JComboBox(name); //콤보박스에 name 붙임JScrollPane s = ne..
public class JTextTest extends JFrame {JLabel lb, lb1, lb2;JTextField id;JPasswordField passwd;JPanel idPanel, paPanel, loginPanel;JButton b1, b2;JTextArea content; public JTextTest() {super("JText test");setLayout(new FlowLayout());EtchedBorder eborder = new EtchedBorder(); lb = new JLabel("아이디와 패스워드를 입력하세요");lb.setBorder(eborder);add(lb); // 메시지를 프레임에 붙임 idPanel = new JPanel();lb1 = new JLabel..
Swing은 AWT 보다 약간 더 세련 되어졌다.윈도 창의 종료 버튼의 이벤트를 추가하였다.AWT와 마찬가지로 패널을 이용한다. public class JButtonTest extends JFrame {public JButtonTest() {super("Test");JPanel panel = new JPanel();JButton btn = new JButton("Button");panel.add(btn); ImageIcon icon1 = new ImageIcon(".\\src\\1.png"); //기본 이미지ImageIcon icon2 = new ImageIcon(".\\src\\2.png"); //눌렸을 때 이미지JToggleButton tg = new JToggleButton(icon1); //토글버..
안드로이드의 프레임 레이아웃과 유사 (화면 겹침) class CardLayoutTest extends Frame {CardLayout cl = new CardLayout();Panel p[] = new Panel[5];Color color[] = { Color.red, Color.blue, Color.yellow, Color.green,Color.cyan }; public CardLayoutTest(String str) {super(str); setLayout(cl);MouseHandle mh = new MouseHandle();//마우스핸들 객체 생성 for (int i = 0; i < 5; i++) {p[i] = new Panel();p[i].setBackground(color[i]);p[i].add..