1

我想添加JTextPane对应于JComboBox动态所做的选择并将它们对齐在另一个下方。这里我用过SpringLayout。如果任何其他布局是合适的,请帮助它。我试过如下所示:

代码

    package com.vastu;

import java.awt.Color;

public class Panel1 extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public Panel1() {
        setuppanel();
    }

    @SuppressWarnings("unchecked")
    private void setuppanel(){
        setBackground(new Color(147, 112, 219));
        final SpringLayout springLayout = new SpringLayout();
        setLayout(springLayout);





        JLabel lblTarabala = new JLabel("TARABALA :");
        springLayout.putConstraint(SpringLayout.NORTH, lblTarabala, 11, SpringLayout.NORTH, this);
        springLayout.putConstraint(SpringLayout.WEST, lblTarabala, 36, SpringLayout.WEST, this);
        lblTarabala.setFont(new Font("Times New Roman", Font.BOLD, 18));
        add(lblTarabala);

        final JTextPane txtrNakshatrastarOfYajamana = new JTextPane();
        springLayout.putConstraint(SpringLayout.NORTH, txtrNakshatrastarOfYajamana, 50, SpringLayout.NORTH, this);
        springLayout.putConstraint(SpringLayout.WEST, txtrNakshatrastarOfYajamana, 36, SpringLayout.WEST, this);
        txtrNakshatrastarOfYajamana.setFont(new Font("Times New Roman", Font.BOLD, 14));
        txtrNakshatrastarOfYajamana.setEditable(false);
        txtrNakshatrastarOfYajamana.setBackground(new Color(147, 112, 219));
        txtrNakshatrastarOfYajamana.setText("STAR OF THE OWNER  :");
        add(txtrNakshatrastarOfYajamana);


        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"};
        @SuppressWarnings({ "rawtypes" })
        JComboBox star = new JComboBox(nakshatras);
        springLayout.putConstraint(SpringLayout.NORTH, star, 50, SpringLayout.NORTH, this);
        springLayout.putConstraint(SpringLayout.WEST, star, 210, SpringLayout.WEST, this);
        star.setBackground(new Color(211, 211, 211));
        add(star);

        String[] more={"MORE MEMBERS","FATHER","MOTHER","HUSBAND","WIFE","SON","DAUGHTER"};
        @SuppressWarnings("rawtypes")
        JComboBox members = new JComboBox(more);
        springLayout.putConstraint(SpringLayout.NORTH, members, 50, SpringLayout.NORTH, this);
        springLayout.putConstraint(SpringLayout.WEST, members, 382, SpringLayout.WEST, this);
        add(members);
        members.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                @SuppressWarnings("rawtypes")
                JComboBox jc = (JComboBox) e.getSource();
                Integer i=jc.getSelectedIndex();
                if(i==0){
                    repaint();
                }else if(i==1){
                    JTextPane newmember=new JTextPane();
                    newmember.setFont(new Font("Times New Roman", Font.BOLD, 14));
                    newmember.setEditable(false);
                    newmember.setText("STAR OF FATHER  :");
                    newmember.setBackground(new Color(147, 112, 219));
                    springLayout.putConstraint(SpringLayout.SOUTH, newmember, 35, SpringLayout.SOUTH, txtrNakshatrastarOfYajamana);
                    springLayout.getLayoutAlignmentX(txtrNakshatrastarOfYajamana);
                    add(newmember);
                    revalidate();
                }
            }
        });

    }
}

布局图像

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述


我希望“父亲之星”出现在“所有者之星”窗格的正下方。有人可以提出解决方案吗?

4

0 回答 0