亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在彼此下方添加 JLabel 序列?

如何在彼此下方添加 JLabel 序列?

拉風的咖菲貓 2021-11-24 15:54:37
我剛剛在向 GUI 添加一系列 JLabel 時遇到了一些問題。例如,我需要在屏幕左側彼此下方顯示 9 個 JLabel。這是我到目前為止得到的:公共肉類面板(){    JLabel label1 = new JLabel("Meat");    JLabel label2 = new JLabel("Bacon");    JLabel label3 = new JLabel("Capicola");    JLabel label4 = new JLabel("Hamburger");    JLabel label5 = new JLabel("Pepperoni");    JLabel label6 = new JLabel("Meatball");    JLabel label7 = new JLabel("Sausage");    JLabel label8 = new JLabel("Chicken");    JLabel label9 = new JLabel("Linguica");    add(label1, BorderLayout.WEST);    add(label2, BorderLayout.WEST);    add(label3, BorderLayout.WEST);    add(label4, BorderLayout.WEST);    add(label5, BorderLayout.WEST);    add(label6, BorderLayout.WEST);    add(label7, BorderLayout.WEST);    add(label8, BorderLayout.WEST);    add(label9, BorderLayout.WEST);    setLayout(new BorderLayout(9,0));}但是,它們只是彼此相鄰顯示。我怎樣才能讓它們在彼此的正下方,就像在列表中一樣?
查看完整描述

2 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

在容器內布置組件開始。

您不會被一個容器/布局困住,您可以將它們組合起來以生成復雜的 UI。

在您的示例中,您嘗試將多個組件添加到WEST容器的位置,但BorderLayout僅支持在其 5 個可用位置中的每個位置管理單個組件。

此外,在大多數情況下,您應該在嘗試將組件添加到容器之前設置布局管理器

以下是使用復合組件/布局生成復雜 UI 的常用方法

http://img1.sycdn.imooc.com//619defde0001160403610304.jpg

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.GridLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;


public class Test {


    public static void main(String[] args) {

        new Test();

    }


    public Test() {

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                JFrame frame = new JFrame();

                // This is just a filler

                frame.add(new JPanel() {

                    @Override

                    public Dimension getPreferredSize() {

                        return new Dimension(200, 200);

                    }

                });

                frame.add(new MenuPane(), BorderLayout.WEST);

                frame.pack();

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

            }

        });

    }


    public class MenuPane extends JPanel {


        public MenuPane() {

            setLayout(new GridLayout(0, 1));

            add(new JLabel("Meat"));

            add(new JLabel("Bacon"));

            add(new JLabel("Capicola"));

            add(new JLabel("Hamburger"));

            add(new JLabel("Pepperoni"));

            add(new JLabel("Meatball"));

            add(new JLabel("Sausage"));

            add(new JLabel("Chicken"));

            add(new JLabel("Linguica"));

        }


    }


}


查看完整回答
反對 回復 2021-11-24
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

您可以使用 BoxLayout 更多信息:https : //docs.oracle.com/javase/tutorial/uiswing/layout/box.html


JPanel panel= new JPanel();

BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.X_AXIS);// from left to rigth  

panel.setLayout(boxlayout); 


JLabel label1 = new JLabel("Meat");

JLabel label2 = new JLabel("Bacon");

JLabel label3 = new JLabel("Capicola");

JLabel label4 = new JLabel("Hamburger");

...

panel.add(label1);

panel.add(label2);

panel.add(label3);

panel.add(label4);

...


frame.add(panel);


查看完整回答
反對 回復 2021-11-24
  • 2 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號