본문 바로가기
IT/JAVA

[Java] Swing - CardLayout (창 넘어가기)

by 행복한 용용이 2020. 10. 14.
반응형

버튼 누르면 창 넘어가기

View1과 View2라는 이름으로 JPanel 2개 생성 후 꾸며준다.

MainFrame에서 화면1버튼 누르면 View1로 바뀌고 화면2버튼 누르면 View2로 바뀌게 한다.


package com.kitri.test;

 

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

 

 

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

 

import javax.swing.JButton;

 

 

public class MainFrame extends JFrame implements ActionListener{

 

 

     private JPanel contentPane;

     private JButton b1;

     private JButton b2;

 

     JPanel panel;

 

     View1 v1 = new View1();

     View2 v2 = new View2();

 

     CardLayout card = new CardLayout();

 

 

     /**

       * Launch the application.

       */

     public static void main(String[] args) {

         EventQueue.invokeLater(new Runnable() {

         public void run() {

             try {

                 MainFrame frame = new MainFrame();

                 frame.setVisible(true);

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

     });

}

 

 

/**

  * Create the frame.

  */

public MainFrame() {

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     setBounds(100, 100, 488, 495);

     contentPane = new JPanel();

     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

     setContentPane(contentPane);

     contentPane.setLayout(null);

 

     panel = new JPanel();

 

     panel.setLayout(card);

     panel.add("view1", v1)//화면에 이름붙임.

     panel.add("view2",v2);

     card.show(panel, "view2")//실행하자 마자 보이는

 

     panel panel.setBounds(12, 55, 448, 392);

     contentPane.add(panel);

 

     b1 = new JButton("\uD654\uBA741");

     b1.setBounds(12, 10, 97, 35);

     contentPane.add(b1);

 

     b2 = new JButton("\uD654\uBA742");

     b2.setBounds(121, 10, 97, 35);

     contentPane.add(b2);

 

     b1.addActionListener(this);

     b2.addActionListener(this);

}

 

 

@Override

public void actionPerformed(ActionEvent e) {

     Object ob = e.getSource();

     if(ob == b1) {

         card.show(panel, "view1");

     }else if(ob == b2) {

         card.show(panel, "view2");

     }

 

  }

 

 

}

 

 

 

package com.kitri.test;

 

 

import javax.swing.JPanel;

import javax.swing.JTextArea;

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JButton;

import javax.swing.ImageIcon;

 

public class View1 extends JPanel {

 

/**

  * Create the panel.

  */

public View1() {

setLayout(new BorderLayout(0, 0));

 

JButton btnNewButton = new JButton("New button");

btnNewButton.setIcon(new ImageIcon("C:\\Users\\Administrator\\Desktop\\0001336415_001_20190325135203638.jpg"));

add(btnNewButton, BorderLayout.CENTER);

 

JTextArea textArea = new JTextArea();

textArea.setText("\uACBD\uBCF5\uAD6D \uBD04");

textArea.setBackground(Color.PINK);

add(textArea, BorderLayout.SOUTH);

 

}

 

}

 

[출처] 빡쏘끼룩

반응형