ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210826_JLabel+JTable+JTextArea
    Swing 2021. 8. 26. 17:28

    public class QuizJTable_02 extends JFrame {

     

    Container cp;

    JLabel lblTitle,lblOut;

    JTable table;

    JTextArea area;

     

     

    public QuizJTable_02(String title) {

    super(title);

     

    cp=this.getContentPane();

    cp.setBackground(new Color(171,190,190));

    this.setBounds(700,100,800,800); 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.initDesign();

    this.setVisible(true);

    }

     

     

    public void initDesign() {

     

    //이효리를 찍으면 그 열의 내용이 결과창이 나오게 하기

     

    String[] title= {"이름","주소","핸드폰"};

    String [][] data= {

    {"요다","서울시 광진구","010-1111-1111"}, {"림짱","서울시 강남구","010-2222-2222"},

    {"루크","서울시 종로구","010-3333-3333"}

    };

     

    lblTitle=new JLabel("JTable 연습",JLabel.CENTER);

    lblOut=new JLabel("Result",JLabel.CENTER);

     

    table=new JTable(data,title);

    JScrollPane pane=new JScrollPane(table);

     

    //테이블에 마우스 이벤트 추가

    table.addMouseListener(new TableEvent());

     

    this.add("North",lblTitle);

    this.add("Center",pane);

    this.add("South",lblOut);

    }

     

     

    //테이블 클릭시 발생하는 마우스이벤트

    class TableEvent extends MouseAdapter{

     

    public void mouseClicked(MouseEvent e) {

     

    //선택한 행번호 얻기

    int rowNum=table.getSelectedRow();

     

    String str="이름: "+table.getValueAt(rowNum, 0)+"주소: "+table.getValueAt(rowNum, 1)+"연락처: "+table.getValueAt(rowNum, 2);

    lblOut.setText(str);

    }

    }

     

     

    public static void main(String[] args) {

     

    new QuizJTable_02("Quiz JTable");

    }

    }

    선택하면 아래에 이름,주소,핸드폰 출력

    댓글

Designed by Tistory.