我是一名初学程序员,使用JTabbedPane创建应用程序,默认情况下只有一个选项卡。如果用户点击了"添加标签"按钮,给出一个新选项卡。新选项卡有一些jtextfields和comboboxes。我想从新标签的组件(jtextfields,combobes)中获取文本,但我不知道如何访问它。 (这样我就可以使用" .getText()"函数)
另外,你们可以给我一个提示:将所有标签中的文字保存到格式化的" .txt"是最快/最好的方法。没有硬编码的文件?
提前致谢,如果您需要更多信息,我会发布:)
添加新标签的代码(单击按钮后,新的JPanel表单添加了一些jtextfields):
private void dodajOsebo_buttonActionPerformed(java.awt.event.ActionEvent evt) {
int tabNum = OsebniPodatki_TabbedPane.getTabCount() + 1;
OsebniPodatki_TabbedPane.addTab("Oseba " + tabNum, new PodatkiOStranki());
PodatkiOPotovanju_TabbedPane.addTab("Oseba " + tabNum, new PodrobnostiPotovanja());
}
其中一个自定义JPanel表单的代码:
package FRI;
public class PodatkiOStranki extends javax.swing.JPanel {
metode m = new metode();
public PodatkiOStranki() {
initComponents();
}
private void initComponents() {
spol_buttonGroup = new javax.swing.ButtonGroup();
podatki_LABEL = new javax.swing.JLabel();
Ime_op = new javax.swing.JLabel();
Priimek_op = new javax.swing.JLabel();
Spol = new javax.swing.JLabel();
PotniList_op = new javax.swing.JLabel();
Naslov_op = new javax.swing.JLabel();
kraj_op = new javax.swing.JLabel();
elPosta_op = new javax.swing.JLabel();
ime_field = new javax.swing.JTextField();
priimek_field = new javax.swing.JTextField();
spol_m = new javax.swing.JRadioButton();
spol_z = new javax.swing.JRadioButton();
potniList_field = new javax.swing.JTextField();
naslov_field = new javax.swing.JTextField();
kraj_field = new javax.swing.JTextField();
elPosta_field = new javax.swing.JTextField();
ponastaviVnos_button = new javax.swing.JButton();
setMaximumSize(new java.awt.Dimension(400, 600));
setMinimumSize(new java.awt.Dimension(375, 562));
setPreferredSize(new java.awt.Dimension(375, 562));
podatki_LABEL.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
podatki_LABEL.setText("Podatki o stranki");
Ime_op.setText("Ime:");
Priimek_op.setText("Priimek:");
Spol.setText("Spol:");
PotniList_op.setText("Št. potnega lista:");
Naslov_op.setText("Naslov:");
kraj_op.setText("Kraj & poštna št:");
elPosta_op.setText("E-pošta:");
ime_field.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
ime_fieldKeyTyped(evt);
}
});
priimek_field.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
priimek_fieldKeyTyped(evt);
}
});
spol_buttonGroup.add(spol_m);
spol_m.setText("Moški");
spol_buttonGroup.add(spol_z);
spol_z.setText("Ženska");
kraj_field.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
kraj_fieldKeyTyped(evt);
}
});
ponastaviVnos_button.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
ponastaviVnos_button.setText("Ponastavi vnos");
ponastaviVnos_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ponastaviVnos_buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(podatki_LABEL)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Priimek_op)
.addComponent(PotniList_op)
.addComponent(Naslov_op)
.addComponent(Ime_op)
.addComponent(elPosta_op)
.addComponent(kraj_op)
.addComponent(Spol))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(priimek_field)
.addComponent(naslov_field)
.addComponent(potniList_field)
.addComponent(kraj_field)
.addGroup(layout.createSequentialGroup()
.addComponent(spol_m)
.addGap(18, 18, 18)
.addComponent(spol_z))
.addComponent(ime_field, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
.addComponent(elPosta_field, javax.swing.GroupLayout.Alignment.TRAILING))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(107, 107, 107)
.addComponent(ponastaviVnos_button, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(podatki_LABEL)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Ime_op)
.addComponent(ime_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Priimek_op)
.addComponent(priimek_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Spol)
.addComponent(spol_m)
.addComponent(spol_z))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(potniList_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PotniList_op))
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(naslov_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Naslov_op))
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(kraj_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(kraj_op))
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(elPosta_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(elPosta_op))
.addGap(75, 75, 75)
.addComponent(ponastaviVnos_button, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(183, Short.MAX_VALUE))
);
}// </editor-fold>
private void ime_fieldKeyTyped(java.awt.event.KeyEvent evt) {
m.preveriVnos(ime_field, evt);
}
private void priimek_fieldKeyTyped(java.awt.event.KeyEvent evt) {
m.preveriVnos(priimek_field, evt);
}
private void kraj_fieldKeyTyped(java.awt.event.KeyEvent evt) {
m.preveriVnos(kraj_field, evt);
}
private void ponastaviVnos_buttonActionPerformed(java.awt.event.ActionEvent evt) {
m.ponastaviVnosPodatkiOStranki(ime_field, priimek_field, potniList_field, naslov_field, elPosta_field, kraj_field, spol_buttonGroup);
}
// Variables declaration - do not modify
private javax.swing.JLabel Ime_op;
private javax.swing.JLabel Naslov_op;
private javax.swing.JLabel PotniList_op;
private javax.swing.JLabel Priimek_op;
private javax.swing.JLabel Spol;
private javax.swing.JTextField elPosta_field;
private javax.swing.JLabel elPosta_op;
private javax.swing.JTextField ime_field;
private javax.swing.JTextField kraj_field;
private javax.swing.JLabel kraj_op;
private javax.swing.JTextField naslov_field;
private javax.swing.JLabel podatki_LABEL;
private javax.swing.JButton ponastaviVnos_button;
private javax.swing.JTextField potniList_field;
private javax.swing.JTextField priimek_field;
private javax.swing.ButtonGroup spol_buttonGroup;
private javax.swing.JRadioButton spol_m;
private javax.swing.JRadioButton spol_z;
// End of variables declaration
}
答案 0 :(得分:0)
这很大程度上取决于您如何声明和使用变量。
对于第二部分,如果您有对该选项卡的引用,则可以遍历其子组件(已经很长,但我认为该方法可能是getComponents())并检查每个是否为JTextField类型。
根据您的自定义jpanels:
您可能会在那里定义一个方法:
public void getContent(){ String text = naslov_field.getText(); text + = kraj_field.getText(); }
而不是
private void dodajOsebo_buttonActionPerformed(java.awt.event.ActionEvent evt){
int tabNum = OsebniPodatki_TabbedPane.getTabCount() + 1;
OsebniPodatki_TabbedPane.addTab("Oseba " + tabNum, new PodatkiOStranki());
PodatkiOPotovanju_TabbedPane.addTab("Oseba " + tabNum, new PodrobnostiPotovanja());
}
将jpanels声明为变量。
//variable decl
PodrobnostiPotovanja currentAddePodronPanel;
PodatkiOStranki currentAddedPodatPanel;
private void dodajOsebo_buttonActionPerformed(java.awt.event.ActionEvent evt) {
int tabNum = OsebniPodatki_TabbedPane.getTabCount() + 1;
currentAddedPodatPanel = new PodatkiOStranki();
currentAddePodronPanel = new PodrobnostiPotovanja();
OsebniPodatki_TabbedPane.addTab("Oseba " + tabNum, currentAddedPodatPanel);
PodatkiOPotovanju_TabbedPane.addTab("Oseba " + tabNum, currentAddePodronPanel);
}
假设您有一个从jpanel获取文本的按钮:
public void saveText(ActionEvent event){
String content = currentAddedPodatPanel.getContent();
//etc
}
关于格式化的注意事项:从jtextfield开始,你必须自己做。否则看看jtextarea与文档(有点乏味)或JTextPane或JEditorPane The really big index