簡體   English   中英

給JPanel定制寬度和高度

[英]Give JPanel customized width and height

當我嘗試將大小調整為我想要占用的任何大小時,為什么我的JPanel會在框架的其余部分中擴展。

這是代碼片段

public class GUITabbedPane extends JFrame{

TabsPanelClasses tabPanel = new TabsPanelClasses();
public GUITabbedPane()
{
    TabsPanelClasses.CreateAccount createAccount = tabPanel.new  CreateAccount();
    TabsPanelClasses.BorrowBook borrowBook = tabPanel.new  BorrowBook();

    JTabbedPane tabs = new JTabbedPane(JTabbedPane.LEFT);

    //Tab text support html tags
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Create Account</body></html>", null,createAccount.createAccountPanel(),"New Member Account");
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Borrow Book</body></html>", null,borrowBook.borrowBookInputs(),"Borrowing a book");
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Return Book</body></html>", null,null,"Returning a book");
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Add Book</body></html>", null,null,"Adding a book record");
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Delete Book</body></html>", null,null,"Deleting a book record");
    tabs.addTab("<html><body marginwidth=30 marginheight=20>Display Details</body></html>", null,null,"Books and Members details");

    add(tabs);
    pack();

}
public static void main(String[] args) {
    GUITabbedPane frame = new GUITabbedPane();

    frame.setDefaultLookAndFeelDecorated(true);
    frame.setSize(800, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

createAccount()返回在面板中顯示的面板。

     public class TabsPanelClasses {

 class CreateAccount
   {

       JLabel label = new JLabel("Applicants` name : ");
       JTextField textField = new JTextField("New members` name here");
       JLabel label1 = new JLabel("Subscription fee ");

       JRadioButton radioButton1 = new JRadioButton("$25");
       JRadioButton radioButton2 = new JRadioButton("$15");
       ButtonGroup group = new ButtonGroup();
       JButton btn = new JButton("Create Account");

       public JPanel createAccountPanel()
       {
           JPanel panel = new JPanel(new GridLayout(3,2));

           panel.setBorder(new TitledBorder("Create Account"));
           panel.add(label);
           panel.add(textField);


           panel.add(label1);
           group.add(radioButton1);
           group.add(radioButton2);

           panel.add(radioButton1);
           panel.add(radioButton2);
           panel.add(btn);
           panel.setVisible(false);
           panel.setSize(new Dimension(50,50));
           panel.revalidate();
           panel.repaint();

           return panel;

       }



   }


}

還要設置框架的布局:

frame.setLayout(LayoutManager);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM