본문 바로가기

카테고리 없음

JFC 구성 및 일반 클래스 활용2

툴바

import java.awt.*;
import javax.swing.*;

class Round22_Ex22_Sub extends JFrame {
    
    private Container con;
    private JToolBar jtb = new JToolBar(JToolBar.HORIZONTAL);
    private JButton bt = new JButton(new ImageIcon("bbb.gif"));
    private JButton bt1 = new JButton(new ImageIcon("ccc.gif"));
    
    public Round22_Ex22_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        con = this.getContentPane();
        con.setLayout(new BorderLayout());
        
        jtb.setRollover(true);
        jtb.add(bt);
        jtb.addSeparator(new Dimension(20, 30));
        jtb.add(bt1);
        con.add("North", jtb);
    }
    public void start() {
        
    }
}

public class Round22_Ex22 {
    public static void main(String[] args) { 
        new Round22_Ex22_Sub();
    }
}

 

MenuItem 단축키 KeyStore

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Round22_Ex23_Sub extends JFrame implements KeyListener, ActionListener {
    
    private Container con;
    private JTextArea ta = new JTextArea();
    private JScrollPane jsp = new JScrollPane(ta);
    private JMenuBar mb = new JMenuBar();
    private JMenu file = new JMenu("File");
    private JMenuItem fexit = new JMenuItem("Exit");
    
    public Round22_Ex23_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        this.setJMenuBar(mb);
        file.setMnemonic('F');
        mb.add(file);
        KeyStroke ks = KeyStroke.getKeyStroke('E', InputEvent.CTRL_MASK ^ InputEvent.ALT_MASK);
        
        fexit.setAccelerator( ks );
        file.add(fexit);
        
        con = this.getContentPane();
        con.setLayout(new BorderLayout());        
        con.add("Center", jsp);
    }
    public void start() {
        ta.addKeyListener(this);
        fexit.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e) {
        if( e.getSrouce() == fexit ) {
            System.exit(0);
        }
    }
    
    public void keyTyped( KeyEvent event ) {
        if( e.getSrouce() == ta ) {
            // System.out.println("key = " + e.getKeyChar());
            // System.out.println("mod = " + e.getModifiers());
            
            KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyChar(), e.getModifiers());
            KeyStroke ks1 = KeyStroke.getKeyStroke('r', InputEvent.ALT_MASK);
            //CTRL_MASK, SHIFT_MASK
            if( ks.equals(ks1) ) {
                System.out.println("alt + r을 눌렀습니다.");
            } else {
                System.out.println(ks);
            }
        }        
    }
    public void keyPressed( KeyEvent e ) { }
    public void keyReleased( KeyEvent e ) { }
}

public class Round22_Ex23 {
    public static void main(String[] args) { 
        new Round22_Ex23_Sub();
    }
}

 

계층 패널 LayeredPane

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

class Round22_Ex24_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private BorderLayout bl = new BorderLayout();
    private JLayeredPane jlp = new JLayeredPane();
    private JPanel jp = new JPanel(new FlowLayout());
    private JPanel jp1 = new JPanel(new FlowLayout());
    private JPanel jp2 = new JPanel(new FlowLayout());
    private ImageIcon ii = new ImageIcon("aaa.gif");
    private JLabel lb = new JLabel(ii);
    private JButton bt = new JButton("1");
    private JButton bt2 = new JButton("2");
    private JButton bt3 = new JButton("3");

    public Round22_Ex24_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(500, 500);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(bl);
        jp.add(bt);
        jp1.add(bt1);
        jp2.add(bt2);
        jp.setBackground(Color.red);
        jp1.setBackground(Color.green);
        jp2.setBackground(Color.blue);
        jp.setBounds(50, 50, 100, 100);
        jp1.setBounds(75, 75, 100, 100);
        jp2.setBounds(100, 100, 100, 100);
        
        lb.setBounds(60, 60, 50, 50);
        jlp.add(jp, new Integer(0));
        jlp.add(jp1, new Integer(1));
        jlp.add(jp2, new Integer(2));
        jlp.moveToFront(lb);
        con.add("Center", jlp);
    }
    public void start() {
    
    }    
}
public class Round22_Ex24 {
    public static void main(String[] args) { 
        new Round22_Ex24_Sub();
    }
}

 

계층 패널 LayeredPane 2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

class Round22_Ex25_Sub extends JFrame implements MouseListener, ActionListener {
    private JRootPane jrp;
    private Container con;
    private BorderLayout bl = new BorderLayout();
    private JButton bt = new JButton("A");
    private JButton bt1 = new JButton("B");
    private JButton bt2 = new JButton("C");
    private JPanel jp = new JPanel(null);
    private ImageIcon ii = new ImageIcon("aaa.gif");
    private JLabel lb = new JLabel(ii);
    private JButton bt3 = new JButton("닫기");
    
    public Round22_Ex25_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(bl);
        con.add("North", bt);
        con.add("Center", bt1);
        con.add("South", bt2);
        
        lb.setBounds(0, 0, 100, 200);
        jp.add(lb);
        bt3.setBounds(200, 100, 80, 60);
        jp.add(bt3);
        jp.setOpaque(false); // 투명하게
        jrp.setGlassPane(jp);
        jp.setVisible(true);
    }
    public void start() {
        bt.addMouseListener(this);
        bt1.addMouseListener(this);
        bt2.addMouseListener(this);
        bt3.addActionListener(this);
    }
    
    public void mouseClicked(MouseEvent e) { }
    public void mousePressed(MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { 
        if( e.getSource() == bt || e.getSource() == bt1 || e.getSource() == bt2 )
        {
            if( e.getSource() == bt ) {
                lb.setBounds(e.getX(), e.getY(), 50, 50);
            } else if( e.getSource() == bt1 ) {
                lb.setBounds(e.getX(), e.getY() + bt.getHeight(), 50, 50);
            } else if( e.getSource() == bt2 ) {
                lb.setBounds(e.getX(), e.getY() + bt.getHeight() + bt1.getHeight(), 50, 50);
            }
            
        }
    }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    
    public void actionPerformed(ActionEvent e) { 
        if( e.getSource() == bt3 ) {
            jp.setVisible(false);
        }
    }
}
public class Round22_Ex25 {
    public static void main(String[] args) { 
        new Round22_Ex25_Sub();
    }
}

 

JSpinner

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.text.*;

class Round22_Ex26_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private JSpinner js = new JSpinner();
    //private String str[] = {"AAA", "BBB", "CCC"};
    private DateFormatSymbols dfs = new DateFormatSymbols();
    private String[] str = dfs.getMonths();
    private SpinnerListModel slm = new SpinnerListModel(str);
    
    public Round22_Ex26_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout());
        js.setModel(slm);
        con.add("North", js);
    }
    public void start() {

    }
}
public class Round22_Ex26 {
    public static void main(String[] args) { 
        new Round22_Ex26_Sub();
    }
}

 

JSpinner2 모델 변경

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.text.*;

class Round22_Ex27_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private JSpinner js = new JSpinner();
    private SpinnerDateModel sdm = new SpinnerDateModel();
    private JTextField tf = new JTextField();

    private JSpinner js1 = new JSpinner();
    private SpinnerNumberModel snm = new SpinnerNumberModel(50, 0, 100, 2);
    
    public Round22_Ex27_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout());
        sdm.setCalendarField(Calendar.MONTH);
        js.setModel(sdm);
        
        tf = ((JSpinner.DateEditor)js.getEditor()).getTextField();
        tf.setEnabled(false);
        con.add("North", js);
        js1.setModel(snm);
        con.add("South", js1);
    }
    public void start() {

    }
}
public class Round22_Ex27 {
    public static void main(String[] args) { 
        new Round22_Ex27_Sub();
    }
}

 

SpiltPane

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.text.*;

class Round22_Ex28_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private JSplitPane jsp = new JSpliPane(
        JSpliPane.HORIZONTAL_SPLIT
        , true
        , new JButton("좌측버튼")
        , new JButton("우측버튼")
    );
    
    public Round22_Ex28_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout());
        
        jsp.setRightComponent(new JButton("우측1버튼"));
        jsp.setContinousLayout(true);
        //jsp.setDividerLocation(50);
        //jsp.setDividerSize(5);
        jsp.setOneTouchExpandable(true);
        jsp.setResizeWeight(0.3);
        
        con.add("Center", jsp);
        con.add("North", new JLabel("Test"));
        con.add("South", new JButton("Yes"));
    }
    public void start() {

    }
}
public class Round22_Ex28 {
    public static void main(String[] args) { 
        new Round22_Ex28_Sub();
    }
}

 

HTML을 표시하는 EditorPane

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import java.io.*;

class Round22_Ex29_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private JEditorPane jep;
    private JScrollPane jsp;
    private JToolTip jtt = new JToolTip();

    public Round22_Ex29_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout(5, 5));

        con.add("North", new JLabel("Editor Pane!!!", JLabel.CENTER));
        con.add("South", new JButton("확인"));
        /*
        try {
            // jep = new EditorPane("http://www.freechal.com");
            jep = new EditorPane(
                "text/html"
                ,"<html><body>" +
                "<h1>Hello</h1><p><h2>hello</h2><a hef=\"#\">소스보기</a>" +
                "</body></html>"
            );
        } catch(IOException e ) { }
        */
        jep.setToolTipText("Test");
        jtt.setTipText(jep.getToolTipText());
        jsp = new JScrollPane(jep);
        con.add("Center", jsp);

    }
    public void start() {

    }
}
public class Round22_Ex29 {
    public static void main(String[] args) { 
        new Round22_Ex29_Sub();
    }
}

 

ViewPort 예제

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

class Round22_Ex30_Sub extends JFrame {
    private JRootPane jrp;
    private Container con;
    private JScrollPane jsp = new JScrollPane();
    private JLabel jl = new JLabel("Colum Header !" + JLable.CENTER);
    private JViewPort jv = new JViewPort();
    
    private JLabel jl1 = new JLabel(" 1 " + JLable.CENTER);
    private JViewPort jv1 = new JViewPort();
    
    private JLabel jl2 = new JLabel(new ImageIcon("image/kathyCosmo.gif"));
    private JViewPort jv2 = new JViewPort();
    
    private JScrollBar jsb = new JScrollBar(JScrollBar.VERTICAL);
    private JScrollBar jsb1 = new JScrollBar(JScrollBar.HORIZONTAL);

    public Round22_Ex30_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout());
        con.add("North", new JLabel("JViewport !!!",JLabel.CENTER));
        con.add("South", new JButton("확인"));
        jv.setView(j1);
        jv1.setBackground(Color.red);
        jv1.setView(jl1);
        jv2.setView(jl2);
        jv2.setScrollMode(JViewport.BLIT_SCROLL_MODE);
        jsp.setColumnHeaderView(jv);    // North
        jsp.setRowHeaderView(jv1);      // West
        jsp.setViewportView(jv2);       // Center
        jsp.setVerticalScrollBar(jsb);  // East
        jsp.setHorizontalScrollBar(jsb1); // South
        jsp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new JButton("1"));
        con.add("Center", jsp);
    }
    public void start() {

    }
}
public class Round22_Ex30 {
    public static void main(String[] args) { 
        new Round22_Ex30_Sub();
    }
}

과제

과제1 벡터(Vector)를 저장공간으로 하는 개인 인명 관리록을 만들어보자.

 

과제2. 로컬 어플리케이션 게시판을 만들어보자.