用java编写一个表格,下面带三个按钮,三个按钮分别为添加,删除,保存。 100
1个回答
展开全部
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
JButton[] bts={new JButton("添加"),new JButton("删除"),new JButton("修改")};
JFrame jf = new JFrame("用户管理");
jf.setLayout(new BorderLayout());
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setAutoResizeMode(1);
jp.add(table);
jp2.add(bts[0]);
jp2.add(bts[1]);
jp2.add(bts[2]);
jp.add(jp2);
jf.setSize(500, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jp,BorderLayout.CENTER);
jf.setVisible(true);
}
}
class MyTableModel implements TableModel {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
int columnCount = 4;
String[] title = { "ID", "姓名", "年龄", "性别" };
String[] countKey = { "id", "name", "age", "sex" };
public MyTableModel() {
for (int i = 0; i < 10; i++) {
Map<String, String> row = new HashMap<String, String>();
row.put("id", String.valueOf(i));
row.put("name", "siwng" + i);
row.put("age", "20");
row.put("sex", "男");
list.add(row);
}
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public int getColumnCount() {
return this.columnCount;
}
@Override
public String getColumnName(int columnIndex) {
return title[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Map<String, String> row = list.get(rowIndex);
return row.get(countKey[columnIndex]);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
JButton[] bts={new JButton("添加"),new JButton("删除"),new JButton("修改")};
JFrame jf = new JFrame("用户管理");
jf.setLayout(new BorderLayout());
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setAutoResizeMode(1);
jp.add(table);
jp2.add(bts[0]);
jp2.add(bts[1]);
jp2.add(bts[2]);
jp.add(jp2);
jf.setSize(500, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jp,BorderLayout.CENTER);
jf.setVisible(true);
}
}
class MyTableModel implements TableModel {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
int columnCount = 4;
String[] title = { "ID", "姓名", "年龄", "性别" };
String[] countKey = { "id", "name", "age", "sex" };
public MyTableModel() {
for (int i = 0; i < 10; i++) {
Map<String, String> row = new HashMap<String, String>();
row.put("id", String.valueOf(i));
row.put("name", "siwng" + i);
row.put("age", "20");
row.put("sex", "男");
list.add(row);
}
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public int getColumnCount() {
return this.columnCount;
}
@Override
public String getColumnName(int columnIndex) {
return title[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Map<String, String> row = list.get(rowIndex);
return row.get(countKey[columnIndex]);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询