0

Why would the order of Tabs in a JTabbedPane affect if the content of the tab works as expected?

I am writing my first advanced application and am having some trouble with my JTabbedPane. Here's what I have:

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

newJobTab(), newUpdateTab() and newReportsTab() are placed into a JTabbed pane within the initialize() method. Each create an instance of a GUI class that I made. It basically has a bunch of Text fields, and comboboxes, etc. and they interact with a database to either populate the fields or collect info from the fields.

The functionality of the buttons on the tab are the main difference between the three. Individually, each tab works as I would expect it to. When I place them in the Tabbed pane, only the third tab works properly. If I switch around the order, it is the same deal. Whichever one is the third tab is the only one that functions the way I want.

Here is my revision to my original post...now with code.

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();
}

}

4

2 回答 2

1

以下是复制错误:

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

newJobPanel可能不是有意的。


staticGUI字段也是错误的:

static JTextField TitleField;
于 2013-05-13T01:22:34.430 回答
0

这个问题正是 JB Nizet 早先猜到的。本来应该是实例变量的是静态方法和变量。在我的示例代码 SampleTJV 中,如果从 public static JTextField TitleField 中删除单词 static;该程序完全按预期工作。

于 2013-05-13T03:02:09.987 回答