在我的應用程序中,我有一個包含基本面板的主框架。基座面板將容納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