我在java swing中創建了一個應用程序。
該應用程序是一個JFrame
與JTabbedPane
與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;
}
}