1

我在 java swing 中创建了一个应用程序。
该应用程序是一个JFrame带有JTabbedPane3 个选项卡式窗格的应用程序。

在第一个窗格中有一个进度复选框,如果选中该复选框,则单击另一个窗格,即第二个或第三个窗格将收到一条ERROR消息,显示“Tabbed 1 in progress”。

问题是,当我在选中进度复选框时单击其他窗格时,它将首先进入我选择的窗格,然后只有重定向起作用,并且错误消息也被执行两次。

我已经以某种方式阻止了错误消息的两次执行,但是在单击进度复选框时无法阻止转到单击的窗格。

谁能告诉我这个问题的一些解决方案。

我的完整代码如下。

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;
    }
}
4

2 回答 2

2

不能用的方法JTabbedPane吗?

public void setEnabledAt(int index, boolean enabled)

关于进度开始以防止单击禁用的选项卡

于 2012-12-28T07:40:27.283 回答
2

这是因为您使用了 tabStateChanged 事件,所以只有在更改选项卡时才会触发该事件,因此您可以将事件类型更改为 clicked 或更改代码使其不首先显示错误,而是更改 selected先 Tab 然后显示错误框

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
       }
于 2012-12-28T07:32:26.003 回答