gpt4 book ai didi

java - CardLayout 之前的功能不起作用?

转载 作者:行者123 更新时间:2023-11-29 05:26:55 24 4
gpt4 key购买 nike

我的卡片布局的下一个功能工作正常,但上一个功能不工作。就我而言,只有“layout.previous(_);”在我的 makePanel() 方法中的 actionPerformed 方法体中应该可以工作,但是当我运行我的程序并单击 prev 按钮时,没有任何反应。我究竟做错了什么? –

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


public class Temp
{
public static void main (String[] args)
{
new MakeAQuiz();
}

static class MakeAQuiz
{

private JPanel start, base, fields, buttonz, question;
private String [] labels = {"Enter your question: ", "Answer 1: ", "Answer 2: ", "Answer 3: ", "Answer 4: "};
private JButton [] buttons = {new JButton("<<Go back"), new JButton("I'm done"), new JButton("Next>>")};
private JFrame makeFrame;

public MakeAQuiz()
{
start = new JPanel(new CardLayout());
start.add(makePanel(),"1");

makeFrame = new JFrame();
makeFrame.setSize(500,600);
makeFrame.add(start);
makeFrame.setVisible(true);
makeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public JFrame getJFrame()
{
return makeFrame;
}

public JPanel makePanel()
{
question = new JPanel(new FlowLayout());
fields = new JPanel(new GridBagLayout());
buttonz = new JPanel(new FlowLayout());
base = new JPanel(new BorderLayout());

GridBagConstraints c = new GridBagConstraints();
c.weighty=0.5; //adds padding between the fields vertically

for (int i = 1; i<5; i++)
{
c.gridy++; //puts each field in a seperate line/row
JLabel label = new JLabel(labels[i]);
// c.fill = GridBagConstraints.HORIZONTAL;
fields.add(label,c);
JTextField textField = new JTextField(20);
fields.add(textField,c);
}


final CardLayout layout = (CardLayout)start.getLayout();

buttons[1].addActionListener(new ActionListener() {
// @Override
public void actionPerformed(ActionEvent e) {
buttons[0].setEnabled(false);
buttons[2].setEnabled(false);
// for(Component comp : cardPanel.getComponents()) {
// if(comp instanceof Page) {
// Page page = (Page)comp;
// page.printData();
// }
// }
}
});

buttons[2].addActionListener(new ActionListener()
{
// @Override
public void actionPerformed(ActionEvent e)
{
start.add(makePanel(), String.valueOf(start.getComponentCount() + 1));
layout.next(start);
}
});

buttons[0].addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
layout.previous(start);
}
});

buttonz.add(buttons[0]);
buttonz.add(buttons[1]);
buttonz.add(buttons[2]);

JLabel l = new JLabel(labels[0]);
JTextField t = new JTextField(30);
question.add(l);
question.add(t);

base.add(question,BorderLayout.NORTH);
base.add(buttonz,BorderLayout.SOUTH);
base.add(fields,BorderLayout.CENTER);

return base;
}

}
}

最佳答案

一旦我向 start 面板添加了一些额外的组件,我就没有问题(您的代码)。

但是,您会遇到问题,因为您已将按钮添加到显示在 CardLayout 中的面板中。

更好的解决方案是将按钮放在主屏幕底部并将其与卡片分开。

您需要维护某种计数器或对当前页面的引用,因为 CardLayout 不提供任何获取对当前卡片的引用的方法。这将允许您适本地启用/禁用下一个/上一个按钮...

更新了可运行示例...

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MakeAQuiz {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new MakeAQuiz();
}
});
}

private JPanel start, base, fields, buttonz, question;
private String[] labels = {"Enter your question: ", "Answer 1: ", "Answer 2: ", "Answer 3: ", "Answer 4: "};
private JButton[] buttons = {new JButton("<<Go back"), new JButton("I'm done"), new JButton("Next>>")};
private JFrame makeFrame;

public MakeAQuiz() {
start = new JPanel(new CardLayout());
start.add(makePanel(), "1");

makeFrame = new JFrame();
makeFrame.add(start);

buttonz = new JPanel(new FlowLayout());

final CardLayout layout = (CardLayout) start.getLayout();

buttons[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttons[0].setEnabled(false);
buttons[2].setEnabled(false);
}
});

buttons[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = start.getComponentCount();
start.add(new JLabel(Integer.toString(count), JLabel.CENTER), Integer.toString(count));
layout.next(start);
}
});

buttons[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
layout.previous(start);
}
});

buttonz.add(buttons[0]);
buttonz.add(buttons[1]);
buttonz.add(buttons[2]);

makeFrame.add(buttonz, BorderLayout.SOUTH);

makeFrame.pack();
makeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
makeFrame.setVisible(true);
}

public JFrame getJFrame() {
return makeFrame;
}

public JPanel makePanel() {
question = new JPanel(new FlowLayout());
fields = new JPanel(new GridBagLayout());
base = new JPanel(new BorderLayout());

GridBagConstraints c = new GridBagConstraints();
c.weighty = 0.5; //adds padding between the fields vertically

for (int i = 1; i < 5; i++) {
c.gridy++; //puts each field in a seperate line/row
JLabel label = new JLabel(labels[i]);
// c.fill = GridBagConstraints.HORIZONTAL;
fields.add(label, c);
JTextField textField = new JTextField(20);
fields.add(textField, c);
}

JLabel l = new JLabel(labels[0]);
JTextField t = new JTextField(30);
question.add(l);
question.add(t);

base.add(question, BorderLayout.NORTH);
base.add(fields, BorderLayout.CENTER);

return base;
}

}

关于java - CardLayout 之前的功能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367938/

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