设置包含JRadioButton的JPanel的大小

时间:2014-05-23 21:52:52

标签: java swing jframe jpanel jradiobutton

我有一些问题,我希望我的jpanel看起来像这样 enter image description here 但在编码之后,我有了这个观点,我没有找到解决方案, enter image description here 我需要一些帮助。

我将单选按钮放在jScrollPan中 // listmateriel是单选按钮的列表(向量)---------- // imagebox是jpanel包含jradio按钮和图像---------

// gridlayoutPanel2是Jpanel包含两个文本字段和jlabel以及一个JComboBox

package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......


public class Frame extends JFrame implements ActionListener  {
    private static final int  Haut = 550;
    private static final int Larg = 720;
    private final JPanel utilisateur;
    private final JButton recherche;
    private final JRadioButton radioButton,radioButton2;
    private Vector listmateriel;
    private final JLabel label;

    public Frame()
    {
         super("Gestion des materiels");
         setSize(Larg,Haut);
         setResizable(false);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JTabbedPane menu = new JTabbedPane();








          JPanel emprunt = new JPanel();
         emprunt.setPreferredSize(new Dimension(Larg, Haut ));
         emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
         emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));


         JPanel emp1 = new JPanel();
         emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
         emp1.setLayout(new BorderLayout());


         JLabel title2 = new JLabel("Nouvel emprunt:");
         title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));


         JPanel box2 = new JPanel();
         box2.setPreferredSize(new Dimension(Larg, 40 ));
         box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
         box2.add(title2);
         box2.add(Box.createHorizontalStrut(650));
         emprunt.add(box2);



         JLabel label3 = new JLabel("test");
         JButton dispo = new JButton("Disponibilité");
         dispo.addActionListener((ActionListener) this);
         dispo.setPreferredSize(new Dimension(200, 30));

         JPanel box1 = new JPanel();
         box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
         box1.add(dispo);
         box1.add(Box.createHorizontalStrut(450));
         box1.add(label3);
         emp1.add(box1,BorderLayout.SOUTH);




        listmateriel = new Vector();
         for(int k =0 ;k <10 ; k++)
              listmateriel .add(new JRadioButton(res.getString("nom"+k)));


       ......

        ImageIcon newIcon = new ImageIcon(bi);
        JLabel picture = new JLabel(newIcon);
        picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));


        JPanel jplRadio = new JPanel();
        jplRadio.setLayout(new GridLayout(0, 1));

        for(int i = 0 ; i < listmateriel.size() ;i++)
        {  
            jplRadio.add((Component) listmateriel.elementAt(i));

          }



        JPanel imagebox = new JPanel();
        imagebox.setLayout(new BorderLayout());
        JScrollPane scroll3 = new JScrollPane(jplRadio);
         scroll3.setPreferredSize(new Dimension(200, 30));

        imagebox.add(scroll3, BorderLayout.WEST);
        imagebox.add(picture, BorderLayout.CENTER);
        emp1.add(imagebox,BorderLayout.CENTER);

         JPanel box3 = new JPanel();
         box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
         JLabel title3 = new JLabel("Remplir les champs suivants:");
         box3.add(title3);
         box3.add(Box.createHorizontalStrut(650));



         JPanel gridlayoutPanel2 = new JPanel();

            ........

             JPanel util5 = new JPanel();
            util5.setLayout(new GridLayout(0,1));
            util5.setBorder(BorderFactory.createLineBorder(Color.black));

           JLabel resul2 = new JLabel("bsfgbsdg");      
           JScrollPane scroll2 = new JScrollPane();

           util5.add(resul2);

         emprunt.add(emp1);
         emprunt.add(box3);
         emprunt.add(gridlayoutPanel2);
         emprunt.add(util5);


         menu.add("emprunts",emprunt);

         //---------------------
         getContentPane().add(menu);
         pack();
         setVisible(true);

    }


}

1 个答案:

答案 0 :(得分:2)

GridLayout(0,1)而不是BoxLayout是解决方案谢谢@trashgod