我寫了一個應用程序,它連接到多個數據庫(檢查link)。我分別有幾個包含帶有INPUT文本框的查詢面板的選項卡。現在我想要到達選定選項卡中的特定文本字段以查詢相關數據庫......請讓我現在瞭解如何實現它?當我的應用程序有幾個相同的選項卡時,如何到達選項卡中的組件?(如何確定活動選項卡?)
我要指出,我已經創建了一個應該被添加到JTabbedPane的如下JPanel的一類:
package testGUI_V2;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import static testGUI_V2.DialogBoxTest.service;
public class TabbedPane {
private String state;
private JLabel jLabel3;
private JTextField jTextField2;
private JTextPane jTextPane1;
private JButton jButton4;
private static int item = 0;
private int tabIndex;
public JPanel CreatePanel(){
state = service.toUpperCase();
tabIndex = item;
JPanel inst1 = new JPanel();
String tabTitle =String.format("Query Panel %d",item+1);
inst1.setSize(302, 138);
inst1.setLayout(null);
inst1.setBorder(BorderFactory
.createTitledBorder(BorderFactory
.createEtchedBorder(BevelBorder.LOWERED), tabTitle));
inst1.setPreferredSize(new java.awt.Dimension(321, 108));
jLabel3 = new JLabel("Employee ID", SwingConstants.LEFT);
inst1.add(jLabel3);
jLabel3.setBounds(12, 15, 103, 26);
jLabel3.setVisible(true);
jTextField2 = new JTextField();
inst1.add(jTextField2);
jTextField2.setVisible(true);
jTextField2.setBounds(109, 15, 93, 30);
jTextPane1 = new JTextPane();
inst1.add(jTextPane1);
jTextPane1.setBounds(6, 50, 307, 50);
jButton4 = new JButton("Search");
inst1.add(jButton4);
jButton4.setVisible(true);
jButton4.setBounds(231, 15, 82, 29);
//jButton4.addActionListener(search);
item++;
return inst1;
}
public JTextField getQueryJTextField(){
return jTextField2;
}
public String getPanelState(){
return state;
}
public int getTabIndex(){
return tabIndex;
}
}
此外,下面的代碼都與「連接」按鈕,它調用createPanel ()方法TabbedPane類別:
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("Connect");
jButton2.setBounds(229, 50, 90, 29);
jButton2.setVisible(false);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
LinkedFrame inst2 = new LinkedFrame(inst, true);
inst2.setLocationRelativeTo(rootPane);
inst2.setVisible(true);
if (Connector.getProcessStatue()) {
TabbedPane inst= new TabbedPane();
jPanel3.setVisible(true);
tabbedPane1.addTab(connectionItem,null ,info.get(inst.getTabIndex()));
info.add(inst.CreatePanel());
System.out.println(inst.getPanelState());
jButton2.setVisible(false);
jButton3.setVisible(true);
setSize(342, 265);
}
}
});
}
請讓我知道我的策略是否屬實?
您能否澄清一下這個問題與您之前關於此主題的不同之處[如何動態創建多個選項卡?](http://stackoverflow.com/questions/11740048/how-to-create-several-tabs-動態)請編輯您的問題,以包含一個[sscce](http://sscce.org/),以顯示問題。 – trashgod 2012-08-02 23:44:44
在我以前的問題中,我正在尋找一種解決方案在我的應用程序中創建多個選項卡....它已解決,我已將它們放入我的APP中,但現在當有多個選項卡時,無法到達面板中的文本框。 .. :( – msc87 2012-08-02 23:58:39