님이 Java에 새로운 기능을 제공하며 약간의 도움이 필요합니다. 그건 내 코드가 엉성한 것이라고 확신하고있다. GUI 빌더를 위해 NetBeans를 사용하고 있으며 과정에서 스윙 스윙을하고 있습니다. 여기에 SSCC가 있지만 NetBeans를 사용하고 있기 때문에 상당히 오래되었습니다.탭을 추가하면 탭 (Java)
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
내가 도움이 필요 주요 부분은이 부분은이 코드를 실행하면 나머지는 그냥 어쨌든 코드의 벽 ... 에 대한
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);
}
죄송 GUI를 건설하고있다 2 개의 탭을 추가하려고 시도하면 두 번째 탭이 추가하려고하는 첫 번째 탭을 대체합니다. 내가 도대체 뭘 잘못하고있는 겁니까? 나는 그것의 무언가 간단하다고 확신하지만 어떤 도움도 매우 극명하게 될 것입니다! 고맙습니다!
정말 고마워요. 그렇게 많은 의미가 있습니다. 그래서 매번 (내 특정 프로그램에서 JTabbedPane) 동일한 작업을 표시하려면 addTab 메서드에서 만들어야합니다. 내가 갖고 있고 매번 복사본을 만들면 어떨까요? – Sup3rb0wlz
TabbedPane 내부에는 아마도 String -> Component에 대한 정렬 된 맵이있을 것입니다. 자바 정렬은 객체에 대한 참조를 전달하므로 복제본을 만들거나 새 구성 요소를 인스턴스화하면 별도의 객체가되어 예상대로 작동합니다. 새로운 개체를 인스턴스화하는 것이 더 나은 방법 일 수 있습니다. – Mike