设计思路:
1、MyFrame
package swing02;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.net.URL;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class MyFrame extends JFrame {
// Model:负责数据
DefaultTableModel tableModel = new DefaultTableModel();
// View:负责显示,创建JTable的时候指定一个Model
JPanel root = new JPanel();
JTable table = null;
public MyFrame(String title) {
super(title);
this.setContentPane(root);
root.setLayout(new BorderLayout());
//表格初始化
initTable();
//初始化工具栏
initToolBar();
}
private void initTable()
{
//创建JTable,直接重写isCellEditable(),设为不可编辑
table=new JTable(tableModel) {
@Override
public boolean isCellEditable(int row, int column) {
// TODO Auto-generated method stub
return false;
}
};
// 添加至滚动面板
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);// 确保表永远不会小于视图区
table.setRowSelectionAllowed(true);// 整行选择
table.setRowHeight(30);
root.add(scrollPane, BorderLayout.CENTER);
// 初始化设置
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("性别");
tableModel.addColumn("出生日期");
tableModel.addColumn("手机号");
// 列设置自定义绘制
table.getColumnModel().getColumn(2).setCellRenderer(new SexColumnRenderer());
table.getColumnModel().getColumn(0).setCellRenderer(new IDColumnRenderer());
table.getColumnModel().getColumn(0).setPreferredWidth(110);
}
//
private void addTableRow(Student stu) {
// 方法01
// import java.util.Vector是个泛型,表示数组
// Vector<Object>rowData=new Vector<>();
// rowData.add(stu.id);
// rowData.add(stu.name);
// rowData.add(stu.sex);
// rowData.add(stu.birthday);
// rowData.add(stu.cellphone);
// tableModel.addRow(rowData);
// 方法2
Object[] rowData = new Object[5];
rowData[0] = stu.id;
rowData[1] = stu.name;
rowData[2] = stu.sex;
rowData[3] = stu.birthday;
rowData[4] = stu.cellphone;
tableModel.addRow(rowData);
}
private void initToolBar()
{
JToolBar toolBar=new JToolBar();
root.add(toolBar, BorderLayout.PAGE_START);
toolBar.setFloatable(false);
//按钮
JButton addButton=createToolButton("添加","ic_add.png");
toolBar.add(addButton);
toolBar.addSeparator();
addButton.addActionListener((e)->{
showAddDialog();
});
// 按钮
JButton deleteButton = createToolButton("删除", "ic_delete.png" );
toolBar.add(deleteButton);
deleteButton.addActionListener( (e)->{
});
}
private JButton createToolButton(String buttonName,String iconName)
{
//图标
String imagePath="/icons/"+iconName;
URL imageURL=getClass().getResource(imagePath);
//创建按钮
JButton button=new JButton(buttonName);
button.setToolTipText(buttonName);
button.setIcon(new ImageIcon(imageURL));
//button.setFocusPainted(false);
return button;
}
private void showAddDialog()
{
EditStudentDialog dlg=new EditStudentDialog(this);
if( dlg.exec() )
{
Student stu=dlg.getValue();
addTableRow( stu);
}
}
}
2、SexColumnRenderer
package swing02;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;
public class SexColumnRenderer extends JLabel implements TableCellRenderer
{
public SexColumnRenderer()
{
this.setHorizontalAlignment(SwingConstants.CENTER);
this.setFont(this.getFont().deriveFont(Font.PLAIN));
this.setBackground(new Color(0,0,0,0));
}
//设置颜色...以及...男-女
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
boolean sex=(boolean)value;
if(sex==true)
this.setText("男");
else
this.setText("女");
//背景
this.setOpaque(true);
if(isSelected)
{
this.setBackground(table.getSelectionBackground());
this.setForeground(table.getSelectionForeground());
}
else
{
this.setBackground(table.getBackground());
this.setForeground(table.getForeground());
}
return this;
}
}
3、IDColumnRenderer
package swing02;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
//此处用于选中状态(JCheckBox)
public class IDColumnRenderer extends JCheckBox implements TableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
//JCheckBox默认选中状态
this.setSelected(isSelected);
//学号信息
if(value!=null)
{
this.setText(value.toString());
}
//背景
this.setOpaque(true);//保持可以进行面板绘制
if(isSelected)
{
this.setBackground(table.getSelectionBackground());
this.setForeground(table.getSelectionForeground());
}
else
{
this.setBackground(table.getBackground());
this.setForeground(table.getForeground());
}
return this;
}
}
4、EditStudentDialog
package swing02;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import js.swing.JsColumnLayout;
import js.swing.JsPanel;
import js.swing.JsRowLayout;
public class EditStudentDialog extends JDialog
{
public JTextField idField=new JTextField(20); //学号
public JTextField nameField=new JTextField(20); //姓名
public JComboBox<String> sexField=new JComboBox<>(); //下拉框
public JTextField phoneField=new JTextField(20); //手机号
public JTextField birthField=new JTextField(20); //生日(暂用文本来表示)
JButton okButton=new JButton("确定");
//默认是取消
private boolean retValue=false;
public EditStudentDialog(JFrame owner)
{
super(owner,"编辑学生信息",true);
this.setSize(300, 300);
//设置一个容器
JsPanel root=new JsPanel();
this.setContentPane(root);
this.setLayout(new JsColumnLayout(10));
root.padding(10);
//中间面板
JsPanel main=new JsPanel();
root.add(main, "1w");//占据中间区域
main.setLayout(new JsColumnLayout(10));
main.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
if(true)
{
JsPanel row=new JsPanel();
main.add(row,"24px");
row.setLayout(new JsRowLayout(10));
row.add(new JLabel("学号"), "50px");
row.add(idField, "1w");
}
if(true)
{
JsPanel row=new JsPanel();
main.add(row,"24px");
row.setLayout(new JsRowLayout(10));
row.add(new JLabel("姓名"), "50px");
row.add(nameField, "1w");
}
if(true)
{
JsPanel row=new JsPanel();
sexField.addItem("男");
sexField.addItem("女");
main.add(row,"24px");
row.setLayout(new JsRowLayout(10));
row.add(new JLabel("性别"), "50px");
row.add(sexField, "1w");
}
if(true)
{
JsPanel row=new JsPanel();
main.add(row,"24px");
row.setLayout(new JsRowLayout(10));
row.add(new JLabel("生日"), "50px");
row.add(birthField, "1w");
}
if(true)
{
JsPanel row=new JsPanel();
main.add(row,"24px");
row.setLayout(new JsRowLayout(10));
row.add(new JLabel("手机号"), "50px");
row.add(phoneField, "1w");
}
// if(true)
// {
// JsPanel row=new JsPanel();
// main.add(row,"24px");
// row.setLayout(new JsRowLayout(10));
// row.add(new JLabel("生日"), "50px");
// row.add(birthField, "1w");
// }
//底下
JsPanel button=new JsPanel();
root.add(button, "30px"); //底部区域30px
button.setLayout(new JsRowLayout(10));
button.add(new JLabel(),"1w"); //占位
button.add(okButton, "auto"); //按钮靠右显示
//lamada表达式
okButton.addActionListener((e)->{
if(checkValid())
{
retValue=true; //设置对话框的返回值
setVisible(false);//MyDialog.this.setVisible(false)
}
});
}
//返回值为true:表明用户点击了"确定"
//返回值为false:表示用户×掉了窗口,或者点了取消
public boolean exec()
{
//相对owner居中显示
Rectangle frmRect=this.getOwner().getBounds();
int width=this.getWidth();
int height=this.getHeight();
int x=frmRect.x+(frmRect.width-width)/2;
int y=frmRect.y+(frmRect.height-height)/2;
this.setBounds(x, y, width, height);
//显示窗口(阻塞,直到对话框窗口关闭)
this.setVisible(true);
return retValue;
}
//检查输入有效性
public boolean checkValid()
{
Student v=getValue();
if(v.id.isEmpty())
{
JOptionPane.showMessageDialog(this, "学号不能为空");
return false;
}
if(v.name.isEmpty())
{
JOptionPane.showMessageDialog(this, "姓名不得为空!");
return false;
}
return true;
}
//设置初始值
public void setValue(Student v)
{
idField.setText(v.id);
nameField.setText(v.id);
sexField.setSelectedIndex(v.sex? 1:0); //条件表达式
phoneField.setText(v.id);
birthField.setText(v.id);
}
//获取用户的输入
public Student getValue()
{
Student v=new Student();
v.id=idField.getText().trim();
v.name=nameField.getText().trim();
v.sex=sexField.getSelectedIndex()==0;
v.cellphone=phoneField.getText().trim();
v.birthday=birthField.getText().trim();
return v;
}
}
5、Student
package swing02;
/*
* 表格里得数据
*/
public class Student
{
public String id;
public String name;
public boolean sex;
public String birthday;
public String cellphone;
}
6、MyDemo
package swing02;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyDemo
{
private static void createGUI()
{
// JFrame指一个窗口,构造方法的参数为窗口标题
JFrame frame = new MyFrame("信息列表");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口的其他参数,如窗口大小
frame.setSize(500, 300);
// 显示窗口
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}