2012-12-28 86 views
1

我在java swing中創建了一個應用程序。
該應用程序是一個JFrameJTabbedPane與3 tabbedpanes。如何防止重定向到JTabbedPane中選定的選項卡?

在第一窗格有一個進度複選框,如果該複選框選擇然後點擊到另一窗格i'e第二或第三窗格會得到一個ERROR消息表示「選項卡式正在進行1」。

的問題是,當我選擇了進度複選框當點擊到其他窗格,它首先會去,我選擇的則僅重定向的作品也被執行了兩次錯誤消息窗格。

我已經阻止錯誤消息的執行兩次莫名其妙,但不能阻止去點擊窗格中單擊進度複選框時。

誰能告訴我該問題的一些解決方案。

我的完整代碼如下。

import javax.swing.JOptionPane; 

public class sample extends javax.swing.JFrame { 
public static int status=0; 
    public sample() { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     tab = new javax.swing.JTabbedPane(); 
     jDesktopPane2 = new javax.swing.JDesktopPane(); 
     jLabel1 = new javax.swing.JLabel(); 
     progress = new javax.swing.JCheckBox(); 
     jDesktopPane4 = new javax.swing.JDesktopPane(); 
     jLabel2 = new javax.swing.JLabel(); 
     jDesktopPane5 = new javax.swing.JDesktopPane(); 
     jLabel3 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     tab.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT); 
     tab.setTabPlacement(javax.swing.JTabbedPane.LEFT); 
     tab.addChangeListener(new javax.swing.event.ChangeListener() { 
      public void stateChanged(javax.swing.event.ChangeEvent evt) { 
       tabStateChanged(evt); 
      } 
     }); 

     jDesktopPane2.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background")); 

     jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
     jLabel1.setText("TABBED PANE 1 BODY "); 
     jLabel1.setBounds(50, 60, 230, 40); 
     jDesktopPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER); 

     progress.setText("Progress"); 
     progress.addItemListener(new java.awt.event.ItemListener() { 
      public void itemStateChanged(java.awt.event.ItemEvent evt) { 
       progressItemStateChanged(evt); 
      } 
     }); 
     progress.setBounds(50, 40, 90, 23); 
     jDesktopPane2.add(progress, javax.swing.JLayeredPane.DEFAULT_LAYER); 

     tab.addTab("Tabbed Pane 1", jDesktopPane2); 

     jDesktopPane4.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background")); 

     jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
     jLabel2.setText("TABBED PANE 2 BODY "); 
     jLabel2.setBounds(30, 90, 230, 40); 
     jDesktopPane4.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER); 

     tab.addTab("Tabbed Pane 2", jDesktopPane4); 

     jDesktopPane5.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background")); 

     jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
     jLabel3.setText("TABBED PANE 3 BODY "); 
     jLabel3.setBounds(60, 160, 230, 40); 
     jDesktopPane5.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER); 

     tab.addTab("Tabbed Pane 3", jDesktopPane5); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(tab, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(tab, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void progressItemStateChanged(java.awt.event.ItemEvent evt) { 
     if(progress.isSelected()) 
      setStatus(1); 
     else 
      setStatus(0); 
    } 

    private void tabStateChanged(javax.swing.event.ChangeEvent evt) { 
     int o=getStatus(); 
     if(o==1) 
     { 
     JOptionPane.showMessageDialog(null, "Tabbed 1 Progress!!!", "Confirmation!!!",JOptionPane.ERROR_MESSAGE); 
     setStatus(0); 
     tab.setSelectedIndex(0); 
     setStatus(1); 
     } 
    } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       sample s= new sample(); 
       s.setLocationRelativeTo(null); 
       s.setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JDesktopPane jDesktopPane2; 
    private javax.swing.JDesktopPane jDesktopPane4; 
    private javax.swing.JDesktopPane jDesktopPane5; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JCheckBox progress; 
    private javax.swing.JTabbedPane tab; 
    // End of variables declaration 

    public static void setStatus(int i) { 
     status=i; 
    } 
    public static int getStatus() 
    { 
     return status; 
    } 
} 

回答

2

那是因爲你使用tabStateChanged事件,所以只有當標籤被改變的情況下將被觸發,這樣你就可以改變事件類型點擊或更改代碼,以便它不顯示錯誤第一,但首先改變選擇的選項卡,然後顯示錯誤框

int o=getStatus(); 
     if(o==1 && tab.getSelectedIndex() != 0) //if the selected index isn't 0 and the o == 1 
     { 
      tab.setSelectedIndex(0); 
      JOptionPane.showMessageDialog(null, "Tabbed 1 Progress!!!", "Confirmation!!!",JOptionPane.ERROR_MESSAGE); 
      //setStatus(0); delete this 
      //setStatus(1); delete this 
     } 
2

不能使用的JTabbedPane的方法是什麼?

public void setEnabledAt(int index, boolean enabled) 
的進展

開始,以防止對殘疾人的標籤

點擊
相關問題