Java选择选项卡

Java选择选项卡,java,swing,jpanel,cardlayout,Java,Swing,Jpanel,Cardlayout,我有4个JPanel。在其中一个面板中,我有一个组合框。在组合框中选择“值a”时,应显示面板2。同样,如果我选择“值B”,应选择面板3 虽然应该在此上下文中使用操作侦听器。如何调用该操作侦听器中的另一个选项卡 public class SearchComponent { .... . public SearchAddComponent(....) { panel = addDropDown(

我有4个JPanel。在其中一个面板中,我有一个组合框。在组合框中选择“值a”时,应显示面板2。同样,如果我选择“值B”,应选择面板3

虽然应该在此上下文中使用操作侦听器。如何调用该操作侦听器中的另一个选项卡

    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]);
  }

        }
公共类搜索组件
{
....
.
公共SearchAddComponent(…)
{
panel=addDropDown(panelList(),“panel”,gridbag,h6Box);
panel.addComponentListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
ItemSelectable为=(ItemSelectable)actionEvent.getSource();
对象名称=selectedString(is);
}
});
}
公共静态最终向量小组成员(){
List panelList=new ArrayList();
小组成员。添加(“A”);
小组成员。添加(“B”);
小组成员。添加(“C”);
小组成员。添加(“D”);
小组成员。添加(“E”);
小组成员。添加(“F”);
向量=零;
集合。副本(panelVector、panelList);
返回向量;
}
公共对象selectedString(ItemSelectable为){
所选对象[]=为.GetSelectedObject();
返回((selected.length==0)?“null”:(ComboItem)selected[0]);
}
}

使用
卡布局
。有关工作示例,请参阅上的Swing教程。

使用
卡布局
。有关工作示例,请参阅上的Swing教程。

尝试以下代码:

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);
}
}
请尝试以下代码:

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);
}
}

请展示你迄今为止所做的代码…请展示你迄今为止所做的代码…-1,这段代码比教程中的代码好多少?教程包括对代码的解释,它还使用了一个组合框,这是要求的一部分。无需在论坛上重复回答。-1,这段代码比教程中的代码好多少教程中的代码?教程包括对代码的解释,并且它还使用了一个组合框,这是要求的一部分。无需在论坛中重复回答。