在myeclipse环境下,用我上一篇所说的new一个panel组件,取名为listPanel。这个面板是用来显示我们一进程序的界面。将此面板加到JFrame中显示。然后利用界面传参技术,将AddressApp(JFrame)作为一个参数mianFrame传入listPanel来实现调度。
作为ui层我们要显示的话就要用Jlist来调用逻辑层来显示数据。这里我们用Myset来做逻辑层。所以Myset中就有一个getAll()函数来返回一个数组用来构建list。其次我们还要添加转向其他面板的按钮,效果图如下所示:
具体实现代码如下:
/*
* listPanel.java
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import cn.hncu.busines.Myset;
/**
*
* @author __USER__
*/
public class listPanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
/** Creates new form listPanel */
public listPanel(JFrame mainFrame) {
this.mainFrame = mainFrame;
initComponents();
myInitComponents();
this.setOpaque(false);
Icon bjImage = new ImageIcon("./image/beijing.jpg");
JLabel bglbl = new JLabel(bjImage);
this.add(bglbl, new Integer(Integer.MIN_VALUE));
bglbl.setBounds(0, 0, bjImage.getIconWidth(), bjImage.getIconHeight());
this.validate();
}
public listPanel(JFrame mainFrame,Object[] objs) {
this.mainFrame = mainFrame;
initComponents();
listAddress.setListData(objs);
this.setOpaque(false);
Icon bjImage = new ImageIcon("./image/beijing.jpg");
JLabel bglbl = new JLabel(bjImage);
this.add(bglbl, new Integer(Integer.MIN_VALUE));
bglbl.setBounds(0, 0, bjImage.getIconWidth(), bjImage.getIconHeight());
this.validate();
}
private void myInitComponents() {
Myset set = new Myset();
Object objs[] = set.getAll();
listAddress.setListData(objs);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
btnAdd = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
listAddress = new javax.swing.JList();
btnQuery = new javax.swing.JButton();
setMinimumSize(new java.awt.Dimension(800, 600));
setPreferredSize(new java.awt.Dimension(800, 600));
setLayout(null);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 36));
jLabel1.setForeground(new java.awt.Color(0, 255, 255));
jLabel1.setText("\u5730\u5740APP");
add(jLabel1);
jLabel1.setBounds(300, 40, 160, 47);
btnAdd.setBackground(new java.awt.Color(153, 153, 255));
btnAdd.setFont(new java.awt.Font("Microsoft YaHei UI", 2, 36));
btnAdd.setForeground(new java.awt.Color(51, 51, 255));
btnAdd.setText("add...");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
add(btnAdd);
btnAdd.setBounds(130, 370, 190, 60);
btnUpdate.setBackground(new java.awt.Color(255, 102, 153));
btnUpdate.setFont(new java.awt.Font("Microsoft YaHei UI", 2, 36));
btnUpdate.setForeground(new java.awt.Color(204, 204, 255));
btnUpdate.setText("update...");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
add(btnUpdate);
btnUpdate.setBounds(440, 370, 210, 60);
btnDelete.setBackground(new java.awt.Color(255, 0, 51));
btnDelete.setFont(new java.awt.Font("Microsoft YaHei UI", 2, 24));
btnDelete.setForeground(new java.awt.Color(255, 51, 255));
btnDelete.setText("Delete...");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
add(btnDelete);
btnDelete.setBounds(130, 460, 180, 50);
listAddress.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
jScrollPane1.setViewportView(listAddress);
add(jScrollPane1);
jScrollPane1.setBounds(140, 140, 460, 210);
btnQuery.setFont(new java.awt.Font("Microsoft YaHei UI", 3, 24));
btnQuery.setForeground(new java.awt.Color(0, 153, 255));
btnQuery.setText("\u8f6c\u5230\u67e5\u8be2...");
btnQuery.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnQueryActionPerformed(evt);
}
});
add(btnQuery);
btnQuery.setBounds(440, 460, 200, 50);
}// </editor-fold>
//GEN-END:initComponents
private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new QueryPanel(mainFrame));
mainFrame.validate();
}
// private JList<Object> getListAddress() {
// if(listAddress==null){
// listAddress=new JList<Object>();
// }
// Myset set=new Myset();
// Object objs[]=set.getAll();
// listAddress.setListData(objs);
// return listAddress;
// }
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String updateStr = (String) listAddress.getSelectedValue();
if(updateStr!=null){
mainFrame.setContentPane(new updatePanel(mainFrame, updateStr));
//mainJrame.setContentPane(new updatePanel(mainJrame,updateStr,index));
mainFrame.validate();
}else{
JOptionPane.showMessageDialog(this, "请添加联系人");
mainFrame.setContentPane(new addJpanel(mainFrame));
mainFrame.validate();
}
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
String deleteStr = (String) listAddress.getSelectedValue();
if(deleteStr!=null){
mainFrame.setContentPane(new deleteJpanel(mainFrame, deleteStr));
mainFrame.validate();
}else{
JOptionPane.showMessageDialog(this, "无联系人,请添加");
mainFrame.setContentPane(new addJpanel(mainFrame));
mainFrame.validate();
}
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new addJpanel(mainFrame));
mainFrame.validate();
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnQuery;
private javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList listAddress;
// End of variables declaration//GEN-END:variables
}