抱歉的代码转储,但我不知道是哪里造成的问题。当我点击循环按钮的时候,它应该带我到循环页面(它是这样做的),但是我应该可以点击开始和中间的面板,蓝色的应该是黄色的动画面板,黄色的。当我运行这个程序时,我只看到黄色的动画面板,当我单击开始时,我得到一个“exception in thread”awt-eventqueue-0“java.lang.illegalargumentexception:wrong parent for cardlayout”错误。
/**
* @description:scheduling algoritmn visualisation app
* @version: 1.0
* @date created: 22/11/2020
*/
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.net.*;
public class SchedulingAlgorithms extends JFrame{
CardLayout cl;
JPanel contentPane;
Container deckPanel;
Image image;
/**
* Constructor for objects of class SchedulingAlgorithms
*/
public SchedulingAlgorithms(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 658, 336);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel headerPanel = new JPanel();
JLabel label = new JLabel();
headerPanel.setBackground(Color.LIGHT_GRAY);
headerPanel.setBounds(5, 5, 125, 106);
contentPane.add(headerPanel);
try{
URL url = new URL("file:C:///Users//alyssa//Scheduling_Animations//icon.png");
image=ImageIO.read(url);
headerPanel.add(new JLabel(new ImageIcon(image)));
}catch (IOException e) {
e.printStackTrace();
}
JPanel btnPanel = new JPanel();
btnPanel.setBackground(Color.LIGHT_GRAY);
btnPanel.setBounds(5, 111, 125, 181);
contentPane.add(btnPanel);
btnPanel.setLayout(null);
cl= new CardLayout();
JPanel deckPanel = new JPanel();
deckPanel.setBounds(140, 5, 492, 287);
contentPane.add(deckPanel);
deckPanel.setLayout(cl);
JPanel home = new JPanel();
deckPanel.add(home, "home");
JPanel homePic = new JPanel(new GridLayout(2,1));
home.add(homePic, "homePic");
JLabel instructions1 = new JLabel();
instructions1.setText("Select a scheduling algorithm to see it's visual representation");
homePic.add(instructions1);
try{
URL url = new URL("file:C:///Users//alyssa//Scheduling_Animations//schedule.png");
image=ImageIO.read(url);
homePic.add(new JLabel(new ImageIcon(image)));
}catch (IOException e) {
e.printStackTrace();
}
JPanel card1 = new JPanel();
deckPanel.add(card1, "rr");
card1.setLayout(new BorderLayout());
JLabel txt2= new JLabel();
txt2.setText("Round Robin: Set the boundaries of the animation");
card1.add(txt2);
JPanel card2 = new JPanel();
deckPanel.add(card2, "fcfs");
JTextArea txt3 = new JTextArea();
txt3.setText("First-Come-First-Served");
card2.add(txt3);
JPanel card3 = new JPanel();
deckPanel.add(card3, "sjf");
JTextArea txt4 = new JTextArea();
txt4.setText("Shortest Job First");
card3.add(txt4);
JPanel card4 = new JPanel();
deckPanel.add(card4, "ps");
JTextArea txt5 = new JTextArea();
txt5.setText("Priority Scheduling");
card4.add(txt5);
JButton btn0 = new JButton("Home");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Home")
cl.show(deckPanel, "home");
}
});
btn0.setBounds(10, 11, 89, 23);
btnPanel.add(btn0);
JButton btn1 = new JButton("Round Robin");
btn1.setBounds(10, 45, 89, 23);
btnPanel.add(btn1);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Round Robin")
cl.show(deckPanel, "rr");
}
});
JButton btn2 = new JButton("First-Come-First-Served");
btn2.setBounds(10, 79, 89, 23);
btnPanel.add(btn2);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="First-Come-First-Served")
cl.show(deckPanel, "fcfs");
}
});
JButton btn3 = new JButton("Shortest Job First");
btn3.setBounds(10, 113, 89, 23);
btnPanel.add(btn3);
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Shortest Job First")
cl.show(deckPanel, "sjf");
}
});
JButton btn4 = new JButton("Priority Scheduling");
btn4.setBounds(10, 147, 89, 23);
btnPanel.add(btn4);
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Priority Scheduling")
cl.show(deckPanel, "ps");
}
});
JPanel northPanel=new JPanel();
card1.add(northPanel,BorderLayout.NORTH);
JLabel heading=new JLabel();
heading.setText("Round Robin.");
northPanel.add(heading);
JLabel instructions2= new JLabel();
instructions2.setText("Set the boundaries of the animation");
northPanel.add(instructions2);
JPanel centerPanel=new JPanel();
centerPanel.setBackground(Color.blue);
card1.add(centerPanel,BorderLayout.CENTER);
centerPanel.setLayout(new CardLayout());
JPanel animPanel=new JPanel();
animPanel.setBackground(Color.yellow);
centerPanel.add(animPanel,"anim");
JPanel southPanel=new JPanel();
card1.add(southPanel,BorderLayout.SOUTH);
JButton start = new JButton("START");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="START")
cl.show(centerPanel, "anim");
}
});
start.setBounds(311, 159, 65, 23);
southPanel.add(start);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SchedulingAlgorithms frame = new SchedulingAlgorithms();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
1条答案
按热度按时间z8dt9xmd1#
您可以使用变量“cl”作为对布局管理器的引用来设置“deckpanel”的布局:
但当你试图改变“中心面板”上的卡片时:
不能共享cardlayout引用。如果希望“中心面板”也使用cardlayout,则需要为该布局管理器保留一个单独的参照。因此,对于各个布局管理器,您确实需要“decklayout”和“centerlayout”变量。这就是为什么变量名应该是描述性的,所以您知道如何使用变量。
不要使用“==”进行对象比较。而是使用
equals(...)
方法。