gpt4 book ai didi

java - 如何显示所有卡片布局

转载 作者:行者123 更新时间:2023-12-01 11:29:33 24 4
gpt4 key购买 nike

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

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

最佳答案

简单情况下 getSelectedItem 的结果为 String...

private class ComboListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String name = (String)fractalChooser.getSelectedItem();

if (name != null) {
cardLayout.show(fractalCards, name);
}

}
}

关于java - 如何显示所有卡片布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com