在我的应用程序中,我有一个包含基本面板的主框架。基座面板将容纳4个子面板。因此可以说,第一个子面板出现,对数据执行一个过程,然后它必须让第二个子面板出现,并将结果数据传递给它。第二个子面板必须进行另一次计算并将其传递给第三个面板,依此类推。 我为此使用了卡片布局,但我不知道如何在前面板的工作结束后使面板可见。如何在同一帧中一个接一个地运行面板
下面是一个简化的版本:
public class LittleCardLayout{
public static void main(String[] args) {
new LittleCardLayout();
}
public LittleCardLayout(){
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BasePane());
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
基板材:
public class BasePane extends JPanel{
public BasePane() {
setLayout(new BorderLayout());
CardLayout cl = new CardLayout();
JPanel mainView = new JPanel(cl);
mainView.add(new JPanel(), "empty");
mainView.add(new TopPanel(), "toppanel");
cl.show(mainView, "toppanel");
add(mainView, BorderLayout.NORTH);
}
}
子面板1:
public class TopPanel extends JPanel {
int myValue = 23;
int newVal;
public TopPanel(){
JButton btn = new JButton("Load Value");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newVal= 23-3;
add(new BottomPanel(newVal), "toppanel");
}
});
add(btn, BorderLayout.NORTH);
}
}
子面板2:
public class BottomPanel extends JPanel {
int num;
JLabel myLabel = new JLabel();
BottomPanel(int num){
this.num = num;
num = num*5;
myLabel.setText(Integer.toString(num));
add(myLabel);
}
}
它必须像循序渐进的过程一样工作。 因此,任何想法将数据从一个面板传递到另一个面板并使它们彼此可见? 也是正确的方式来做出这样的一步一步的过程,或者有更好的主意?
@Aify它的顶部的一些想法传递数据,但不会使第二个面板可见。 – Dan 2015-02-11 00:17:47