简体   繁体   English

如何在jbutton上的另一个下面添加jlabel

[英]how to add jlabel one below the other on jbutton click

I wanted to add jlabel dynamically on button click right below the other. 我想在其他按钮右下方单击动态添加jlabel。 I am not able to figure out the layout to be used in order to add jlabel. 我无法弄清楚要用来添加jlabel的布局。 Any hint or solution will be appreciated 任何提示或解决方案将不胜感激

Code goes here 代码在这里

    package com.vastu;

import javax.swing.JPanel;

public class spanel extends JPanel {

    String[] nakshatras = {"SELECT","Ashwini","Bharani","Kritika","Rohini","Mrugashira","Aardra","Punarvasu","Pushya","Aashlesha","Magha","Poorva","Phalguni","Uttara","Phalguni","Hasta","Chitra","Swati","Vishakha","Anuradha","Jyeshta","Moola","Poorvashada","Uttarashada","Shravana","Dhanishta","Shatabhisha","Poorvabhadra","Uttarabhadra","Revati"};
    String[] more={"MORE MEMBERS","FATHER","MOTHER","HUSBAND","WIFE","SON","DAUGHTER"};
    public spanel() {
        setBackground(new Color(147, 112, 219));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JLabel lblTarabala = new JLabel("tarabala:");
        add(lblTarabala);

        JLabel lblStarOfOwner = new JLabel("STAR OF OWNER  :");
        add(lblStarOfOwner);

        JComboBox comboBox = new JComboBox(nakshatras);
        add(comboBox);

        JComboBox comboBox_1 = new JComboBox(more);
        add(comboBox_1);

        comboBox_1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JComboBox jc = (JComboBox) e.getSource();
                Integer i=jc.getSelectedIndex();
                if(i==0){
                    repaint();
                }else if(i==1){
                    JLabel newmember=new JLabel();
                    JComboBox star_f=new JComboBox(nakshatras);
                    GridBagConstraints gbc_starf = new GridBagConstraints();
                    star_f.setBackground(new Color(211, 211, 211));
                    newmember.setFont(new Font("Times New Roman", Font.BOLD, 14));
                    newmember.setText("STAR OF FATHER  :");
                    newmember.setBackground(new Color(147, 112, 219));
                    gbc_starf.gridwidth=5;
                    add(newmember);
                    add(star_f);
                    revalidate();
                }else if(i==2){
                    JLabel newmember=new JLabel();
                    JComboBox star_m=new JComboBox(nakshatras);
                    GridBagConstraints gbc_starm = new GridBagConstraints();
                    star_m.setBackground(new Color(211, 211, 211));
                    newmember.setFont(new Font("Times New Roman", Font.BOLD, 14));
                    newmember.setText("STAR OF MOTHER  :");
                    newmember.setBackground(new Color(147, 112, 219));
                    gbc_starm.gridwidth=5;
                    add(newmember);
                    add(star_m);
                    revalidate();
                }else if(i==3){
                    JLabel newmember=new JLabel();
                    JComboBox star_h=new JComboBox(nakshatras);
                    GridBagConstraints gbc_starh = new GridBagConstraints();
                    star_h.setBackground(new Color(211, 211, 211));
                    newmember.setFont(new Font("Times New Roman", Font.BOLD, 14));
                    newmember.setText("STAR OF HUSBAND  :");
                    newmember.setBackground(new Color(147, 112, 219));
                    gbc_starh.gridwidth=5;
                    add(newmember);
                    add(star_h);
                    revalidate();
                }

            }
        });

    }
}

here i used box layout but the fields appear on full screen. 在这里,我使用了框式布局,但是字段显示在全屏上。 as 在此处输入图片说明


How am i supposed to custom resize component in boxlayout? 我应该如何在boxlayout中自定义调整大小的组件?

You can use GridBagLayout for this purpose, and specify it's fill property to either HORIZONTAL or NONE , if you do not want the JLabel s to resize VERTICALLY or BOTH ways respectively. 您可以为此目的使用GridBagLayout ,并且如果您不希望JLabel分别以VERTICALLY or BOTH方式调整大小,则可以将GridBagLayoutfill属性指定为HORIZONTAL or NONE One simplest example for your help : 一个最简单的示例可为您提供帮助:

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

public class DynamicComponentAddition {

    private JFrame frame;
    private JPanel compPanel;
    private JLabel label;
    private JButton button; 

    private GridBagConstraints gbc;

    private int counter;

    public DynamicComponentAddition() {
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.gridx = 0;
        counter = 0;
    }

    private void displayGUI() {
        frame = new JFrame("Dynamic component addition");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout(5, 5));

        compPanel = new JPanel(new GridBagLayout());
        button = new JButton("Add Component");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                final JLabel label = new JLabel(
                        Integer.toString(counter), JLabel.CENTER);
                addComp(compPanel, label, counter++);
            }
        });

        contentPane.add(compPanel, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp, int gridy) {
        gbc.gridy = gridy;      
        panel.add(comp, gbc);
        frame.pack();
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new DynamicComponentAddition().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM