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

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

如何使用 2 個 JPanel 對象對齊 JButtons 以形成基本的 Java GUI

如何使用 2 個 JPanel 對象對齊 JButtons 以形成基本的 Java GUI

子衿沉夜 2022-12-21 12:43:10
我正在嘗試重新創建以下非?;镜?GUI:我的輸出如下:我很難格式化 JButtons。首先,無論我做什么,我都無法讓第二個面板“shapePane”顯示在第一個面板“colorPane”下方,其次,我無法正確地使按鈕的 y 軸變大以使它們更胖外貌。此外,頂部面板“shapePane”似乎隨著窗口大小的調整而動態移動,而第二個“colorPane”無論窗口大小如何調整都保持在固定位置。如果有人可以提供一些幫助,我將不勝感激
查看完整描述

1 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

以下應該讓你開始:


設置標簽的垂直和水平位置,使其顯示在左下角及其所需的寬度。為了獲得更大的布局靈活性,請考慮將其變形為JPanel:


    label1 = new JLabel("current time here");

    label1.setVerticalAlignment(SwingConstants.BOTTOM);

    label1.setHorizontalAlignment(SwingConstants.LEFT);

    label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger

    getContentPane().add(label1, BorderLayout.WEST);

為按鈕窗格使用 GridLayout:


    colorPane = new JPanel();

    colorPane.setLayout(new GridLayout(2, 3));

初始化按鈕并將它們一一添加到網格窗格中:


    redButton = makeButton("Red");

    colorPane.add(redButton);

在哪里makeButton實現了避免重復代碼的方法:


private JButton makeButton(String text) {

    JButton b = new JButton(text);

    b.setHorizontalAlignment(SwingConstants.LEFT);

    b.addActionListener(this);

    b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work

    return b;

}

設置文本區域的列數。它的高度由布局管理器設置:


textArea = new JTextArea(0,20);

getContentPane().add(textArea, BorderLayout.EAST);

把它們放在一起:


import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.SwingConstants;


public class GUI extends JFrame implements ActionListener

{

    private final JButton circleButton, rectangleButton, redButton;

    private final JButton greenButton, blueButton, exitButton;

    private final JTextArea textArea;

    private final JLabel label1;

    private final JPanel colorPane;


    private static final int ROWS = 2, COLS = 3;


    public GUI (String title)

    {

        super(title);


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //set label's vertical and horizontal position so it appears in the bottom left

        //and and its desired width

        //for more layout flexibility consider warping it in a JFrame

        label1 = new JLabel("current time here");

        label1.setVerticalAlignment(SwingConstants.BOTTOM);

        label1.setHorizontalAlignment(SwingConstants.LEFT);

        label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger

        getContentPane().add(label1, BorderLayout.WEST);


        //use a GridLayout for the buttons pane

        colorPane = new JPanel();

        colorPane.setLayout(new GridLayout(ROWS, COLS));

        getContentPane().add(colorPane, BorderLayout.CENTER);//each BorderLayout position can hold ONE component


        redButton = makeButton("Red");

        colorPane.add(redButton);

        greenButton = makeButton("Green");

        colorPane.add(greenButton);

        blueButton = makeButton("Blue");

        colorPane.add(blueButton);

        rectangleButton = makeButton("Rectangle");

        colorPane.add(rectangleButton);

        circleButton = makeButton("Circle");

        colorPane.add(circleButton);

        exitButton = makeButton("Exit");

        colorPane.add(exitButton);


        //set the text area number of columns. Its height is set by the layout manger

        textArea = new JTextArea(0,20);

        getContentPane().add(textArea, BorderLayout.EAST);


        pack();

    }


    private JButton makeButton(String text) {

        JButton b = new JButton(text);

        b.setHorizontalAlignment(SwingConstants.LEFT);

        b.addActionListener(this);

        b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work

        return b;

    }


    @Override

    public void actionPerformed(ActionEvent e) {

        System.out.println(((JButton)e.getSource()).getText()+ " button pressed");

    }


    public static void main(String[] args) {

        new GUI("My Gui").setVisible(true);

    }

}



一個簡單的增強可能是將所有按鈕引用存儲在一個Map:

public class GUI extends JFrame implements ActionListener

{

    private Map <String, JButton> buttons; // a map to hold references to all buttons 

    private final JTextArea textArea;

    private final JLabel label1;

    private final JPanel colorPane;


    private static final int ROWS = 2, COLS = 3;

    private static final String[] BUTTON_LABELS = {"Red","Green", "Blue", "Rectangle", "Circle", "Exit"};


    public GUI (String title)

    {

        super(title);

        buttons = new HashMap<>();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //set label's vertical and horizontal position so it appears in the bottom left

        //and and its desired width

        //for more layout flexibility consider warping it in a JFrame

        label1 = new JLabel("current time here");

        label1.setVerticalAlignment(SwingConstants.BOTTOM);

        label1.setHorizontalAlignment(SwingConstants.LEFT);

        label1.setPreferredSize(new Dimension(200, 0)); //height is set by the layout manger

        getContentPane().add(label1, BorderLayout.WEST);


        //use a GridLayout for the buttons pane

        colorPane = new JPanel();

        colorPane.setLayout(new GridLayout(ROWS, COLS));

        getContentPane().add(colorPane, BorderLayout.CENTER);//each BorderLayout position can hold ONE component


        for(String text : BUTTON_LABELS){

            JButton button = makeButton(text);

            colorPane.add(button);

            buttons.put(text, button);

        }


        //set the text area number of columns. Its height is set by the layout manger

        textArea = new JTextArea(0,20);

        getContentPane().add(textArea, BorderLayout.EAST);


        pack();

    }


    private JButton makeButton(String text) {

        JButton b = new JButton(text);

        b.setHorizontalAlignment(SwingConstants.LEFT);

        b.addActionListener(this);

        b.setPreferredSize(new Dimension(125, 55)); //set preferred and let Layout manager do its work

        return b;

    }


    @Override

    public void actionPerformed(ActionEvent e) {

        System.out.println(((JButton)e.getSource()).getText()+ " button pressed");

    }


    public static void main(String[] args) {

        new GUI("My Gui").setVisible(true);

    }

}

http://img1.sycdn.imooc.com//63a28f130001ebb608780237.jpg

查看完整回答
反對 回復 2022-12-21
  • 1 回答
  • 0 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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