坚持改变面板的尺寸

时间:2016-08-28 13:25:22

标签: java swing jpanel layout-manager

我是Java的新手。所以我在互联网上复制了源代码并将其改为我想要的。

我正在开发一个java的销售点系统,并已经完成了收银系统。

如果有人点击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);
        }



}

2 个答案:

答案 0 :(得分:0)

这是我能提出的最近的布局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

答案 1 :(得分:-2)

有几种不同的方法可以防止Swing组件调整大小。一种方法是使用absolute positioning。这基本上涉及删除任何布局管理(GridLayout,FlowLayout,BorderLayout等)。如果要保留布局管理器但还要禁用组件大小调整,则需要将组件强制为静态大小。请记住,在调整父窗口大小时,这两个解决方案都将禁用任何组件调整大小。

public class TableButton extends JButton {

    private Dimension staticSize;

    public TableButton(String text, Dimension size) {
        super(text);
        staticSize = size;
    }

    @Override
    public Dimension getSize() {
        return staticSize;
    }

    @Override
    public Dimension getMinimumSize() {
        return staticSize;
    }

    @Override 
    public Dimension getMaximumSize() {
        return staticSize;
    }

    @Override 
    public Dimension getPreferredSize() {
        return staticSize;
    }

    @Override
    public int getWidth() {
        return staticSize.width;
    }

    @Override
    public int getHeight() {
        return staticSize.height;
    }
}

Dimension largeTableSize = new Dimension(80,80);
btntable1 = new TableButton("T1", largeTableSize);
btntable2 = new TableButton("T2", largeTableSize);
btntable3 = new TableButton("T3", largeTableSize);
btntable4 = new TableButton("T4", largeTableSize);
btntable5 = new TableButton("T5", largeTableSize);
btntable6 = new TableButton("T6", largeTableSize);