如何显示所有Cardlayouts

时间:2015-05-29 05:49:15

标签: java swing layout-manager jcombobox cardlayout

如何从组合框中选定的卡中获取字符串,并使用getSelectedItem fractalChooser上的combobox方法并将结果转换为字符串?目前它只显示一个对象。

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


public class FractalDriver
{
   private static final int WIDTH = 350;
   private static final int HEIGHT = 300;
   private static final String CANTOR = "Cantor";
   private static final String CIRCLE = "Circle";
   private static final String MANDELBROT = "Mandelbrot";
   private static final String SIERPINSKI = "Sierpinski";
   private static final String[] allFractals = {CANTOR, CIRCLE, MANDELBROT,  SIERPINSKI};

   private JFrame frame;
   private CardLayout cardLayout;
   private JPanel fractalCards;
   private JComboBox<String> fractalChooser;
   public FractalDriver()
   {
    makeFrame();
   }

private void makeFrame()
{
    frame = new JFrame("Fractals!");
    frame.setSize(WIDTH, HEIGHT);
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createContents();
    frame.setVisible(true);
 } 

 private void createContents() 
{
    JScrollPane cantorPane = new JScrollPane(new CantorPanel(6));
    JScrollPane circlePane = new JScrollPane(new CirclesPanel(6));
    JScrollPane mandelbrotPane = new JScrollPane(new MandelbrotPanel(6));
    JScrollPane sierpinskiPane = new JScrollPane(new SierpinskiPanel(6));

    cardLayout = new CardLayout();
    fractalCards = new JPanel();
    fractalCards.setLayout(cardLayout);
    fractalCards.add(cantorPane, CANTOR);
    fractalCards.add(circlePane, CIRCLE);
    fractalCards.add(mandelbrotPane, MANDELBROT);
    fractalCards.add(sierpinskiPane, SIERPINSKI);
    fractalChooser = new JComboBox<String> (allFractals);
    fractalChooser.addActionListener(new ComboListener());
    frame.add(fractalChooser, BorderLayout.NORTH);
    frame.add(fractalCards, BorderLayout.CENTER);
}

private class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
    fractalChooser.getSelectedItem();
    cardLayout.show(fractalCards, SIERPINSKI);
  }
}

public static void main(String[] args)
 {
    new FractalDriver();
 }
}

1 个答案:

答案 0 :(得分:1)

简单说明<Spinner fx:id="spn"> <styleClass> <String fx:value="split-arrows-horizontal" /> </styleClass> </Spinner> getSelectedItem ...

的结果
String