Java Swing 多种组件完美界面实例(代码+截图)

来源:互联网 发布:php测试链接ftp服务器 编辑:程序博客网 时间:2024/06/17 13:55

最近做个稍微复杂点的界面,需要在界面中组件可以动态添加和删除,直接上图效果
这里写图片描述

其中在分类后面的点击“+”按钮,可以动态添加分类,新添加的分类,可以点击后面的“-”按钮删除掉这一行。
对应的代码如下:

/******************************************************************************* * @project: capital-pdm-sj-jszx * @package: com.acconsys.avidm4.ui * @file: CreatPdmPartUI.java * @author: burns * @created: 2017-12-12 * @purpose: *  * @version: 1.0 *  * Revision History at the end of file. *  * Copyright 2017 AcconSys All rights reserved. ******************************************************************************/package com.acconsys.avidm4.ui;import java.awt.BorderLayout;public class CreatePdmPartUI extends JDialog {    /**     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)     */    private static final long serialVersionUID = -279332465989743454L;    private JPanel northPanel;    private JPanel centerPanel;    private JPanel southPanel;    private JScrollPane centerScrollPane;    private GridBagLayout centerLayout;    private GridBagConstraints centerConstraints;    private ArrayList<JComboBox> flList;    public CreatePdmPartUI(JFrame parent) {        super(parent, true);        initData();        initUI();    }    private void initUI() {        BorderLayout mainLayout = new BorderLayout();        getContentPane().setLayout(mainLayout);        northPanel = new JPanel();        northPanel.setPreferredSize(new Dimension(750, 20));        centerPanel = new JPanel();        centerScrollPane = new JScrollPane();        centerScrollPane.setViewportView(centerPanel);        northPanel.setPreferredSize(new Dimension(750, 400));        southPanel = new JPanel();        northPanel.setPreferredSize(new Dimension(750, 100));        /**         * 上部部分内容         */        {            GridLayout northLayout = new GridLayout(3, 2);            northPanel.setLayout(northLayout);            JLabel cpL = new JLabel("产品:");            JTextField cpTF = new JTextField("测试产品");            JLabel lxL = new JLabel("*类型:");            JComboBox lxCB = new JComboBox(new String[] { "--请选择一个类型",                    "Slider", "Spinner", "Widget", "部件" });            JLabel isOrNotCpL = new JLabel("*创建为产品:");            JComboBox isOrNotCpCB = new JComboBox(new String[] { "", "是", "否" });            northPanel.add(cpL);            northPanel.add(cpTF);            northPanel.add(lxL);            northPanel.add(lxCB);            northPanel.add(isOrNotCpL);            northPanel.add(isOrNotCpCB);        }        /**         * 中部部分         */        {            centerPanel.setBorder(BorderFactory.createTitledBorder("部件属性"));            centerLayout = new GridBagLayout();            centerPanel.setLayout(centerLayout);            centerConstraints = new GridBagConstraints();            centerConstraints.fill = GridBagConstraints.BOTH;            // centerConstraints.anchor = GridBagConstraints.WEST;            // centerConstraints.weightx=0;            // centerConstraints.weighty=0;            {                JLabel bhL = new JLabel("*编号:");                JTextField bhTF = new JTextField();                centerPanel.add(bhL);                centerPanel.add(bhTF);                add(centerLayout, bhL, centerConstraints, 0, 0, 1);                add(centerLayout, bhTF, centerConstraints, 1, 0, 0);            }            {                JLabel mcL = new JLabel("*名称:");                JTextField mcTF = new JTextField();                centerPanel.add(mcL);                centerPanel.add(mcTF);                add(centerLayout, mcL, centerConstraints, 0, 0, 1);                add(centerLayout, mcTF, centerConstraints, 1, 0, 0);            }            {                JLabel zpmsL = new JLabel("*装配模式:");                JComboBox zpmsCB = new JComboBox(new String[] { "", "可分",                        "不可分", "组件" });                centerPanel.add(zpmsL);                centerPanel.add(zpmsCB);                add(centerLayout, zpmsL, centerConstraints, 0, 0, 1);                add(centerLayout, zpmsCB, centerConstraints, 1, 0, 0);            }            {                JLabel yL = new JLabel("*源:");                JComboBox yCB = new JComboBox(new String[] { "", "制造", "购买",                        "购买-单一供应源" });                centerPanel.add(yL);                centerPanel.add(yCB);                add(centerLayout, yL, centerConstraints, 0, 0, 1);                add(centerLayout, yCB, centerConstraints, 1, 0, 0);            }            {                JLabel mrzzdmL = new JLabel("*默认追踪代码:");                JComboBox mrzzdmCB = new JComboBox(new String[] { "", "批号",                        "批号/序列号", "序列号", "未追踪" });                centerPanel.add(mrzzdmL);                centerPanel.add(mrzzdmCB);                add(centerLayout, mrzzdmL, centerConstraints, 0, 0, 1);                add(centerLayout, mrzzdmCB, centerConstraints, 1, 0, 0);            }            {                JLabel mrdwL = new JLabel("*默认单位:");                JComboBox mrdwCB = new JComboBox(new String[] { "", "每个",                        "根据需要", "千克", "米", "升", "平方米", "立方米" });                centerPanel.add(mrdwL);                centerPanel.add(mrdwCB);                add(centerLayout, mrdwL, centerConstraints, 0, 0, 1);                add(centerLayout, mrdwCB, centerConstraints, 1, 0, 0);            }            {                JLabel sjbjL = new JLabel("*收集部件:");                JComboBox sjbjCB = new JComboBox(new String[] { "", "是", "否" });                centerPanel.add(sjbjL);                centerPanel.add(sjbjCB);                add(centerLayout, sjbjL, centerConstraints, 0, 0, 1);                add(centerLayout, sjbjCB, centerConstraints, 1, 0, 0);            }            {                JLabel smzqmbL = new JLabel("*生命周期模版:");                JTextField smzqmbTF = new JTextField("(已生成)");                centerPanel.add(smzqmbL);                centerPanel.add(smzqmbTF);                add(centerLayout, smzqmbL, centerConstraints, 0, 0, 1);                add(centerLayout, smzqmbTF, centerConstraints, 1, 0, 0);            }            {                JLabel wzL = new JLabel("*位置:");                JRadioButton zdxzwjjCB = new JRadioButton("自动选择文件夹");                JTextField zdxzwjjTF = new JTextField("/测试产品");                JLabel tempL = new JLabel();                JRadioButton xzwjjCB = new JRadioButton("选择文件夹");                JTextField xzwjjTF = new JTextField("");                JTree wjjTree = new JTree();                centerPanel.add(wzL);                centerPanel.add(zdxzwjjCB);                centerPanel.add(zdxzwjjTF);                centerPanel.add(tempL);                centerPanel.add(xzwjjCB);                centerPanel.add(xzwjjTF);                centerPanel.add(wjjTree);                add(centerLayout, wzL, centerConstraints, 0, 0, 1);                add(centerLayout, zdxzwjjCB, centerConstraints, 0, 0, 1);                add(centerLayout, zdxzwjjTF, centerConstraints, 1, 0, 0);                add(centerLayout, tempL, centerConstraints, 0, 0, 1);                add(centerLayout, xzwjjCB, centerConstraints, 0, 0, 1);                add(centerLayout, xzwjjTF, centerConstraints, 1, 0, 1);                add(centerLayout, wjjTree, centerConstraints, 0, 0, 0);            }            {                JLabel flL = new JLabel("*分类:");                JComboBox flCB = new JComboBox(new String[] { "", "B", "L",                        "S", "W", "C", "F", "T" });                JButton flB = new JButton("+");                flB.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        final JLabel flL = new JLabel("*分类:");                        final JComboBox flCB = new JComboBox(new String[] { "", "B", "L",                                "S", "W", "C", "F", "T" });                        final JButton flB = new JButton("-");                        flB.addActionListener(new ActionListener() {                            public void actionPerformed(ActionEvent e) {                                centerPanel.remove(flL);                                centerPanel.remove(flCB);                                centerPanel.remove(flB);                                flList.remove(flCB);                                System.out.println("-------------------");                                System.out.println("删除组件后,分类可选数量>>>"+flList.size());                                for (JComboBox cb : flList) {                                    System.out.println(cb.toString());                                }                                System.out.println("-------------------");                                centerPanel.validate();//调用容器的validate()方法                                CreatePdmPartUI.this.validate();                                CreatePdmPartUI.this.repaint();                            }                        });                        centerPanel.add(flL);                        centerPanel.add(flCB);                        centerPanel.add(flB);                        flList.add(flCB);                        System.out.println("+++++++++++++++");                        System.out.println("增加组件后,分类可选数量>>>"+flList.size());                        for (JComboBox cb : flList) {                            System.out.println(cb.toString());                        }                        System.out.println("+++++++++++++++");                        add(centerLayout, flL, centerConstraints, 0, 0, 1);                        add(centerLayout, flCB, centerConstraints, 1, 0, 1);                        add(centerLayout, flB, centerConstraints, 0, 0, 0);                        centerPanel.validate();// 调用容器的validate()方法                        CreatePdmPartUI.this.validate();                        CreatePdmPartUI.this.repaint();                    }                });                centerPanel.add(flL);                centerPanel.add(flCB);                centerPanel.add(flB);                flList = new ArrayList<JComboBox>();                flList.add(flCB);                System.out.println("+++++++++++++++");                System.out.println("增加组件后,分类可选数量>>>"+flList.size());                for (JComboBox cb : flList) {                    System.out.println(cb.toString());                }                System.out.println("+++++++++++++++");                add(centerLayout, flL, centerConstraints, 0, 0, 1);                add(centerLayout, flCB, centerConstraints, 1, 0, 1);                add(centerLayout, flB, centerConstraints, 0, 0, 0);            }        }        /**         * 下部内容:按钮。         */        {            GridLayout southLayout = new GridLayout(1, 2);            southPanel.setLayout(southLayout);            JButton createPdmPartB = new JButton("创建部件");            JButton closeB = new JButton("关闭");            closeB.addActionListener(new ActionListener() {                @Override                public void actionPerformed(ActionEvent e) {                    CreatePdmPartUI.this.dispose();                }            });            southPanel.add(createPdmPartB);            southPanel.add(closeB);        }        getContentPane().add(northPanel, BorderLayout.NORTH);        getContentPane().add(centerScrollPane, BorderLayout.CENTER);        getContentPane().add(southPanel, BorderLayout.SOUTH);        /**         * 设置界面大小和居中         */        {            this.setResizable(true);            this.setSize(800, 600);            // 屏幕居中            int windowWidth = this.getWidth(); // 获得窗口宽            int windowHeight = this.getHeight(); // 获得窗口高            Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包            Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸            int screenWidth = screenSize.width; // 获取屏幕的宽            int screenHeight = screenSize.height; // 获取屏幕的高            this.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight                    / 2 - windowHeight / 2);        }    }    /**     *      * @Title: add     * @Description: TODO(添加控件到容器)     * @param @param container     * @param @param c     * @param @param constraints     * @param @param x     * @param @param y     * @param @param w     * @param @param h 设定文件     * @return void 返回类型     * @throws     */    public void add(GridBagLayout layout, Component c,            GridBagConstraints constraints, int weightx, int weighty,            int gridwidth) {        constraints.weightx = weightx;        constraints.weighty = weighty;        constraints.gridwidth = gridwidth;        layout.setConstraints(c, constraints);    }    private void initData() {        // TODO Auto-generated method stub    }    public static void main(String[] args) {        CreatePdmPartUI cpu = new CreatePdmPartUI(null);        cpu.setVisible(true);    }}/******************************************************************************* * <B>Revision History</B><BR> * [type 'revision' and press Alt + / to insert revision block]<BR> *  *  *  * Copyright 2017 AcconSys All rights reserved. ******************************************************************************/

参考链接:
1、

java中怎么重构界面,即:即点另外一个按钮一个界面的一部分会发生变化(重新添加组件)


2、参考的另一个代码运行效果如下
这里写图片描述

代码如下

package com.acconsys.swing.chapter5;import java.awt.*;import javax.swing.*;public class GridBagDemo extends JFrame {    public static void main(String args[]) {        GridBagDemo demo = new GridBagDemo();    }    public GridBagDemo() {        init();        this.setSize(600, 600);        this.setVisible(true);    }    public void init() {        j1 = new JButton("打开");        j2 = new JButton("保存");        j3 = new JButton("另存为");        j4 = new JPanel();        String[] str = { "java笔记", "C#笔记", "HTML5笔记" };        j5 = new JComboBox(str);        j6 = new JTextField();        j7 = new JButton("清空");        j8 = new JList(str);        j9 = new JTextArea();        j9.setBackground(Color.PINK);// 为了看出效果,设置了颜色        GridBagLayout layout = new GridBagLayout();        this.setLayout(layout);        this.add(j1);// 把组件添加进jframe        this.add(j2);        this.add(j3);        this.add(j4);        this.add(j5);        this.add(j6);        this.add(j7);        this.add(j8);        this.add(j9);        GridBagConstraints s = new GridBagConstraints();// 定义一个GridBagConstraints,        // 是用来控制添加进的组件的显示位置        s.fill = GridBagConstraints.BOTH;        // 该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况        // NONE:不调整组件大小。        // HORIZONTAL:加宽组件,使它在水平方向上填满其显示区域,但是不改变高度。        // VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。        // BOTH:使组件完全填满其显示区域。        s.gridwidth = 1;// 该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个        s.weightx = 0;// 该方法设置组件水平的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间        s.weighty = 0;// 该方法设置组件垂直的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间        layout.setConstraints(j1, s);// 设置组件        s.gridwidth = 1;        s.weightx = 0;        s.weighty = 0;        layout.setConstraints(j2, s);        s.gridwidth = 1;        s.weightx = 0;        s.weighty = 0;        layout.setConstraints(j3, s);        s.gridwidth = 0;// 该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个        s.weightx = 0;// 不能为1,j4是占了4个格,并且可以横向拉伸,        // 但是如果为1,后面行的列的格也会跟着拉伸,导致j7所在的列也可以拉伸        // 所以应该是跟着j6进行拉伸        s.weighty = 0;        layout.setConstraints(j4, s);        s.gridwidth = 2;        s.weightx = 0;        s.weighty = 0;        layout.setConstraints(j5, s);        ;        s.gridwidth = 4;        s.weightx = 1;        s.weighty = 0;        layout.setConstraints(j6, s);        ;        s.gridwidth = 0;        s.weightx = 0;        s.weighty = 0;        layout.setConstraints(j7, s);        ;        s.gridwidth = 2;        s.weightx = 0;        s.weighty = 1;        layout.setConstraints(j8, s);        ;        s.gridwidth = 5;        s.weightx = 0;        s.weighty = 1;        layout.setConstraints(j9, s);    }    JButton j1;    JButton j2;    JButton j3;    JPanel j4;    JComboBox j5;    JTextField j6;    JButton j7;    JList j8;    JTextArea j9;}
阅读全文
0 0
原创粉丝点击