将新组件添加到在另一个类中创建的JPanel

时间:2017-11-03 13:12:05

标签: java swing jpanel awt jlabel

我的var hasHeader = false; var hasFooter = false; // users choice of layout var customPosition = []; $(this).siblings('.layoutOptions').children('input:checked').each(function(i) { customPosition[i] = $(this).val(); if ( customPosition[i]=='content-header'){ hasHeader = true; }else if (customPosition[i]=='content-footer'){ hasFooter = true; } }); $(this).siblings('.contentOptions').children('input:checked').val(); for (i = 0; i< customPosition.length; i++){ console.log(i); console.log(customPosition[i]); var canvField = (canvasBHeight - shadowBVal*2) - strokeBWidth*2; if (customPosition[i] == 'content-header'){ //header positioning var group = new fabric.Group([],{ left: strokeBWidth + 10, stroke: '#ddd', strokeLineJoin: 'round', strokeWidth: 2, hasControls: true, lockMovementX: false, lockMovementY: false, lockRotation: true, selectable: true, position: posB, fill: '#ccc', width: ((canvasBWidth - shadowBVal*2) - strokeBWidth*2 - 10), //height: canvasBHeight/5 - strokeBWidth, height: canvField/5, top: strokeBWidth, ry: 0, rx: 20, //originX: 'center', //originY: 'center' }); //add dummy content fabric.Image.fromURL('image/bordenconfigurator/dummies/header.png', function(oImg) { oImg.set({ }); group.add(oImg); console.log('group '+i+' '+customPosition[i]+':'); console.log(JSON.stringify(group)); //console.log(group); }); }else if(customPosition[i] == 'content-footer') { console.log(customPosition[i]); //footer positioning var group = new fabric.Group([],{ left: strokeBWidth + 10, stroke: '#ddd', strokeLineJoin: 'round', strokeWidth: 2, hasControls: true, lockMovementX: false, lockMovementY: false, lockRotation: true, selectable: true, position: posB, fill: '#ccc', width: ((canvasBWidth - shadowBVal*2) - strokeBWidth*2 - 10), //height: canvasBHeight/5 - strokeBWidth, height: canvField/5, top: strokeBWidth + canvField*4/5 + 10, ry: 0, rx: 20, //originX: 'center', //originY: 'center' }); //add dummy content fabric.Image.fromURL('image/bordenconfigurator/dummies/footer.png', function(oImg) { oImg.set({ top: canvasBHeight- strokeBWidth-shadowBVal, }); group.add(oImg); canvasBuild.centerObjectH(oImg); console.log('group '+i+' '+customPosition[i]+':'); console.log(JSON.stringify(group)); }); }else{ console.log(customPosition[i]); //body positioning var height,top; if (hasHeader === true && hasFooter === true){ height = canvField*3/5 + 5; top = strokeBWidth + canvField/5 + 5; }else if(hasHeader ===true && hasFooter === false){ height = canvField*4/5 + 5; top = strokeBWidth + canvField/5 + 5; }else if(hasHeader ===false && hasFooter === true){ height = canvField*4/5; top = strokeBWidth + 5; } var group = new fabric.Group([],{ left: strokeBWidth + 10, stroke: '#ddd', strokeLineJoin: 'round', strokeWidth: 2, hasControls: true, lockMovementX: false, lockMovementY: false, lockRotation: true, selectable: true, position: posB, fill: 'rgba(255,255,255,0)', width: ((canvasBWidth - shadowBVal*2) - strokeBWidth*2 - 10), height: height, //top: strokeBWidth + canvField/5 + 5, top: top, ry: 20, rx: 20 }); } group.toObject = (function(toObject) { return function() { return fabric.util.object.extend(toObject.call(this), { customPosition: this.customPosition, label: this.label, id: this.id }); }; })(group.toObject); group.customPosition = customPosition[i]; group.id = posB; canvasBuild.add(group); group.length = 0; posB++; } //addOrientationLines(); //canvasBuild.renderAll(); //console.log(JSON.stringify(canvasBuild)); }); 网格为JFrame(使用JPanels)。每个Borderlayout都包含JPanel(由于位于JButton内,因此会扩展为完整大小)。所以我有像扫雷一样的东西。

这些按钮有一个监听器,带有构造函数Listener(面板),所以我可以使用监听器中的面板。

如果我在监听器中执行Borderlayout,则按钮会消失,但panel.removeAll();仍然存在,因此我可以获得一个可用空间。

我做JPanel并且它有效,但如果我想添加一个组件,例如另一个按钮或panel.setBackground(Color.pink);,它就不起作用。它可以在同一个类中工作但不能分开,或者在类的方法中工作。

谢谢。希望你能理解!

这是我的听众课程:

JLabel

这里是创建网格的类:

public class ListenerCasillas implements ActionListener {

    JPanel panel;

    ListenerCasillas(JPanel panel){

    this.panel = panel;

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        panel.removeAll();//works
        panel.setBackground(Color.green);//works
        panel.add(new JLabel("1"));//doesn't work
        panel.repaint();//works

    }

}

完整的java项目(如果你想测试): https://drive.google.com/file/d/0B0WNwgY4eOjvNmNHM0E5U0FxVGc/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

解决方法是在revalidate() ListenerCasillas方法中调用actionPerformed()

public class ListenerCasillas implements ActionListener {
    JPanel panel;

    ListenerCasillas(JPanel panel){
        this.panel = panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        panel.removeAll();
        panel.setBackground(Color.green);
        panel.add(new JLabel("1"));
        panel.revalidate();
        panel.repaint();
    }
}

有关repaint()revalidate()的详细信息,请查看this brilliant answer