2017-04-09 89 views
0

我有一個JTabbedPane一個問題,我有我的JPanel的一個邊界,我假設JTabbedPane都有自己的佈局,使邊境根據佈局JTabbedPane具有基本的擴展,這樣的:是否可以更改JTabbedPane的佈局?

How it turns out.

然後我試圖改變JTabbedPane的邊框,將jTabbedPane.setLayout(new GridBagLayout());,儘管這完全弄亂了起來,原來是這樣的:

After changing the layout.

我一直在試圖使它看起來的方式是這樣的(爲了澄清,我想實現的內部邊界,而不是外部的。):

Possible outcome.

是否有可能實際上改變佈局以適應我想要實現的目標,還是有更好的方法?

任何幫助將不勝感激,請和謝謝。請讓我知道我的問題是否需要改進,或者需要任何其他代碼或澄清。

的代碼:

import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import java.awt.*; 

public class Test extends JFrame 
{ 
    public Test() 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       try 
       { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 

       setLayout(new GridBagLayout()); 

       add(new HomePanel()); 

       pack(); 
       setVisible(true); 
       setLocationRelativeTo(null); 
       setExtendedState(JFrame.MAXIMIZED_BOTH); 
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
     }); 
    } 

    public static void main(String[] args) 
    { 
     Test t = new Test(); 
    } 

    public class HomePanel extends JPanel 
    { 
     protected JTabbedPane tabbedPane; 

     public HomePanel() 
     { 
      tabbedPane = new JTabbedPane(); 
      tabbedPane.setPreferredSize(new Dimension(800, 500)); 

      tabbedPane.addTab("Your Bookings", new ShowBookingPanel()); 

      add(tabbedPane); 

     } 
    } 

    public class ShowBookingPanel extends JPanel 
    { 
     protected JTable bookingTable; 
     protected JScrollPane scrollPane; 


     public ShowBookingPanel() 
     { 
      TitledBorder titledBorder = new TitledBorder("Your Bookings"); 
      titledBorder.setTitleJustification(TitledBorder.CENTER); 
      Border border = new CompoundBorder(titledBorder, new EmptyBorder(40, 50, 40, 50)); 

      setBorder(border); 

      String[] columnNames = {"Check-In Date", "Check-Out Date", "Room Type", "Price", "Action"}; 
      Object[][] data = {{"22/09/1997", "TBA", "Single", "Priceless"}, {"12/03/2017", "23/04/2017", "Double", "£50"}, 
       {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"}, 
       {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"}, 
       {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"}}; 

      TableModel model = new DefaultTableModel(data, columnNames) 
      { 
       public boolean isCellEditable(int row, int column) 
       { 
        return false; 
       } 
      }; 

      bookingTable = new JTable(model); 
      bookingTable.setPreferredScrollableViewportSize(new Dimension(500, 300)); 
      bookingTable.setFillsViewportHeight(true); 
      bookingTable.getTableHeader().setReorderingAllowed(false); 
      bookingTable.getTableHeader().setResizingAllowed(false); 
      bookingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

      scrollPane = new JScrollPane(bookingTable); 

      setLayout(new GridBagLayout()); 

      add(scrollPane); 
     } 
    } 
} 
+0

*「我一直試圖讓它看起來像這樣:」*這可以通過在另一個標題邊框內的空白邊框內使用標題邊框來實現。但有兩個標題完全相同的標題邊界的目的是什麼?它也出現在上面已經將'ShowBookingPanel'的輸入分割爲'HomePanel'源代碼的末尾。爲了儘快提供更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 (只有一個源文件,其中包含一組導入,一個'main'類爲'main'方法,一個或多個默認訪問類。) –

+0

對不起,我只是想要一個標題爲邊界的,我沒有刪除原來的標題那邊的邊界。第二個標題邊界是我要去的那個。這些文件是三個獨立的文件,所以我不知道如果我將三者結合在一起,它是否會起作用。 –

+0

*「第二個標題邊界是我要去的那個。」*第二個是「內部」還是「外部」? *「這些文件是三個獨立的文件,所以我不知道如果我將三者結合在一起,它是否會起作用。」*它會。但是你爲什麼不爲自己嘗試,而不是告訴我你不知道什麼? –

回答

2

變化:

 tabbedPane.addTab("Your Bookings", new ShowBookingPanel()); 

到:

 JPanel centeredPanel = new JPanel(new GridBagLayout()); 
     centeredPanel.add(new ShowBookingPanel()); 

     tabbedPane.addTab("Your Bookings", centeredPanel); 

作爲成分被添加作爲唯一的組分GridBagLayout將只要中心中的部件,不用拉伸它,使用默認約束。