【问题标题】:Calling a JPanel in a JFrame of another Class在另一个类的 JFrame 中调用 JPanel
【发布时间】:2013-11-03 13:32:36
【问题描述】:

我创建了一个带有一个 JPanel 的类,该 JPanel 将用于 CardLayout,如果我删除注释 // 它在代码底部具有窗口大小,则可以正常工作。它将以这种方式运行,并且一切都运行良好。但是,当我尝试从另一个包含 JFrame 的类中调用它时,它不起作用。

CardDemo.java:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

public class CardDemo extends JPanel implements ListSelectionListener {

CardLayout cl;
JPanel p1, p2, p3, p4, p5;
JList l1;


public CardDemo(){

    p1 = new JPanel();
    p1.setBackground(Color.red);   //the top panel

    String[] list1 = { "One", "Two", "Three"};
    l1 = new JList(list1);
    l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    l1.addListSelectionListener(this);

    p1.add(l1);

    p2 = new JPanel();
    p2.setBackground(Color.blue);  //bottom panel - we never actually see this

    cl = new CardLayout();
    p2.setLayout(cl);


    p3 = new JPanel();
    p3.setBackground(Color.green);
    p4 = new JPanel();
    p4.setBackground(Color.yellow); 
    p5 = new JPanel();
    p5.setBackground(Color.cyan);


    p2.add(p3, "One");
    p2.add(p4, "Two");
    p2.add(p5, "Three");

    //this.setSize(500,400);
   //this.setVisible(true);
   this.setLayout(new GridLayout(2,2));
   this.add(p1);
   this.add(p2);

}

/** The actionPerformed method handles button clicks
 */
public void valueChanged(ListSelectionEvent e) {
    String listLabel = (String) ((JList)e.getSource()).getSelectedValue();    //get the label of the button that was clicked
    cl.show(p2, listLabel);             //use the label to display the relevant panel
}
}   

Test.java

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

public class Test extends JFrame
{

public Test()
{
  JFrame frame = new JFrame("CardLayoutDemo");
  frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

  CardDemo b = new CardDemo();
  b.add(frame);

  frame.pack();
  frame.setVisible(true);
}


}

一切都编译得很好,但是当我运行 Test.java 时出现以下错误:

java.lang.IllegalArgumentException: 向容器中添加窗口(在 java.awt.Container 中)

我做错了什么,因为我似乎无法确定它。

【问题讨论】:

  • 关于"PLEASE DO NOT APPROVE THIS AS I DO NOT WANT THIS KEPT ON HERE, I WISH TO DELETE THIS AFTER AS I DON'T WANT MY WORK TO REMAIN ON THE NET, MUCH APPRECIATED."——你不能这样做。如果它发布在这里,它应该保留在这里,因为这里发布的问题不仅是为了您的利益,而且是为了所有未来有类似问题的访问者。我已经从你的问题中删除了这个,所以毫无疑问。
  • 问题回滚到以前的状态。请不要在没有解释的情况下更改您的代码。
  • 没有人会窃取您的代码。不管怎样,我坚持我之前的评论,这些问题不仅是为了提问者的利益,而且主要是为了这个网站的未来访问者。

标签: java swing jframe jpanel cardlayout


【解决方案1】:

你需要改变

b.add(frame);  

frame.add(b);

【讨论】:

    【解决方案2】:

    您正在将您的 JFrame 添加到您的 JPanel。应该反过来。 frame 变量是指一个 JFrame,它是一个顶级窗口。这是所有组件的顶级容器,因此您需要向其中添加组件,而不是相反。

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2021-01-29
      • 2012-06-13
      相关资源
      最近更新 更多