2013-05-12 34 views
0

如果選項卡的內容按預期工作,爲什麼JTabbedPane中的選項卡順序會受到影響?JTabbedPane中的選項卡會影響內容

我寫了我的第一個高級應用程序,並且遇到了一些JTabbedPane問題。 這是我有:

public ProjectTracker() { 
    initialize(); 
    newJobTab(); 
    newUpdateTab(); 
    newReportsTab(); 
} 

newJobTab(),newUpdateTab()和newReportsTab()被放入initialize()方法中的JTabbed窗格。每個都創建一個我創建的GUI類的實例。它基本上有一堆文本字段和組合框等,它們與數據庫交互以填充字段或從字段收集信息。

選項卡上按鈕的功能是三者之間的主要區別。單獨地,每個標籤按我預期的那樣工作。當我將它們放置在「選項卡式」窗格中時,只有第三個選項卡正常工作。如果我切換訂單,這是一樣的交易。無論哪一個是第三個標籤是唯一以我想要的方式運作的標籤。

這是我對原始文章的修改......現在用代碼。

public class SampleTracker { 

private JFrame frmProjectTracker; 
private JTabbedPane tabbedPane; 
public String Title; 

SampleTJV newJobPanel; 
SampleTJV updatePanel; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       SampleTracker window = new SampleTracker(); 
       window.frmProjectTracker.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public SampleTracker() { 
    initialize(); 
    newJobTab(); 
    newUpdateTab(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frmProjectTracker = new JFrame(); 
    frmProjectTracker.setBounds(100, 100, 662, 461); 
    frmProjectTracker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frmProjectTracker.getContentPane().setLayout(new FormLayout(new ColumnSpec[] { 
      ColumnSpec.decode("662px"),}, 
     new RowSpec[] { 
      RowSpec.decode("50px"), 
      RowSpec.decode("389px"),})); 

    tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
    frmProjectTracker.getContentPane().add(tabbedPane, "1, 2, fill, fill"); 
} 


private void newJobTab(){ 
    newJobPanel = new SampleTJV(); 
    newJobPanel.UpdateButton.setText("Enter Job"); 
    tabbedPane.addTab("Enter New Job", null, newJobPanel, null); 
    newJobPanel.UpdateButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
       newJobPanel.collectInfo(); 
       Title = newJobPanel.Title; 
       //Here the connection to DB is made and the Title is written to DB 
       newJobPanel.newJobField.setText(Title); 
      } 
    }); 
} 

private void newUpdateTab(){ 
    updatePanel = new SampleTJV(); 
    newJobPanel.UpdateButton.setText("Update Job"); 
    tabbedPane.addTab("Update Job", null, updatePanel, null); 
    updatePanel.UpdateButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      updatePanel.collectInfo(); 
      Title = updatePanel.Title; 
      updatePanel.updateJobField.setText(Title); 
     } 
    }); 
} 

}

public class SampleTJV extends JPanel { 

private static final long serialVersionUID = 1L; 
public static JTextField TitleField; 

public String Title; 
public JButton UpdateButton; 
public JTextField newJobField; 
public JTextField updateJobField; 
/** 
* Create the panel. 
*/ 
public SampleTJV() { 
    setLayout(null); 
    TitleField = new JTextField(); 

    TitleField.setColumns(10); 
    TitleField.setBounds(109, 6, 134, 28); 
    add(TitleField); 

    newJobField = new JTextField(); 
    newJobField.setBounds(171, 79, 134, 28); 
    add(newJobField); 
    newJobField.setColumns(10); 

    UpdateButton = new JButton("Update Job"); 
    UpdateButton.setBounds(267, 7, 112, 29); 
    add(UpdateButton); 

    JLabel lblNewJobResult = new JLabel("New Job Result"); 
    lblNewJobResult.setBounds(47, 85, 112, 16); 
    add(lblNewJobResult); 

    JLabel lblUpdateJobResult = new JLabel("Update Job Result"); 
    lblUpdateJobResult.setBounds(47, 125, 112, 16); 
    add(lblUpdateJobResult); 

    updateJobField = new JTextField(); 
    updateJobField.setColumns(10); 
    updateJobField.setBounds(171, 119, 134, 28); 
    add(updateJobField); 

} 

public void collectInfo(){ 
    Title = TitleField.getText(); 
} 

}

+2

您必須發佈代碼。我的猜測是你有靜態變量和方法,它們應該是實例變量和方法。另外請確保定義「不起作用」。究竟發生了什麼?有什麼異常?什麼? – 2013-05-12 07:33:21

+0

你如何在JTabbedPane中添加這些選項卡?一些代碼...? – 2013-05-12 07:34:25

+1

1)*「我不知道如何在不發佈大量代碼的情況下發布更多代碼。」*爲了更快地獲得更好的幫助,請發佈** [SSCCE](http://sscce.org/)。** 2)始終複製/粘貼錯誤和異常輸出。 – 2013-05-12 07:35:24

回答

1

下面是一個複製錯誤:

private void newUpdateTab(){ 
    updatePanel = new SampleTJV(); 
    newJobPanel.UpdateButton.setText("Update Job"); 

newJobPanel有可能是不希望。


也是錯誤的是static GUI字段:

static JTextField TitleField; 
+0

你是對的。這只是一個複製錯誤。純粹的化妝品,而不是問題的答案。 – phischer 2013-05-13 02:58:32

+0

然後'靜態';沒有看到第一次。 – 2013-05-13 07:15:16

0

的問題到底是什麼JB Nizet早猜到了。它是靜態方法和變量,應該是實例變量。 在我的示例代碼SampleTJV中,如果您從 中刪除靜態字static static JTextField TitleField; 該程序完全按照預期工作。

相關問題