JAVA/Java.awt GUI

GUI - JMenuBar, ImageIcon

  • 메뉴 바
    • 이벤트처리는 JMenuItem 에만 가능하다
public class GuiEx13menuBar extends JFrame{
	GuiEx13menuBar() {
		Container c = getContentPane();
		
		//1. bar 생성
		JMenuBar jb = new JMenuBar();
		
		//2. 메뉴 생성
		JMenu j1 = new JMenu("File");
		JMenu j2 = new JMenu("Edit");
		JMenu j3 = new JMenu("Source");

		//3. 서브메뉴 생성
		JMenuItem j4 = new JMenuItem("new");
		JMenuItem j5 = new JMenuItem("Undo Typing");
		JMenuItem j6 = new JMenuItem("Redo");
		
		//4. setting menuBar
		setJMenuBar(jb);
		
		//5. add
		jb.add(j1);
		jb.add(j2);
		jb.add(j3);
		
		j1.add(j4);
		j2.add(j5);
		j2.add(j6);
		
		//6. 이벤트 처리
		j4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// j4 메뉴 클릭 시 이벤트
				System.out.println("create new File");
			}
		});
		
		setSize(600, 350);
		setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GuiEx13menuBar();
	}
}

 

 

  • 이미지삽입
    • 이미지 단독으로 삽입 안되고, JLabel에 부착해야한다.
public class GuiEx14  extends JFrame {
	GuiEx14(){
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		JLabel j = new JLabel("Hello");
		
		//이미지 삽입 - Lable 에 부착해야한다.
		ImageIcon i = new ImageIcon( "E://26.newJava/1920_1080_up.jpg" );
		JLabel j1 = new JLabel(i);
		j1.setSize(500, 400);
		
		JLabel j2 = new JLabel("화요일입니다.", SwingConstants.CENTER);
		
		
		c.add(j);
		c.add(j1);
		c.add(j2);
		
		setSize(600, 550);
		setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GuiEx14();
	}
}

 

 

  • 라디오버튼
public class GuiEx15jRadioButton extends JFrame {
	GuiEx15jRadioButton(){
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		//하나만 선택하기 위해 버튼 그룹이 필요
		ButtonGroup g = new ButtonGroup();
		
		JRadioButton j = new JRadioButton("자바");
		JRadioButton j1 = new JRadioButton("DB");
		JRadioButton j2 = new JRadioButton("C+");
		
		//버튼 그룹에 삽입
		g.add(j);
		g.add(j1);
		g.add(j2);
		
		c.add(j);
		c.add(j1);
		c.add(j2);
		
		setSize(600, 550);
		setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GuiEx15jRadioButton();
	}
}

 

 

  • 이미지 3개 삽입(배열)
public class Review0608A extends JFrame{
	
	Review0608A(){

		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		JLabel j = new JLabel("이미지 3개");
		
		ImageIcon[] im = {
				new ImageIcon("E://26.newJava/pizza.JPG"),
				new ImageIcon("E://26.newJava/burger.JPG"),	
				new ImageIcon("E://26.newJava/pizza.JPG")
		};

		JLabel[] j1 = new JLabel[3];
		for(int i=0; i<im.length; i++) {
			j1[i] = new JLabel(im[i]);
			c.add(j1[i]);	//컨네이너에 부착
		}
		
		
		//setting
		c.add(j);
		
		setSize(600, 550);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public static void main(String[] args) {
		new Review0608A();
	}
}
728x90
728x90

'JAVA > Java.awt GUI' 카테고리의 다른 글

Panel, Slider  (0) 2022.06.08
이미지와 콤보박스  (0) 2022.06.08
GUI 버튼 클릭이벤트 처리방법  (0) 2022.06.07
GUI 문자열과 체크박스  (0) 2022.06.07
Java GUI  (0) 2022.06.07