0

我有 4 个 JPanel。在其中一个面板中,我有一个组合框。在组合框中选择“值 A”后,应显示 Panel2。类似地,如果我选择“值 B”,则应选择 Panel3....

虽然应该在这种情况下使用动作监听器。如何在该动作监听器中调用另一个选项卡。

    public class SearchComponent
        {
          ....

    .


        public SearchAddComponent(....)
        {
        panel = addDropDown(panelList(), "panel", gridbag, h6Box);
                panel.addComponentListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {



                              ItemSelectable is = (ItemSelectable)actionEvent.getSource();
                Object name=selectedString(is);

                    }


        });

            }

        public static final Vector<String> panelList(){

                List<String> panelList = new ArrayList<String>();
                panelList.add("A");
                panelList.add("B");
                panelList.add("C");
                panelList.add("D");
                panelList.add("E");
                panelList.add("F);

                Vector<String> panelVector = null;
                Collections.copy(panelVector, panelList);
                return panelVector;
            }



public Object selectedString(ItemSelectable is) {
    Object selected[] = is.getSelectedObjects();
    return ((selected.length == 0) ? "null" : (ComboItem)selected[0]);
  }

        }
4

2 回答 2

2

使用Card Layout. 有关工作示例,请参阅有关如何使用卡片布局的 Swing 教程。

于 2013-06-19T05:10:50.407 回答
1

试试这段代码:

import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CardLayoutExample {
JFrame guiFrame;
CardLayout cards;
JPanel cardPanel;


public static void main(String[] args) {

     //Use the event dispatch thread for Swing components
     EventQueue.invokeLater(new Runnable()
     {

        @Override
         public void run()
         {

             new CardLayoutExample();         
         }
     });

}

public CardLayoutExample()
{ 
    guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("CardLayout Example");
    guiFrame.setSize(400,300);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);
    guiFrame.setLayout(new BorderLayout());

    //creating a border to highlight the JPanel areas
    Border outline = BorderFactory.createLineBorder(Color.black);

    JPanel tabsPanel = new JPanel();
    tabsPanel.setBorder(outline);
    JButton switchCards = new JButton("Switch Card");
    switchCards.setActionCommand("Switch Card");
    switchCards.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            cards.next(cardPanel);
        }
    });
    tabsPanel.add(switchCards);

    guiFrame.add(tabsPanel,BorderLayout.NORTH);


    cards = new CardLayout();
    cardPanel = new JPanel();
    cardPanel.setLayout(cards);
    cards.show(cardPanel, "Fruits");

    JPanel firstCard = new JPanel();
    firstCard.setBackground(Color.GREEN);
    addButton(firstCard, "APPLES");
    addButton(firstCard, "ORANGES");
    addButton(firstCard, "BANANAS");

    JPanel secondCard = new JPanel();
    secondCard.setBackground(Color.BLUE);
    addButton(secondCard, "LEEKS");
    addButton(secondCard, "TOMATOES");
    addButton(secondCard, "PEAS");

    cardPanel.add(firstCard, "Fruits");
    cardPanel.add(secondCard, "Veggies");

    guiFrame.add(tabsPanel,BorderLayout.NORTH);
    guiFrame.add(cardPanel,BorderLayout.CENTER);
    guiFrame.setVisible(true);
}

//All the buttons are following the same pattern
//so create them all in one place.
private void addButton(Container parent, String name)
{
    JButton but = new JButton(name);
    but.setActionCommand(name);
    parent.add(but);
}
}
于 2013-06-19T05:35:27.700 回答