此图所对应的java文件为EquipShow.java
如上图所示:
1)当鼠标点击其中某一行,再点删除按钮,删除掉当前行
2)当鼠标点击“添加”按钮,弹出一个新页面,
 此图所对应的java文件为AddEquip.java
六项都填写好之后,点击“确定”按钮,把刚才填写内容添加到表格的最后一行。
附两个java文件的代码如下:
EquipShow.java
package main;import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.table.*;public class EquipShow {
public static void main(String[] args) {
JFrame esjf = EquipShowFrame.getHandle();
esjf.setVisible(true);
}
}class EquipShowPanel extends JPanel implements ActionListener , MouseListener{
private EquipShowPanel() {
setVisible(true);
setLayout(new GridBagLayout());
equipShowAdd = new JButton("增加");
        equipShowModify = new JButton("修改");
        equipShowDelete = new JButton("删除");
        equipShowExit = new JButton("退出");
        
        paraCodText = new JTextField(10);
        paraNameText = new JTextField(10);
        paraUnitText = new JTextField(10);
        paraValueText = new JTextField(10);
        paraMaxValueText = new JTextField(10);
        paraMinValueText = new JTextField(10);
        
        paraCodLabel = new JLabel("参数编号");
        paraNameLabel = new JLabel("参数名称");
        paraUnitLabel = new JLabel("参数单位");
        paraValueLabel = new JLabel("正常取值");
        paraMaxValueLabel = new JLabel("最大取值");
        paraMinValueLabel = new JLabel("最小取值");
        
Object[][] data = {
{"GZ001","主蒸汽流量","t/h","670.000","680.000","650.000"},
{"GZ002","主蒸汽压力","Mpa","13.700","14.000","13.400"},
{"GZ003","主蒸汽温度","℃","540.000","550.000","525.000"},
};
String[] columnNames = {
"参数编号","参数名称","单位","正常值","最大值","最小值"
};
equipShowTable =  new JTable(data , columnNames);
equipShowTable.setPreferredScrollableViewportSize(new Dimension(400, 100));
GridBagConstraints c = new GridBagConstraints();
//中间省略框架布局代码
equipShowAdd.addActionListener(this);
equipShowModify.addActionListener(this);
equipShowDelete.addActionListener(this);
equipShowExit.addActionListener(this);
equipShowTable.addMouseListener(this);
   } public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == equipShowModify)
{
equipShowTable.setValueAt(paraCodText.getText(), 
equipShowTable.getSelectedRow(), 0);
equipShowTable.setValueAt(paraNameText.getText(), 
equipShowTable.getSelectedRow(), 1);
equipShowTable.setValueAt(paraUnitText.getText(), 
equipShowTable.getSelectedRow(), 2);
equipShowTable.setValueAt(paraValueText.getText(), 
equipShowTable.getSelectedRow(), 3);
equipShowTable.setValueAt(paraMaxValueText.getText(), 
equipShowTable.getSelectedRow(), 4);
equipShowTable.setValueAt(paraMinValueText.getText(), 
equipShowTable.getSelectedRow(), 5);

}
else if (source == equipShowAdd)
{
AddEquipFrame addEquipFrame = AddEquipFrame.getHandle();
addEquipFrame.setVisible(true);
AddEquipPanel addEquipPanel = new AddEquipPanel();
}
else if(source == equipShowDelete)
{
 //int row = equipShowTable.getSelectedRow(); 
 TableColumnModel columnModel = equipShowTable.getColumnModel();
 TableColumn monColumn = columnModel.getColumn(equipShowTable.getSelectedColumn());
 equipShowTable.removeColumn(monColumn);
}
else if (source == equipShowExit)
{
JOptionPane.showMessageDialog(this, "欢迎您再次使用!");
System.exit(0);
}
}
JLabel paraCodLabel;
JLabel paraNameLabel;
JLabel paraUnitLabel;
JLabel paraValueLabel;
JLabel paraMaxValueLabel;
JLabel paraMinValueLabel;

JTextField paraCodText;
JTextField paraNameText;
JTextField paraUnitText;
JTextField paraValueText;
JTextField paraMaxValueText;
JTextField paraMinValueText;

JButton equipShowAdd;
JButton equipShowModify;
JButton equipShowDelete;
JButton equipShowExit;

JTable equipShowTable; public void mouseClicked(MouseEvent arg0) {
paraCodText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),0));
paraNameText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),1));
paraUnitText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),2));
paraValueText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),3));
paraMaxValueText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),4));
paraMinValueText.setText((String)equipShowTable.getValueAt(
equipShowTable.getSelectedRow(),5));

} public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

} public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

} public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

} public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
private static final EquipShowPanel equipShowPanel = new EquipShowPanel();
public static EquipShowPanel getHandle()
{
return equipShowPanel;
}
}class EquipShowFrame extends JFrame {
private EquipShowFrame() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setSize(450, 280);
setTitle("设备基本参数管理");
setResizable(false);
    int screenHeight = d.height;
    int screenWidth = d.width;
    setLocation((screenWidth - 450) / 2, (screenHeight - 280) / 2);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); Container contentPane = getContentPane();
contentPane.add(equipShowPanel);
}
private static EquipShowFrame equipShowFrame = new EquipShowFrame();
public static EquipShowFrame getHandle()
{
return equipShowFrame;
}
EquipShowPanel equipShowPanel = EquipShowPanel.getHandle();

}

解决方案 »

  1.   

    AddEquip.java 代码如下:package main;import java.awt.*;
    import java.awt.event.*;import javax.swing.*;
    import javax.swing.table.*;public class AddEquip {
    public static void main(String[] args) {
    JFrame esjf = AddEquipFrame.getHandle();
    esjf.setVisible(true);
    }
    }class AddEquipPanel extends JPanel implements ActionListener {
    public AddEquipPanel() {
    setVisible(true);
    setLayout(new GridBagLayout());
    equipShowAdd = new JButton("确定");
    reset = new JButton("重置");
            
            paraCodText = new JTextField(10);
            paraNameText = new JTextField(10);
            paraUnitText = new JTextField(10);
            paraValueText = new JTextField(10);
            paraMaxValueText = new JTextField(10);
            paraMinValueText = new JTextField(10);
            
            paraCodLabel = new JLabel("参数编号");
            paraNameLabel = new JLabel("参数名称");
            paraUnitLabel = new JLabel("参数单位");
            paraValueLabel = new JLabel("正常取值");
            paraMaxValueLabel = new JLabel("最大取值");
            paraMinValueLabel = new JLabel("最小取值");
            
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraCodLabel, c);

    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraCodText, c);

    c.gridx = 3;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraNameLabel, c);

    c.gridx = 4;
    c.gridy = 5;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraNameText, c);

    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraUnitLabel, c);

    c.gridx = 1;
    c.gridy = 6;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraUnitText, c);

    c.gridx = 3;
    c.gridy = 6;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraValueLabel, c);

    c.gridx = 4;
    c.gridy = 6;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraValueText, c);

    c.gridx = 0;
    c.gridy = 7;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraMaxValueLabel, c);

    c.gridx = 1;
    c.gridy = 7;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraMaxValueText, c);

    c.gridx = 3;
    c.gridy = 7;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraMinValueLabel, c);

    c.gridx = 4;
    c.gridy = 7;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(paraMinValueText, c);

    c.gridx = 1;
    c.gridy = 8;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(equipShowAdd, c);

    c.gridx = 3;
    c.gridy = 8;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.4;
    c.weighty = 0.5;
    add(reset, c); equipShowAdd.addActionListener(this);
    reset.addActionListener(this);
       } public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if(source == equipShowAdd)
    {

    EquipShowPanel equipShowPanel = EquipShowPanel.getHandle();
    TableColumnModel columnModel = equipShowPanel.equipShowTable.getColumnModel();
     TableColumn monColumn = columnModel.getColumn(equipShowPanel.equipShowTable.getSelectedColumn());
    /*equipShowPanel.equipShowTable.addColumn(new TableColumn(
    1, 6, equipShowPanel.equipShowTable.getDefaultRenderer(this.getClass()),
    equipShowPanel.equipShowTable.getDefaultEditor(this.getClass())));*/
     equipShowPanel.equipShowTable.addColumn(monColumn);
    JOptionPane.showMessageDialog(this, "添加成功");
    AddEquipFrame.setShow(false);
    }
    else if(source == reset)
    {
    paraCodText.setText("");
    paraNameText.setText("");
    paraUnitText.setText("");
    paraValueText.setText("");
    paraMaxValueText.setText("");
    paraMinValueText.setText("");
    } }
    JLabel paraCodLabel;
    JLabel paraNameLabel;
    JLabel paraUnitLabel;
    JLabel paraValueLabel;
    JLabel paraMaxValueLabel;
    JLabel paraMinValueLabel;

    JTextField paraCodText;
    JTextField paraNameText;
    JTextField paraUnitText;
    JTextField paraValueText;
    JTextField paraMaxValueText;
    JTextField paraMinValueText;

    JButton equipShowAdd;
    JButton reset;}class AddEquipFrame extends JFrame {
    private AddEquipFrame() {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    setSize(420, 150);
    setTitle("设备基本参数管理");
    setResizable(false);
        int screenHeight = d.height;
        int screenWidth = d.width;
        setLocation((screenWidth - 420) / 2, (screenHeight - 150) / 2);
    setVisible(true);
    /*addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });*/ Container contentPane = getContentPane();
    contentPane.add(new AddEquipPanel());
    }
    private static AddEquipFrame addEquipFrame = new AddEquipFrame();
    public static AddEquipFrame getHandle()
    {
    return addEquipFrame;
    }
    public static void setShow(Boolean b)
    {
    //this.frame = frame;
    addEquipFrame.setVisible(b);
    }
    }请高手指点下,具体代码应该怎么写才能实现上述的两个功能,
    我在各论坛上面也找过一些答案,不过可能我比较笨,没有理会其中的含义,希望有高手能帮忙,小弟不胜感激