2013-08-24 37 views
0

我的Java窗格中有8個選項卡,我需要每個選項卡都有一個退出按鈕,可以關閉該選項卡而不是整個程序。我只能得到一個標籤上的按鈕,並沒有想出如何使它工作。這是我到目前爲止的代碼:如何將一個退出按鈕放在多選項卡窗格的每個選項卡中,這將關閉每個選項卡

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.util.Date; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.SwingConstants; 

public class JavaTabs extends JFrame { 

    public JavaTabs() { 
     super("Course Project "); 

     JTabbedPane tab = new JTabbedPane(); 
     // constructing the first panel 
     JLabel l1 = new JLabel(" ", SwingConstants.CENTER); 
     JPanel p1 = new JPanel(); 
     p1.setBackground(Color.lightGray); 
     p1.add(l1); 
     tab.addTab("General", null, p1, " Panel #1"); 
     JButton exit = new JButton("Exit"); 

     // constructing the second panel 
     JLabel l2 = new JLabel("Change Company Name", SwingConstants.CENTER); 
     JPanel p2 = new JPanel(); 
     p2.setBackground(Color.lightGray); 
     p2.add(l2); 
     tab.addTab("Options", null, p2, " Panel #2"); 

     // constructing the third panel 
     JLabel l3 = new JLabel(" "); 
     JPanel p3 = new JPanel(); 
     p3.setBackground(Color.lightGray); 
     tab.addTab("Customers", null, p3, " Panel #3"); 

     // constructing the fourth panel 
     JLabel l4 = new JLabel(" "); 
     JPanel p4 = new JPanel(); 
     p4.setBackground(Color.lightGray); 
     p4.add(l4); 
     tab.addTab("Contractors", null, p4, " Panel #4"); 

     // constructing the fifth panel 
     JLabel l5 = new JLabel(" "); 
     JPanel p5 = new JPanel(); 
     p5.setBackground(Color.lightGray); 
     p5.add(l5); 
     tab.addTab("Pools", null, p5, " Panel #5"); 

     // constructing the sixth panel 
     JLabel l6 = new JLabel(" "); 
     JPanel p6 = new JPanel(); 
     p6.setBackground(Color.lightGray); 
     p6.add(l6); 
     tab.addTab("Hot Tubs", null, p6, "Panel #6"); 

     // constructing the seventh panel 
     JLabel l7 = new JLabel(" "); 
     JPanel p7 = new JPanel(); 
     p7.setBackground(Color.lightGray); 
     p7.add(l7); 
     tab.addTab("Temp Calc", null, p7, "Panel #7"); 

     // constructing the eighth panel 
     JLabel l8 = new JLabel(" "); 
     JPanel p8 = new JPanel(); 
     p8.setBackground(Color.lightGray); 
     p8.add(l8); 
     tab.addTab("Length Calc", null, p8, "Panel #8"); 

     JButton test = new JButton("Exit"); 
     p1.add(test); 

     // add JTabbedPane to container 
     getContentPane().add(tab); 

     setSize(350, 300); 
     setLocation(300, 250); 
     setVisible(true); 
    } 

    public static void main(String args[]) { 
     { 
      JavaTabs tabs = new JavaTabs(); 
      tabs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
    } 
} 
+1

請編輯您的代碼,以使其格式良好,包括合理使用一致的縮進。閱讀你的代碼越容易理解它和你的問題。 –

+2

查看[Oracle關於選項卡式窗格的教程](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html)。 – Vulcan

+1

['TabComponentsDemo'](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg):「在標籤上演示自定義組件,使用帶有關閉按鈕的標籤窗格。 – trashgod

回答

-1

您可以將監聽器添加到該按鈕,並使用此:

tab.setEnabledAt(INT指數,布爾B);

+0

這樣做有助於_closing_ tab在那裏面...? – kleopatra

相關問題