Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CardLayout未运行_Java_Swing_Jpanel_Cardlayout - Fatal编程技术网

Java CardLayout未运行

Java CardLayout未运行,java,swing,jpanel,cardlayout,Java,Swing,Jpanel,Cardlayout,您好,我尝试创建一个程序,当点击按钮时,它将使用CardLayout切换JPanel 资料来源: 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.JButton; import javax.swing.JF

您好,我尝试创建一个程序,当点击按钮时,它将使用CardLayout切换JPanel

资料来源:

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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CardDemo extends JFrame implements ActionListener {

CardLayout cl;
JPanel p1, p2, p3, p4, p5;
JButton b1, b2, b3;

public static void main(String args[]) {
    CardDemo d = new CardDemo();

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

    JButton b1 = new JButton("One");    //create the buttons with labels
    JButton b2 = new JButton("Two");
    JButton b3 = new JButton("Three");

    b1.addActionListener(d);     //this object listens for button clicks
    b2.addActionListener(d);
    b3.addActionListener(d);

    p1.add(b1);       //uttons added to the top panel
    p1.add(b2);
    p1.add(b3);

    cl = new CardLayout();     //create the CardLayout object
    p2.setLayout(cl);          //set p2 to use a CardLayout

    JPanel p3 = new JPanel();         //make the three panels that we
    p3.setBackground(Color.green);    //will put into the CardLayout panel
    JPanel p4 = new JPanel();
    p4.setBackground(Color.yellow);   //give them different colours
    JPanel p5 = new JPanel();
    p5.setBackground(Color.cyan);

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

d.setSize(500,400);
d.setVisible(true);
d.getContentPane().setLayout(new GridLayout(2,2));
d.getContentPane().add(p1);
d.getContentPane().add(p2);
}

/** The actionPerformed method handles button clicks
 */
public void actionPerformed(ActionEvent e) {
    String buttonLabel = ((JButton)e.getSource()).getText();
    cl.show(p2, buttonLabel);
}
}
当我编译代码时,我会出错,我尝试了所有的方法来让它工作,但似乎无法找出哪里出了问题。顺便说一句,我是Java的初学者,我可能在某个地方犯了一个小的业余错误

错误消息如下:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-   static variable p1 cannot be referenced from a static context
at CardDemo.main(CardDemo.java:22)
Java Result: 1

您正在尝试在静态方法中分配成员变量(本例中为main)。那不行。实例变量只能分配给实例

p1 = new JPanel(); //p1 is a member variable
你应该这样做:
d.p1=newjpanel()用于您的示例

但这是一条艰难的道路。我建议创建一个成员方法,初始化其中的所有内容,并在主方法中调用这个init方法

public static void main(String[] args) {
    CardDemo cd = new CardDemo();
    cd.init();
}

private void init() {
   p1 = new JPanel();
...

你会收到什么错误,你的具体问题是什么?你应该考虑使用一个IDE,比如Eclipse或者NETBeBes。IDE是一个在其内部编写代码的程序。这两个函数都有语法突出显示,在您为java键入时显示编译时错误。编程的第一条规则:读取线程“main”java.lang.RuntimeException中的错误消息。@MichaelJ.Gray Exception:不可编译的源代码-不能从CardDemo.main(CardDemo.java:22)的静态上下文引用非静态变量p1。java结果:1不做此假设。相反,尽你所能让那些阅读你的问题并愿意免费帮助你的人的生活尽可能简单。仅读取错误消息通常就足以诊断问题。如果你懒得粘贴完整而准确的错误消息,我就不必费心回答你的问题(其他大多数读者也不会回答);getContentPane().add(p2);是的,解决了这个错误,但是得到了一个空白窗口,没有显示任何内容是的,但是我在d.getContentPane中得到了一个错误,说它找不到符号,因为不再有d了。当您将d放入成员方法中时,它指的是
。顺便问一下,你对Java了解多少?我知道基本知识,是的,我改变了它说d的地方,现在它正在运行,很好!