简体   繁体   English

给JPanel定制宽度和高度

[英]Give JPanel customized width and height

Why is my JPanel spreading all over the remaining part of frame when i try to resize to whatever size i want it to occupy. 当我尝试将大小调整为我想要占用的任何大小时,为什么我的JPanel会在框架的其余部分中扩展。 . .

This is the code snippet 这是代码片段

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() which returns the panel i am display in my frame. 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