import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class ddd extends JPanel{
private JPanel ctrlPanel = new JPanel();
public ddd()
{
this.add(ctrlPanel,BorderLayout.SOUTH);
}
public void setCtrlPanel(JPanel ctrlPanel) {
this.ctrlPanel = ctrlPanel;
}
public JPanel getCtrlPanel()
{
return ctrlPanel;
}
}
class CommonPanel extends JPanel
{
private JButton addBtn = new JButton( "添加 ");
private JButton updateBtn = new JButton( "修改 ");
private JButton delBtn = new JButton( "删除 ");
private JButton cancelBtn = new JButton( "取消 ");
public CommonPanel()
{
add(addBtn);
add(updateBtn);
add(delBtn);
add(cancelBtn);
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ee extends ddd{
private JButton addBtn = new JButton( "添加 ");
private JButton updateBtn = new JButton( "修改 ");
private JButton delBtn = new JButton( "删除 ");
private JButton cancelBtn = new JButton( "取消 ");
ee()
{
// JPanel p3 = this.getCtrlPanel();
// p3.add(addBtn);
// p3.add(updateBtn);
// p3.add(delBtn);
// p3.add(cancelBtn);
JPanel p3 = new CommonPanel();
this.setCtrlPanel(p3);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame ff = new JFrame();
ff.setSize(500,400);
ee e1 = new ee();
ff.add(e1);
ff.setVisible(true);
}
}
我用setCtrlPanel方法改变了this.ctrlPanel 的引用,为什么界面不会出现那4 个按钮。
------解决方案--------------------
class ddd1 extends JPanel {
private JPanel ctrlPanel = new JPanel();
public ddd1() {
this.add(ctrlPanel, BorderLayout.SOUTH);
}
public void setCtrlPanel(JPanel ctrlPanel) {
this.remove(this.ctrlPanel);
this.add(ctrlPanel);
this.ctrlPanel = ctrlPanel;
}
public JPanel getCtrlPanel() {
return ctrlPanel;
}
}
这样改就可以了
------解决方案--------------------
试试吧,调整好了.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0