我是java的新手,需要一些帮助。这就是说我确信我的代码很草率。我正在使用NetBeans作为GUI构建器,并且正在学习这个过程。我在这里有一个SSCC,但因为我使用NetBeans的时间相当长。
public class tabtest extends javax.swing.JFrame {
public tabtest() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
addTabDialog = new javax.swing.JDialog();
titleTextBox = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
addTabConfirmButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
tabPane = new javax.swing.JTabbedPane();
addTabButton = new javax.swing.JButton();
addTabDialog.setBounds(new java.awt.Rectangle(0, 0, 400, 400));
titleTextBox.setText("New Tab");
jLabel1.setText("Tab Title");
addTabConfirmButton.setText("Confirm");
addTabConfirmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addTabConfirmButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout addTabDialogLayout = new javax.swing.GroupLayout(addTabDialog.getContentPane());
addTabDialog.getContentPane().setLayout(addTabDialogLayout);
addTabDialogLayout.setHorizontalGroup(
addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(addTabDialogLayout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addTabConfirmButton)
.addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(211, Short.MAX_VALUE))
);
addTabDialogLayout.setVerticalGroup(
addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(addTabDialogLayout.createSequentialGroup()
.addGap(77, 77, 77)
.addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
.addComponent(addTabConfirmButton)
.addGap(84, 84, 84))
);
jLabel2.setText("This is a new Tab");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addTabButton.setText("add new tab");
addTabButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addTabButtonActionPerformed(evt);
}
});
tabPane.addTab("+", addTabButton);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(34, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(33, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void addTabButtonActionPerformed(java.awt.event.ActionEvent evt) {
addTabDialog.setLocationRelativeTo(null);
addTabDialog.setVisible(true);
}
private void addTabConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {
tabPane.addTab(titleTextBox.getText(),jLabel2);
tabPane.setSelectedIndex(tabPane.getTabCount()-1);
addTabDialog.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tabtest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addTabButton;
private javax.swing.JButton addTabConfirmButton;
private javax.swing.JDialog addTabDialog;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTabbedPane tabPane;
private javax.swing.JTextField titleTextBox;
// End of variables declaration
我需要帮助的主要部分是这部分,其余部分只是构建GUI
private void addTabButtonActionPerformed(java.awt.event.ActionEvent evt){
addTabDialog.setLocationRelativeTo(null);
addTabDialog.setVisible(true);
}
private void addTabConfirmButtonActionPerformed(java.awt.event.ActionEvent evt){
tabPane.addTab(titleTextBox.getText(),jLabel2);
tabPane.setSelectedIndex(tabPane.getTabCount()-1);
addTabDialog.setVisible(false);
}
对不起代码墙...... 无论如何,如果您运行此代码,您将看到在尝试添加2个选项卡后,第二个选项卡将替换您尝试添加的第一个选项卡。我究竟做错了什么?我确信这很简单,但任何帮助都会非常感激!谢谢!
答案 0 :(得分:0)
通过再次添加包含该组件的新选项卡,您实际上并未添加新的选项卡对象。如果您将代码更改为如此
private void addTabConfirmButtonActionPerformed(
java.awt.event.ActionEvent evt) {
tabPane.addTab(titleTextBox.getText(), new JLabel("This is my new tab"));
tabPane.setSelectedIndex(tabPane.getTabCount() - 1);
addTabDialog.setVisible(false);
}
它应该按预期工作。
原因是,第二个参数Component是您要显示的内容。因为您不断添加相同的组件(读取:不是组件的COPY),所以库只会将输入的新标题与ONE JLabel相关联,并绘制TabbedPane以反映这一点。