java - 坚持改变面板的大小

标签 java swing jpanel layout-manager

我是 Java 新手。所以我在网上复制了源代码,改成了我想要的。

我正在用 Java 开发一个销售点系统,并且已经完成了一个收银机系统。

我想做一个餐 table 布置,如果有人点击 Table1 按钮,他们可以转到收银机页面并继续订购、付款。我还没有开发事件部分,因为我一直在改变面板的大小:(

我尝试使用 setPreferredSize 但失败了。

这是我一直在处理的代码,我将附上我想要制作的屏幕截图。

package table;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;


public class tablearray extends JApplet {

    private JPanel parentPanel, titlePanel, tablePanel, tablefor4Panel, barPanel;
    private JPanel table4andbarPanel, tablefor2Panel, bottomPanel;
    private JLabel lblTitle;
    private JButton btntable1, btntable2, btntable3, btntable4, btntable5, btntable6;
    private JButton btntable7, btntable8, btntable9;
    private JButton btnbar1, btnbar2, btnbar3, btnbar4;
    private ButtonGroup grpTablefor4, grpTablefor2, grpBar;

    /**
     * Constants
     */

    // Applet Size
    private final Dimension appletSize = new Dimension(800, 600);


    // Bottom buttons size
    //private final Dimension BUTTON_SIZE = new Dimension(50,50);            

    // flag, if the user does not enter a number in the dialog
    private final int DIALOG_NOT_NUM = -1;                            

    // flag, if the user cancels the dialog
    private final int DIALOG_CANCEL = -2;                            


     public void init()
        {
            // set applet size
            this.setSize(appletSize);

            /**
            * Create Panels
            */
            parentPanel = new JPanel();
            titlePanel = new JPanel();
            tablePanel = new JPanel();
            tablefor4Panel = new JPanel();
            barPanel = new JPanel();
            table4andbarPanel = new JPanel();
            tablefor2Panel = new JPanel();
            bottomPanel = new JPanel();


            /**
            * Set Panel Layouts
            */

            parentPanel.setLayout(new BorderLayout());
            titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
            tablePanel.setLayout(new GridLayout(1,2,50,50));
            table4andbarPanel.setLayout(new BorderLayout());
            tablefor4Panel.setLayout(new GridLayout(2,3,100,100));
            barPanel.setLayout(new GridLayout(1,4,30,30));
            tablefor2Panel.setLayout(new GridLayout(4,1,100,100));
            bottomPanel.setLayout(new BorderLayout(5,5));


            /**
            * Set Panel Borders
            */
            // Tablefor2's Border
            TitledBorder billBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Tablefor2", TitledBorder.CENTER, TitledBorder.CENTER);
            tablefor2Panel.setBorder(billBorder);

            // Table's Border
            TitledBorder orderBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Table management", TitledBorder.LEFT, TitledBorder.CENTER);
            tablePanel.setBorder(orderBorder);

            // Tablefor4's Border
            TitledBorder mainBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Tablefor4", TitledBorder.LEFT, TitledBorder.CENTER);
            tablefor4Panel.setBorder(mainBorder);

            // Bar's Border
            TitledBorder drinkBorder = new TitledBorder(BorderFactory.createEtchedBorder(),
                    "Bar", TitledBorder.LEFT, TitledBorder.CENTER);
            barPanel.setBorder(drinkBorder);

            /**
            * Create Components
            */
            // Title
            lblTitle = new JLabel("- Tasting Room -");
            lblTitle.setFont (new Font ("Book Antiqua", Font.BOLD, 18));
            lblTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
            titlePanel.add(lblTitle);

            //Tablefor4
            grpTablefor4 = new ButtonGroup();
            btntable1 = new JButton("T1");
            btntable2 = new JButton("T2");
            btntable3 = new JButton("T3");
            btntable4 = new JButton("T4");
            btntable5 = new JButton("T5");
            btntable6 = new JButton("T6");
            btntable1.setPreferredSize(new Dimension(100,100));


            grpTablefor4.add(btntable1);
            grpTablefor4.add(btntable2);
            grpTablefor4.add(btntable3);
            grpTablefor4.add(btntable4);
            grpTablefor4.add(btntable5);
            tablefor4Panel.add(btntable1);
            tablefor4Panel.add(btntable2);
            tablefor4Panel.add(btntable3);
            tablefor4Panel.add(btntable4);
            tablefor4Panel.add(btntable5);
            tablefor4Panel.add(btntable6);



            //tablefor2
            grpTablefor2 = new ButtonGroup();
            btntable7 = new JButton("T7");
            btntable8 = new JButton("T8");
            btntable9 = new JButton("T9");
            btntable7.setPreferredSize(new Dimension(30,30));


            grpTablefor2.add(btntable7);
            grpTablefor2.add(btntable8);
            grpTablefor2.add(btntable9);
            tablefor2Panel.add(btntable7);
            tablefor2Panel.add(btntable8);
            tablefor2Panel.add(btntable9);

            // bar
            grpBar = new ButtonGroup();
            btnbar1 = new JButton("B1");
            btnbar2 = new JButton("B2");
            btnbar3 = new JButton("B3");
            btnbar4 = new JButton("B4");


            grpBar.add(btnbar1);
            grpBar.add(btnbar2);
            grpBar.add(btnbar3);
            grpBar.add(btnbar4);

            barPanel.add(btnbar1);
            barPanel.add(btnbar2);
            barPanel.add(btnbar3);
            barPanel.add(btnbar4);


             /** Add Panels
             */
            table4andbarPanel.add(tablefor4Panel, BorderLayout.NORTH);
            table4andbarPanel.add(barPanel,BorderLayout.CENTER);
            tablePanel.add(table4andbarPanel, 0);
            tablePanel.add(tablefor2Panel, 1);
            parentPanel.add(titlePanel, BorderLayout.NORTH);
            parentPanel.add(tablePanel, BorderLayout.CENTER);
            parentPanel.add(bottomPanel, BorderLayout.SOUTH);
            add(parentPanel);
        }



}

最佳答案

这是我能想到的最接近MigLayout的布局:

package com.zetcode;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

/*
Demonstrating layout in MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutBoxesEx extends JFrame {

    public MigLayoutBoxesEx() {

        initUI();
    }

    private void initUI() {

        JButton btn1 = new JButton("1");
        JButton btn2 = new JButton("2");
        JButton btn3 = new JButton("3");
        JButton btn4 = new JButton("4");
        JButton btn5 = new JButton("5");
        JButton btn6 = new JButton("6");

        JButton btn7 = new JButton("7");
        JButton btn8 = new JButton("8");
        JButton btn9 = new JButton("9");

        JButton btn10 = new JButton("10");
        JButton btn11 = new JButton("11");
        JButton btn12 = new JButton("12");
        JButton btn13 = new JButton("13");

        createLayout(btn1, btn2, btn3, btn4, btn5,
                btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13);

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout("gap 10lp 15lp, ins 15lp"));

        add(arg[0], "w 90lp, h 90lp");
        add(arg[1], "w 90lp, h 90lp");
        add(arg[2], "w 90lp, h 90lp");
        add(arg[6], "w 50lp, h 50lp, growy, split 3, flowy, spany 2");
        add(arg[7], "w 50lp, h 50lp, growy");
        add(arg[8], "w 50lp, h 50lp, growy, wrap");

        add(arg[3], "w 90lp, h 90lp");
        add(arg[4], "w 90lp, h 90lp");
        add(arg[5], "w 90lp, h 90lp, wrap");

        add(arg[9], "w 50lp, h 20lp, growx, split 4, span 3");
        add(arg[10], "w 50lp, h 20lp, growx");
        add(arg[11], "w 50lp, h 20lp, growx");
        add(arg[12], "w 50lp, h 20lp, growx");

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutBoxesEx ex = new MigLayoutBoxesEx();
            ex.setVisible(true);
        });
    }
}

截图:

Example screnshot

关于java - 坚持改变面板的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191742/

相关文章:

java - 在 java 中使用 property.getProperty ("sample.*"从属性文件获取所有属性值

java - AWS - 使用 Java 访问 s3 存储桶中文件夹中的文件

java - JPanel 使用 mouseMoved 方法闪烁

java - 在jPanel上绘制并获取绘制的内容

java - 线程运行时按下按键 - 如何捕获按键?

java - Spring的@Lazy初始化是线程安全的吗?

java - 使用 Retrofit 从 API 返回信息,如何在其他类中使用它?

java - 如何获得文本组件中使用的抗锯齿模式(底层系统/操作系统设置)

java - 不使用 Thread.sleep() 让某些东西等待?

java - QStackedWidget 相当于 Java Swing