2012-08-17 94 views
2

我正嘗試以編程方式在java swing中創建嚮導。在java swing中的一個按鈕上執行多個操作

在嚮導窗格上,我有一個下一個按鈕,它必須根據嚮導上顯示的面板執行多個操作。

是否可以使用java命令模式?我可以知道嗎? 在此先感謝。

我用精靈的代碼是

this.mainPanel.add(fileSelectionPane,"SELECT FILE"); 
this.mainPanel.add(sqlConnectionPane,"SQL CONNECTION");   
this.mainPanel.add(devicePane,"PARSER");  
this.mainPanel.add(detailsPane,"DISPLAY");  
thisLayout.show(this.mainPanel,"SELECT FILE");  
this.finishButton.setEnabled(false); 
this.backButton.setEnabled(false); 
if(newValue==1) {  
    this.thisLayout.show(this.mainPanel, "SQL CONNECTION"); 
    this.nextButton.setEnabled(true);  
    this.nextButton.setText("Connect.."); 
    this.cancelButton.setEnabled(true);  
    this.backButton.setEnabled(true); 
} 

if(newValue==2) {  
    this.thisLayout.show(this.mainPanel, "PARSER"); 
    this.nextButton.setEnabled(true);  
    this.nextButton.setText("Parse.."); 
    this.cancelButton.setEnabled(true);  
    this.backButton.setEnabled(true);  
} 

我想下一個按鈕執行上選擇文件,SQL連接的具體行動。

是否可以使用命令模式?

+1

是不是更合理,讓當前的「面板」來決定行動需要採取而不是「下一步」按鈕什麼?? – MadProgrammer 2012-08-17 06:14:30

+2

請在評論中更新您的問題iso發帖代碼。如果您想使用Swing創建嚮導,請參閱[本文](http://java.sun.com/developer/technicalArticles/GUI/swing/wizard/) – Robin 2012-08-17 06:18:44

回答

1
  1. 具有看看CardLayout

  2. 付諸JDialog這些卡(JDialog已經默認BorderLayout preimplemented)到中心區域

  3. 創建一個新的JPanel和地點有JButtons

  4. JPanel with JButtons put to the SOUTH

  5. 搜索在這裏,在這個論壇上,也有基於CardLayout

3

確定爲嚮導或圖像previue幾個例子外觀極好,因此,您加入行動聽衆的按鈕。這些動作偵聽器在發生事件時會做某些事情。

你想改變按鈕的功能取決於哪個面板正在顯示?爲什麼不設置反映嚮導狀態的實例變量?

例如(約),

INT狀態= 0; // home panel

更改面板以幫助頁面,事件偵聽器正在觸發,將「狀態」設置爲1.您現在正在跟蹤正在顯示哪個面板。

現在,在你最初的問題中,當按鈕(你想要多個功能的那個按鈕)觸發時,你可以根據'狀態'變量選擇它將要採取的動作。

1

試試下面的代碼鍵:

JButton btn1; 
btn1= new javax.swing.JButton(); 
btn1.setToolTipText("Submit"); 
btn1.setContentAreaFilled(false); 
btn1.setBorderPainted(false); 
btn1.setMargin(new java.awt.Insets(2, 2, 2, 2)); 
btn1.addActionListener(this); 
btn1.setIcon(this.getIcons()[21]); 
add(btn1); // add to Jpanel 
btn1.setBounds(250,10, 12, 12); 

public void actionPerformed(java.awt.event.ActionEvent evt) { 
    Object obj = evt.getSource(); 
    if (obj == btn1) { 
     // your function on on click of button 
     return; 
    } 
相關問題