为JPanel定制宽度和高度

时间:2013-10-20 09:37:33

标签: java swing jpanel layout-manager

为什么我的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;

       }



   }


}

1 个答案:

答案 0 :(得分:0)

设置框架的布局:

frame.setLayout(LayoutManager);