Dawn' 2015-11-21 03:03 采纳率: 100%
浏览 1496
已采纳

要做咖啡机界面,我的代码怎么实现在主函数里接收右面板

这是右面板:
public class RightPanel extends JPanel{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
static JPanel p3 = new JPanel();
JButton b1,b2,b3,b4,b5,b6,b7;
static JPanel rp=null;
private RightPanel(){
this.setLayout(new FlowLayout());
p1.add(b1=new JButton("加水"));
p1.add(b2=new JButton("加粉"));
p1.add(b3=new JButton("加糖"));
p1.add(b4=new JButton("加杯"));
p2.add(b5=new JButton("投币"));
p2.add(b6=new JButton("退币"));
p2.add(b7=new JButton("取出"));
p3.add(p1);
p3.add(p2);
}
public static JPanel getInstance(){
rp=p3;
return rp;
}
}
这是主函数:
public class CoffeeATM {

public static void main(String[] args) {
    ATMFrame f = new ATMFrame();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}
class ATMFrame extends JFrame{
JPanel rp ;
ATMFrame(){
this.setSize(300,600);
rp=RightPanel.getInstance();
Container c= this.getContentPane();
c.add(rp,BorderLayout.EAST);
}
}
新手,求解答,我的一直是空白页,哪里错了?

  • 写回答

1条回答 默认 最新

  • lambda-fk 2015-12-22 12:49
    关注

    想写单例模式,就好好看看写的对错

        public static JPanel getInstance(){
            rp=p3;
            return rp;
        }
    
    

    私有的构造方法没有调用阿,同志。
    它能显示吗?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 阿里云python代码求解
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路