SimpleWindow(){super("Пробное окно");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Box box = Box.createVerticalBox();
box.add(new JButton("Кнопка"));
box.add(Box.createVerticalStrut(10));
box.add(new JButton("+"));
box.add(Box.createVerticalGlue());
box.add(new JButton("-"));
box.add(Box.createVerticalStrut(10));
box.add(new JButton("Кнопка с длинной надписью"));
setContentPane(box);setSize(250, 100);}
Elementlarni tekislash xususiyatlari
Vertikal panel misolida barcha tugmalar chap tomonga tekislangan. Ushbu gorizontal hizalama sukut bo'yicha qabul qilinadi.
Biroq, dastur oynasini ishlab chiqishda ba'zi elementlarning, masalan, o'ng chekka yoki markaz bo'ylab boshqacha hizalanishi kerak bo'lishi mumkin. Har qanday vizual komponentning (masalan, tugma yoki panel) hizalanishini o'rnatish uchun setAlignmentX(float alignment) — gorizontal hizalama va setalignmenty(float alignment) — vertikal hizalama usullari qo'llaniladi. Parametr sifatida JComponent sinfida belgilangan doimiylardan foydalanish eng oson. Gorizontal tekislash uchun left_alignment (chap tomonda), RIGHT_ALIGNMENT (o'ng tomonda) va CENTER_ALIGNMENT (markazda) konstantalari ishlatiladi. Vertikal tekislash uchun-BOTTOM_ALIGNMENT (pastki chetida), TOP_ALIGNMENT (yuqori chetida) va CENTER_ALIGNMENT (markazda).
6-rasm
Biroq, hizalama kutilganidan biroz boshqacha ishlaydi. Buni aniqlash uchun uchinchi tugmani o'ng chetiga qo'yib, oldingi misolni o'zgartiring. Buning uchun qatorni almashtiring:
box.add(new JButton("-"));
Qolgan uchtasi uchun:
JButton rightButton = new JButton("-");
rightButton.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
Do'stlaringiz bilan baham: |