¾Æ·¡°¡ ÄÚµåÀε¥
ÀÌ·¸°Ô Çϸé ÁÖ¼Ò·Ï ¸¸µç´À âÀÌ ¶å´Ï´Ù.
Àú°¡ ¿øÇÏ´Â °ÍÀº "»ó¼¼" ¹öÆ°À» ´©¸£¸é
»çÁø °ú À̸§ÀÌ ³ª¿À´Â »õ·Î¿î âÀÌ ³ª¿À´Â°ÍÀε¥
¾î¶»°Ô ÇؾßÇÒÁö À߸𸣰ڽÀ´Ï´Ù.
¾î¶»°ÔÇؾßÇϳª¿ä
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class WindowExample8 {
public static void main(String[] args) {
JFrame frame = new JFrame("ÁÖ¼Ò·Ï");
frame.setLocation(500,400);
frame.setPreferredSize(new Dimension(800,500));
Container contenPane = frame.getContentPane();
String colNames[] = {"À̸§", "³ªÀÌ", "¼ºº°", "ÇÚµåÆù", "À̸ÞÀÏ",};//1
DefaultTableModel model = new DefaultTableModel(colNames,0);
JTable table = new JTable(model);
contenPane.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel panel = new JPanel();
JTextField text1 = new JTextField(6);
JTextField text2 = new JTextField(6);
JTextField text3 = new JTextField(6);
JTextField text4 = new JTextField(6);
JTextField text5 = new JTextField(6);
JButton button1 = new JButton("Ãß°¡");
JButton button2 = new JButton("Á¦°Å");
JButton button3 = new JButton("»ó¼¼º¸±â");//2
panel.add(new JLabel("À̸§"));
panel.add(text1);
panel.add(new JLabel("³ªÀÌ"));
panel.add(text2);
panel.add(new JLabel("¼ºº°"));
panel.add(text3);
panel.add(new JLabel("ÇÚµåÆù"));
panel.add(text4);
panel.add(new JLabel("À̸ÞÀÏ"));
panel.add(text5);
panel.add(button1);
panel.add(button2);
panel.add(button3);//3
contenPane.add(panel, BorderLayout.SOUTH);
button1.addActionListener(new AddActionListener(table, text1, text2, text3, text4, text5));
button2.addActionListener(new RemoveActionListener(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
//-----------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
class AddActionListener implements ActionListener {
JTable table;
JTextField text1, text2, text3, text4, text5;
public AddActionListener(JTable table, JTextField text1, JTextField text2, JTextField text3, JTextField text4, JTextField text5) {
this.table = table;
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
this.text4 = text4;
this.text5 = text5;
}
public void actionPerformed(ActionEvent e) {
String arr[] = new String[5];
arr[0] = text1.getText();
arr[1] = text2.getText();
arr[2] = text3.getText();
arr[3] = text4.getText();
arr[4] = text5.getText();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(arr);
}
}
//----------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class RemoveActionListener implements ActionListener {
JTable table;
public RemoveActionListener(JTable table) {
this.table = table;
}
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row == -1)
return;
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.removeRow(row);
}
} | |
|