如果标签的内容按预期工作,为什么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();
}
}
答案 0 :(得分:1)
以下是复制错误:
private void newUpdateTab(){
updatePanel = new SampleTJV();
newJobPanel.UpdateButton.setText("Update Job");
newJobPanel
可能没有意图。
同样错误的是static
GUI字段:
static JTextField TitleField;
答案 1 :(得分:0)
问题正是JB Nizet早先猜到的。它是静态方法和变量,应该是实例变量。 在我的示例代码SampleTJV中,如果从中删除单词static public static JTextField TitleField; 该计划完全符合预期。