[JAVA] swing - 3

JPopupMenu

  • PopupMenu는 컴포넌트 내의 특정 위치에 동적으로 팝업될 수 있다
  • JComponent 클래스를 상속합니다.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PopupMenuExample {
    PopupMenuExample(){
        final JFrame f = new JFrame("PopupMenu Example");

        final JLabel label = new JLabel();
        label.setHorizontalAlignment(JLabel.CENTER);
        System.out.println(JLabel.CENTER);
        label.setSize(400,100);

        final JPopupMenu popupMenu = new JPopupMenu("Edit");

        JMenuItem cut = new JMenuItem("cut");
        JMenuItem copy = new JMenuItem("copy");
        JMenuItem paste = new JMenuItem("paste");

        popupMenu.add(cut); popupMenu.add(copy); popupMenu.add(paste);

        f.add(popupMenu); f.add(label);
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                popupMenu.show(f, e.getX(), e.getY());
                System.out.println("X: "+ e.getX()+ " Y: " + e.getY());
            }
        });
        cut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("cut MemuItem clicked. "+ e.getActionCommand());
            }
        });
        copy.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                label.setText("copy MenuItem clicked. "+ e.getActionCommand());
            }
        });
        paste.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                label.setText("paste MenuItem clicked. "+ e.getActionCommand());
            }
        });

        f.setSize(555,555);
        f.setLayout(null);
        f.setVisible(true);
    }
    public static  void main(String[] args){
        new PopupMenuExample();
    }
}

JCheckBoxMenuItem

  • JCheckBoxMenuItem class represents checkbox which can be included on a menu . A CheckBoxMenuItem can have text or a graphic icon or both, associated with it. MenuItem can be selected or deselected. MenuItems can be configured and controlled by actions.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class JavaCheckBoxMenuItem {
    public static void main(final String args[]) {
        JFrame frame = new JFrame("Jmenu Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        // File Menu, F - Mnemonic
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);
        // File->New, N - Mnemonic
        JMenuItem menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);
        fileMenu.add(menuItem1);

        JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Option_1");
        caseMenuItem.setMnemonic(KeyEvent.VK_C);
        fileMenu.add(caseMenuItem);

        ActionListener aListener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                AbstractButton aButton = (AbstractButton) event.getSource();
                boolean selected = aButton.getModel().isSelected();
                String newLabel;
                Icon newIcon;
                if (selected) {
                    newLabel = "Value-1";
                } else {
                    newLabel = "Value-2";
                }
                aButton.setText(newLabel);
            }
        };

        caseMenuItem.addActionListener(aListener);
        frame.setJMenuBar(menuBar);
        frame.setSize(350, 250);
        frame.setVisible(true);
    }
}

JSeparator

  • 구분선을 구현하기 위한 범용 구성 컴포넌트를 제공할때 사용한다,
import javax.swing.*;

public class SeparatorExample {
    JMenu menu, submenu;
    JMenuItem i1, i2, i3, i4, i5;
    SeparatorExample(){
        JFrame f = new JFrame("Seperator Example");
        JMenuBar mb = new JMenuBar();
        menu = new JMenu("Menu");
        i1 = new JMenuItem("item 1");
        i2 = new JMenuItem("item 2");

        menu.add(i1);menu.addSeparator(); menu.add(i2);
        mb.add(menu);

        f.setJMenuBar(mb);
        f.setSize(400,400);
        f.setVisible(true);
    }
    public  static  void main(String[] args){
        new SeparatorExample();
    }
}

JProgressBar

  • 업무의 진행을 표시하는데 사용된다.

import javax.swing.*;

public class ProgressBarExample extends JFrame {
    JProgressBar jb;
    int i = 0, num = 0;

    ProgressBarExample() {
        jb = new JProgressBar(0, 2000);
        jb.setBounds(40, 40, 160, 30);
        jb.setValue(0);
        jb.setStringPainted(true);
        add(jb);
        setSize(250, 150);
        setLayout(null);
    }

    public void iterate() {
        while (i <= 2000) {
            jb.setValue(i);
            i = i + 20;
            try {
                Thread.sleep(150);
            } catch (Exception e) {
            }
        }
    }

    public static void main(String[] args) {
        ProgressBarExample m = new ProgressBarExample();
        m.setVisible(true);
        m.iterate();
    }
}

JTree

  • 계층구조 데이터 또는 트리구조 자료를 표시하는데 사용되는 객체이다.
  • JTree는 복잡한 컴포넌트이다,

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample {
    JFrame f;
    TreeExample(){
        f = new JFrame();
        DefaultMutableTreeNode style = new DefaultMutableTreeNode("Sytle");
        DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");
        DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
        style.add(color);
        style.add(font);

        DefaultMutableTreeNode red = new DefaultMutableTreeNode("red");
        DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
        DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
        DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
        color.add(red); color.add(blue); color.add(black); color.add(green);

        JTree jTree = new JTree(style);
        f.add(jTree);
        f.setSize(200,200);
        f.setVisible(true);
    }

    public static  void main(String[] args){
        new TreeExample();
    }
}

좋은 웹페이지 즐겨찾기