gpt4 book ai didi

java - 我在简单的程序中获取 CardLayout 更改卡片时遇到问题

转载 作者:行者123 更新时间:2023-12-01 22:36:05 24 4
gpt4 key购买 nike

这个构造函数的存在是为了制作一个内部有四个面板的框架。这些面板中的每一个都应该能够使用卡片布局在空面板和带有内容的面板之间循环。 (第一个面板从显示的内容开始,但需要卡片布局以供将来使用)我刚刚完成第一个和第三个面板的元素添加,但卡片已经给我带来了问题。我尝试了很多方法,但似乎没有什么对我的情况有帮助。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Menu implements ActionListener{

//Create an array of names for the panels
String[] panelNames = {"part1", "part2", "part3", "part4"};

//Create an array of panels
JPanel[] panels = new JPanel[panelNames.length];

//creates an array of the cards to be added to the panels created above
JPanel[] cards = new JPanel[panelNames.length];

//Create an array of buttons
JButton[] buttons1 = new JButton[4];

//Create an array of buttons
JButton[] buttons2 = new JButton[4];

//create a default panel for the initially empty panels
JPanel empty = new JPanel();

//Create a layout
CardLayout c = new CardLayout();

Menu(){

//Create a JFrame to hold the panels
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Select your application");
frame.setSize(700,700);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());

//Panel that contains the four panels needed for this assignment
JPanel parts = new JPanel(new GridLayout(2,2));

//a loop that creates each panels and fills it with the cards
for(int i = 0 ; i < panelNames.length ; i++){

//define the panels and adds them to the frame
panels[i] = new JPanel(c);
panels[i].setPreferredSize(new Dimension(300,300));
cards[i] = new JPanel(new FlowLayout());
parts.add(panels[i]);
panels[i].add(empty, "empty");
panels[i].add(cards[i], "card");
c.show(panels[i], "empty");
}
c.show(panels[0], "card");
frame.add(parts, BorderLayout.NORTH);

//adds 4 buttons to the first panel and names them
Border line = new LineBorder(Color.GRAY,1);
cards[0].setLayout(new GridLayout(4,1));
for(int i = 0 ; i < buttons1.length ; i++){

buttons1[i] = new JButton("TBA");
buttons1[i].setBackground(Color.white);
buttons1[i].setBorder(line);
buttons1[i].addActionListener(this);
cards[0].add(buttons1[i]);
}
buttons1[0].setText("Sudoku");

//adds 4 buttons to the 3rd panel and names them
cards[2].setLayout(new GridLayout(4,1));
for(int i = 0 ; i < buttons2.length ; i++){

buttons2[i] = new JButton();
buttons2[i].setBackground(Color.LIGHT_GRAY);
buttons2[i].addActionListener(this);
cards[2].add(buttons2[i]);
buttons2[i].setEnabled(false);
}
buttons2[0].setText("Sample");
buttons2[1].setText("Solution");
buttons2[2].setText("Verify");
buttons2[3].setText("Return to main");

//adds the exit button and the exit panels to the frame
JPanel exit = new JPanel();
frame.add(exit, BorderLayout.SOUTH);
JButton exitb = new JButton("exit");
exitb.setBackground(Color.red);
exitb.setForeground(Color.white);
exitb.setPreferredSize(new Dimension(245,30));
exit.add(exitb);
exitb.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {

System.exit(0);
}
});

//show the frame
frame.setVisible(true);
}

//method that determined which app to launch and launches it
@Override
public void actionPerformed(ActionEvent ae) {

//finds the source of the event
Object src = ae.getSource();

//these statements do the appropriate actions when a button is pressed
if(src == buttons1[0]){

c.show(panels[2], "card");
buttons2[0].setEnabled(true);
buttons2[3].setEnabled(true);

}

else if(src == buttons2[0]){

buttons2[1].setEnabled(true);
buttons2[0].setEnabled(false);
}
else if(src == buttons2[1]){

buttons2[2].setEnabled(true);
buttons2[1].setEnabled(false);
}
else if(src == buttons2[2]){

buttons2[2].setEnabled(false);
}
else if(src == buttons2[3]){

c.show(panels[2], "empty");
for(int i = 0 ; i < buttons1.length ; i++){buttons2[i].setEnabled(false);
buttons1[i].setEnabled(true);}
}

else{System.out.println("option not yet implemented");}
}
}

感谢您的帮助!

最佳答案

  • 组件只能驻留在单个容器中。每次将组件添加到另一个容器时,它首先(有效)从其当前父容器中删除。您无法按原样重复使用 empty
  • CardLayout不能管理多个容器,它负责管理单个容器。

我的“直觉”感觉是创建一个从 JPanel 扩展的单独的自定义类,它有一个 show 方法,允许您显示指定的组件。该面板将使用它自己的 CardLayout 并管理其内部状态。这将允许您根据需要添加附加内容的跳转点...

关于java - 我在简单的程序中获取 CardLayout 更改卡片时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26856099/

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