请教各位高手如何把一个内部类改写成一个单一的类,并用接口与其他的类结合起来.
比如说我写了一个数据库查询的程序.有一个MainFrame类.底下有几个内部类:查询,修改,增加,删除等.
我如何用接口把这些内部类改写成单一类,并且和MainFrame类连接起来?
求助!
现在的就这么多分,以后分多了一定给大家!!拜托了!
比如说我写了一个数据库查询的程序.有一个MainFrame类.底下有几个内部类:查询,修改,增加,删除等.
我如何用接口把这些内部类改写成单一类,并且和MainFrame类连接起来?
求助!
现在的就这么多分,以后分多了一定给大家!!拜托了!
refacing:)
可以这样做。
(要是你有together等工具可以自己拖出来。)
要是没有的话。
先把内部类CtrlC,CtrlP粘到新的文件里。
public class ...
{
class query_class{}
class modify_class{}
class increase_class{}
class erase_class{}
}
变成
public class ...{}
public class query_class{}//这里以下建立新文件
public class modify_class{}
public class increase_class{}
public class erase_class{}
接着是定义接口
public class database_operation
{
public ... query(...){ query_class_instance.dosth();}
public ... modify(...){modify_class_instance.dosth();}
public ... increase(...){increase_class_instance.dosth();}
public ... erase(...){erase_class_instance.dosth();}
}
ok:)
PS:
数据库操作为什么要用类封装啊?不明白:?
(用SQL语句定义一下不是更好)
举个例子,程序执行时,会弹出一个Frame,有一个table,里面显示的是我数据库表里的内容.然后在窗口中还有4个Button,分别是查询,修改等功能钮.如果我点查询按钮.就会弹出新的一个窗口,来实现具体功能.
这些类是我的内部类,我现在想把他们重写成单一的类,并用接口与主类连接,应如何操作?感谢您的
回复
在线等待解答~~~
急啊!~!!! public class Query extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
JLabel idLabel = new JLabel("编码", JLabel.LEFT);
JTextField tfname = new JTextField();
JLabel sexLabel = new JLabel("性别", JLabel.LEFT);
JComboBox sexBox = new JComboBox();
JButton okButton = new JButton("确定");
JButton cancelButton = new JButton("取消"); int sex;
public Query() {
try {
if(table==null)
table=new String[0][4]; jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1); setSize(290, 110);
pane.setLayout(gridbag); //编码设计
buildConstraints(constraints, 0, 0, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST; gridbag.setConstraints(idLabel, constraints);
okButton.addActionListener(new Query_okButton_actionAdapter(this));
pane.add(idLabel); // text field
buildConstraints(constraints, 1, 0, 1, 1, 50, 0);
constraints.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(tfname, constraints);
pane.add(tfname); //性别查询
buildConstraints(constraints, 0, 1, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST; gridbag.setConstraints(sexLabel, constraints);
pane.add(sexLabel); //性别选择
buildConstraints(constraints, 1, 1, 1, 1, 50, 40);
constraints.fill = GridBagConstraints.CENTER; sexBox.addItem("男");
sexBox.addItem("女");
gridbag.setConstraints(sexBox, constraints);
pane.add(sexBox); //确定
buildConstraints(constraints, 0, 2, 2, 1, 20, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.SOUTHEAST; gridbag.setConstraints(okButton, constraints);
pane.add(okButton);
//取消
buildConstraints(constraints, 2, 2, 2, 1, 0, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.SOUTHEAST; gridbag.setConstraints(cancelButton, constraints);
pane.add(cancelButton);
//content Pane
setContentPane(pane);
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw,
int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
} public void okButton_actionPerformed(ActionEvent e) { String ch=tfname.getText(); int i=sexBox.getSelectedIndex();
if(i==0)
sex=1;
else
sex=0; table=con.Connect(Integer.parseInt(ch),sex,table);
this.hide();
//System.out.println(table.length);
if(table.length==0)
;
else{
/* int y=0;
while(y<table.length){
System.out.println(MainFrame.table[y][0]);
System.out.println(MainFrame.table[y][1]);
System.out.println(MainFrame.table[y][2]);
System.out.println(MainFrame.table[y][3]);
y++;
}*/
MainFrame.this.init(); }
}}
主类是这个。现在我想的是把Query类单独做一个类,然后用接口与MainFrame这个类联系起来
请问该如何做
public class MainFrame extends JFrame {
JPanel contentPane;
// XYLayout xYLayout1 = new XYLayout();
JPanel m_Panel1 = new JPanel();
JPanel m_Panel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
JTable m_Table = new JTable();
JButton queryButton = new JButton();
// XYLayout xYLayout2 = new XYLayout();
JButton deleteButton = new JButton();
JButton addButton = new JButton();
JButton updateButton = new JButton();
Connect con=new Connect();
int id=-1;
public static String[][] table;
TableModel model;
String[] tableHeader = {
"编码", "名称", "性别",
"年龄"};
int flag=0;
int selectedRow;
public MainFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception { contentPane = (JPanel) getContentPane();
// contentPane.setLayout(xYLayout1);
setSize(new Dimension(500, 300));
setTitle("Frame Title");
queryButton.setText("查询");
queryButton.addActionListener(new MainFrame_queryButton_actionAdapter(this));
// jPanel2.setLayout(xYLayout2);
deleteButton.setText("删除");
deleteButton.addActionListener(new MainFrame_deleteButton_actionAdapter(this));
addButton.setText("增加");
addButton.addActionListener(new MainFrame_addButton_actionAdapter(this));
updateButton.setText("修改");
updateButton.addActionListener(new MainFrame_updateButton_actionAdapter(this));
m_Panel2.add(queryButton);//, new XYConstraints(12, 30, -1, -1));
m_Panel2.add(deleteButton);//, new XYConstraints(97, 31, 72, 24));
m_Panel2.add(addButton);//, new XYConstraints(183, 30, 78, -1));
m_Panel2.add(updateButton);//, new XYConstraints(273, 29, 87, 29));
jScrollPane1.getViewport().add(m_Table);
table=con.all(table);
m_Panel1.add(jScrollPane1);
contentPane.add(m_Panel1,BorderLayout.CENTER);//, new XYConstraints(1, 1, 500, 100));
contentPane.add(m_Panel2,BorderLayout.SOUTH);//, new XYConstraints(2, 199, 500, 150));
init();
addTable1LineListener();
}
public void init(){
if (table == null)
table = new String[0][4];
model = new AbstractTableModel() {
String[][] data = table; String[] head = tableHeader; public String getColumnName(int col) {
return head[col];
} public int getColumnCount() {
return 4;
} public int getRowCount() {
return data.length;
} public Object getValueAt(int row, int col) {
return data[row][col];
} public boolean isCellEditable(int row, int col) {
return true;
}
};
m_Table.setModel(model);
m_Table.repaint();
m_Table.updateUI();
}
public String[][] get(String[][] table){
if(table==null)
return null;
else
return table;
} public void queryButton_actionPerformed(ActionEvent e) {
flag=1;
Query dd=new Query();
dd.show();
}
void addTable1LineListener(){
ListSelectionModel rows = m_Table.getSelectionModel();
rows.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if (e.getValueIsAdjusting()) return;
ListSelectionModel row = (ListSelectionModel)e.getSource();
selectedRow = row.getMinSelectionIndex();
if(flag==0)
id = Integer.parseInt(table[selectedRow][0]); }
});
} public void deleteButton_actionPerformed(ActionEvent e) {
flag=1;
Delete dia=new Delete();
dia.show();
} public void addButton_actionPerformed(ActionEvent e) {
flag=1;
Add nd=new Add();
nd.show();
} public void updateButton_actionPerformed(ActionEvent e) {
flag=1;
Update Updatedialog=new Update();
Updatedialog.show(); }
}