【问题标题】:Tabs in a JTabbedPane affect the contentJTabbedPane 中的选项卡会影响内容
【发布时间】:2013-05-12 07:31:22
【问题描述】:

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

}

【问题讨论】:

  • 您必须发布代码。我的猜测是你有静态变量和方法,它们应该是实例变量和方法。还要确保定义“不起作用”。究竟会发生什么?有什么例外吗?什么?
  • 如何在 JTabbedPane 中添加这些选项卡?一些代码...?
  • 1) “我不知道如何在不发布大量代码的情况下发布更多代码。” 为了尽快获得更好的帮助,请发布 SSCCE . 2) 始终复制/粘贴错误和异常输出。
  • 这里有一个简短的example 供参考。
  • “我不确定如何在不发布大量代码的情况下发布更多代码。”您可以通过将程序克隆到一个小的(er)示例来创建和添加选项卡式窗格,方法与您现在相同。当你这样做时,你很有可能会自己发现问题;如果没有,您将有足够小的东西发布,并且代码将简化为您需要提出的问题。如果您从没有上下文的人的角度阅读到目前为止发布的内容,您可能会发现我们没有机会识别问题。

标签: java swing jtabbedpane


【解决方案1】:

以下是复制错误:

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

newJobPanel 可能不是故意的。


static GUI 字段也是错误的:

static JTextField TitleField;

【讨论】:

  • 你是对的。那只是一个复制错误。纯粹是装饰性的,而不是问题的答案。
  • 那么static也是;第一次没看到。
【解决方案2】:

问题正是 JB Nizet 早先猜到的。本来应该是实例变量的是静态方法和变量。 在我的示例代码 SampleTJV 中,如果您从 公共静态 JTextField 标题字段; 该程序完全按预期运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2012-08-30
    相关资源
    最近更新 更多