我写了一个应用程序,它连接到多个数据库(检查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