Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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 JPanel没有出现_Java_Swing_Jpanel - Fatal编程技术网

Java JPanel没有出现

Java JPanel没有出现,java,swing,jpanel,Java,Swing,Jpanel,我不确定我做错了什么,但是我的JPanel的文本没有显示出来。我刚收到问题编号文本,但问题没有显示出来。你知道我做错了什么吗 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class NewFrame extends JFrame { JPanel centerpanel; // For the questions. CardLayout

我不确定我做错了什么,但是我的JPanel的文本没有显示出来。我刚收到问题编号文本,但问题没有显示出来。你知道我做错了什么吗

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

class NewFrame extends JFrame {

  JPanel centerpanel;    // For the questions.
  CardLayout card;       // For the centerpanel.

  JTextField tf;         // Used in question 1.

  boolean                // Store selections for Q2.
    q2Option1,
    q2Option2,
    q2Option3,
    q2Option4;

  JList q4List;          // For question 4.

  double                 // Score on each question.
    q1Score = 0,
    q2Score = 0,
    q3Score = 0,
    q4Score = 0;

  // Constructor.
  public NewFrame (int width, int height)
  {
    this.setTitle ("Snoot Club Membership Test");
    this.setResizable (true);
    this.setSize (width, height);

    Container cPane = this.getContentPane();
    // cPane.setLayout (new BorderLayout());

    // First, a welcome message, as a Label.
    JLabel L = new JLabel ("<html><b>Are you elitist enough for our exclusive club?"
                          + " <br>Fill out the form and find out</b></html>");
    L.setForeground (Color.blue);
    cPane.add (L, BorderLayout.NORTH);

    // Now the center panel with the questions.
    card = new CardLayout ();
    centerpanel = new JPanel ();
    centerpanel.setLayout (card);
    centerpanel.setOpaque (false);

    // Each question will be created in a separate method.
    // The cardlayout requires a label as second parameter.
    centerpanel.add (firstQuestion (), "1");
    centerpanel.add (secondQuestion(), "2");
    centerpanel.add (thirdQuestion(), "3");
    centerpanel.add (fourthQuestion(), "4");
    cPane.add (centerpanel, BorderLayout.CENTER);

    // Next, a panel of four buttons at the bottom.
    // The four buttons: quit, submit, next-question, previous-question.
    JPanel bottomPanel = getBottomPanel ();
    cPane.add (bottomPanel, BorderLayout.SOUTH);

    // Finally, show the frame.
    this.setVisible (true);
  }

  // No-parameter constructor.
  public NewFrame ()
  {
    this (500, 300);
  }


  // The first question uses labels for the question and
  // gets input via a textfield. A panel containing all
  // these things is returned. The question asks for
  // a vacation destination: the more exotic the location,
  // the higher the score.

  JPanel firstQuestion ()
  {
    // We will package everything into a panel and return the panel.
    JPanel subpanel = new JPanel ();

    // We will place things in a single column, so
    // a GridLayout with one column is appropriate.
    subpanel.setLayout (new GridLayout (8,1));

    JLabel L1 = new JLabel ("Question 1:");
    L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L1);

    JLabel L2 = new JLabel ("  Select a vacation destination");
    L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L2);

    JLabel L3 = new JLabel ("    1. Baltimore");
    L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L3);

    JLabel L4 = new JLabel ("    2. Disneyland");
    L4.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L4);

    JLabel L5 = new JLabel ("    3. Grand Canyon");
    L5.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L5);

    JLabel L6 = new JLabel ("    4. French Riviera");
    L6.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L6);

    JLabel L7 = new JLabel ("Enter 1,2,3 or 4 below:");
    L7.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L7);

    // Here's the textfield to get user-input.
    tf = new JTextField ();
    tf.addActionListener (
      new ActionListener () {
        // This interface has only one method.
        public void actionPerformed (ActionEvent a)
    {
      String q1String = a.getActionCommand();
      if (q1String.equals ("2"))
        q1Score = 2;
      else if (q1String.equals ("3"))
        q1Score = 3;
      else if (q1String.equals ("4"))
        q1Score = 4;
      else
        q1Score = 1;
    }
      }
    );
    subpanel.add (tf);

    return subpanel;
  }


  // For the second question, a collection of checkboxes
  // will be used. More than one selection can be made.
  // A listener is required for each checkbox. The state
  // of each checkbox is recorded.

  JPanel secondQuestion ()
  {
    JPanel subpanel = new JPanel ();
    subpanel.setLayout (new GridLayout (7,1));

    JLabel L1 = new JLabel ("Question 2:");
    L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L1);

    JLabel L2 = new JLabel ("  Select ONE OR MORE things that ");
    L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L2);

    JLabel L3 = new JLabel ("  you put into your lunch sandwich");
    L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L3);

    // Initialize the selections to false.
    q2Option1 = q2Option2 = q2Option3 = q2Option4 = false;

    // First checkbox.
    JCheckBox c1 = new JCheckBox ("Ham, beef or turkey");
    c1.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JCheckBox c = (JCheckBox) i.getSource();
          q2Option1 = c.isSelected();
    }
      }
    );
    subpanel.add (c1);

    // Second checkbox.
    JCheckBox c2 = new JCheckBox ("Cheese");
    c2.addItemListener (
      new ItemListener () {
        // This is where we will react to a change in checkbox.
        public void itemStateChanged (ItemEvent i)
    {
      JCheckBox c = (JCheckBox) i.getSource();
          q2Option2 = c.isSelected();
    }
      }
    );
    subpanel.add (c2);

    // Third checkbox.
    JCheckBox c3 = new JCheckBox ("Sun-dried Arugula leaves");
    c3.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JCheckBox c = (JCheckBox) i.getSource();
          q2Option3 = c.isSelected();
    }
      }
    );
    subpanel.add (c3);

    // Fourth checkbox.
    JCheckBox c4 = new JCheckBox ("Lemon-enhanced smoked Siberian caviar");
    c4.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JCheckBox c = (JCheckBox) i.getSource();
          q2Option4 = c.isSelected();
    }
      }
    );
    subpanel.add (c4);

    return subpanel;
  }


  // The third question allows only one among four choices
  // to be selected. We will use radio buttons.

  JPanel thirdQuestion ()
  {
    JPanel subpanel = new JPanel ();
    subpanel.setLayout (new GridLayout (6,1));

    JLabel L1 = new JLabel ("Question 3:");
    L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L1);

    JLabel L2 = new JLabel ("  And which mustard do you use?");
    L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L2);

    // First, create the ButtonGroup instance.
    // We will add radio buttons to this group.
    ButtonGroup bGroup = new ButtonGroup();

    // First checkbox.
    JRadioButton r1 = new JRadioButton ("Who cares?");
    r1.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JRadioButton r = (JRadioButton) i.getSource();
          if (r.isSelected()) q3Score = 1;
    }
      }
    );
    bGroup.add (r1);
    subpanel.add (r1);

    // Second checkbox.
    JRadioButton r2 = new JRadioButton ("Safeway Brand");
    r2.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JRadioButton r = (JRadioButton) i.getSource();
          if (r.isSelected()) q3Score = 2;
    }
      }
    );
    bGroup.add (r2);
    subpanel.add (r2);

    // Third checkbox.
    JRadioButton r3 = new JRadioButton ("Fleishman's");
    r3.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JRadioButton r = (JRadioButton) i.getSource();
          if (r.isSelected()) q3Score = 3;
    }
      }
    );
    bGroup.add (r3);
    subpanel.add (r3);

    // Fourth checkbox.
    JRadioButton r4 = new JRadioButton ("Grey Poupon");
    r4.addItemListener (
      new ItemListener () {
        public void itemStateChanged (ItemEvent i)
    {
      JRadioButton r = (JRadioButton) i.getSource();
          if (r.isSelected()) q3Score = 4;
    }
      }
    );
    bGroup.add (r4);
    subpanel.add (r4);


    return subpanel;
  }


  // For the fourth question we will use a drop-down Choice.

  JPanel fourthQuestion ()
  {
    JPanel subpanel = new JPanel ();
    subpanel.setLayout (new GridLayout (3,1));

    JLabel L1 = new JLabel ("Question 4:");
    L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L1);

    JLabel L2 = new JLabel ("  Your movie preference, among these:");
    L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
    subpanel.add (L2);

    // Create a JList with options.
    String[] movies = { "Lethal Weapon IV", "Titanic", "Saving Private Ryan",
                        "Le Art Movie avec subtitles"};
    q4List = new JList (movies);
    q4Score = 1;
    q4List.addListSelectionListener (
      new ListSelectionListener () {
        public void valueChanged (ListSelectionEvent e)
    {
      q4Score = 1 + q4List.getSelectedIndex();
    }
      }
    );
    subpanel.add (q4List);

    return subpanel;
  }

  void computeResult ()
  {
    // Clear the center panel.
    centerpanel.removeAll();

    // Create a new panel to display in the center.
    JPanel subpanel = new JPanel (new GridLayout (5,1));

    // Score on question 1.
    JLabel L1 = new JLabel ("Score on question 1: " + q1Score);
    L1.setFont (new Font ("Serif", Font.ITALIC, 15));
    subpanel.add (L1);

    // Score on question 2.
    if (q2Option1) q2Score += 1;
    if (q2Option2) q2Score += 2;
    if (q2Option3) q2Score += 3;
    if (q2Option4) q2Score += 4;
    q2Score = 0.6 * q2Score;
    JLabel L2 = new JLabel ("Score on question 2: " + q2Score);
    L2.setFont (new Font ("Serif", Font.ITALIC, 15));
    subpanel.add (L2);

    // Score on question 3.
    JLabel L3 = new JLabel ("Score on question 3: " + q3Score);
    L3.setFont (new Font ("Serif", Font.ITALIC, 15));
    subpanel.add (L3);

    // Score on question 4.
    JLabel L4 = new JLabel ("Score on question 4: " + q4Score);
    L4.setFont (new Font ("Serif", Font.ITALIC, 15));
    subpanel.add (L4);

    // Weighted score.
    double avg = (q1Score + q2Score + q3Score + q4Score) / (double) 4;
    JLabel L5;
    if (avg <= 3.5)
      L5 = new JLabel ("Your average score: " + avg + " - REJECTED!");
    else
      L5 = new JLabel ("Your average score: " + avg + " - WELCOME!");
    L5.setFont (new Font ("Serif", Font.BOLD, 20));
    //L5.setAlignment (JLabel.CENTER);
    subpanel.add (L5);

    // Now add the new subpanel.
    centerpanel.add (subpanel, "5");

    // Need to mark the centerpanel as "altered"
    centerpanel.invalidate();

    // Everything "invalid" (e.g., the centerpanel above)
    // is now re-computed.
    this.validate();
  }

  JPanel getBottomPanel ()
  {
    // Create a panel into which we will place buttons.
    JPanel bottomPanel = new JPanel ();

    // A "previous-question" button.
    JButton backward = new JButton ("Previous question");
    backward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
    backward.addActionListener (
      new ActionListener () {
        public void actionPerformed (ActionEvent a)
    {
      // Go back in the card layout.
      card.previous (centerpanel);
    }
      }
    );
    bottomPanel.add (backward);

    // A forward button.
    JButton forward = new JButton ("Next question");
    forward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
    forward.addActionListener (
      new ActionListener () {
        public void actionPerformed (ActionEvent a)
    {
      // Go forward in the card layout.
      card.next (centerpanel);
    }
      }
    );
    bottomPanel.add (forward);

    // A submit button.
    JButton submit = new JButton ("Submit");
    submit.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
    submit.addActionListener (
      new ActionListener () {
        public void actionPerformed (ActionEvent a)
    {
      // Perform submit task.
      computeResult();
    }
      }
    );
    bottomPanel.add (submit);

    JButton quitb = new JButton ("Quit");
    quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
    quitb.addActionListener (
      new ActionListener () {
        public void actionPerformed (ActionEvent a)
    {
      System.exit (0);
    }
      }
    );
    bottomPanel.add (quitb);

    return bottomPanel;
  }

}


public class Survey {
  public static void main (String[] argv)
  {
    NewFrame nf = new NewFrame (600, 300);
  }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
类NewFrame扩展了JFrame{
JPanel centerpanel;//用于回答问题。
CardLayout卡;//用于中心面板。
JTextField;//用于问题1。
布尔//存储Q2的选择。
Q2选择1,
Q2选择2,
Q2选择3,
选择4;
JList q4List;//用于问题4。
每道题都得双//分。
Q1得分=0,
Q2分数=0,
q3Score=0,
q4Score=0;
//构造器。
公共新帧(整数宽度、整数高度)
{
本条标题为“斯努特俱乐部会员资格测试”;
此参数为.setresizeable(true);
此设置尺寸(宽度、高度);
容器cPane=this.getContentPane();
//cPane.setLayout(新的BorderLayout());
//首先,欢迎信息,作为标签。
JLabel L=新的JLabel(“对于我们的独家俱乐部来说,你够精英吗?”
+“
填写表格并找出答案”); L.前景色(蓝色); cPane.add(L,BorderLayout.NORTH); //现在是中间的面板,上面有问题。 卡片=新卡片布局(); centerpanel=新的JPanel(); centerpanel.setLayout(卡); centerpanel.set不透明(假); //每个问题将以单独的方法创建。 //cardlayout需要一个标签作为第二个参数。 centerpanel.add(第一个问题(),“1”); centerpanel.add(第二个问题(),“2”); centerpanel.add(第三个问题(),“3”); centerpanel.add(第四个问题(),“4”); cPane.add(中心面板,BorderLayout.CENTER); //接下来,在底部有一个由四个按钮组成的面板。 //四个按钮:退出、提交、下一个问题、上一个问题。 JPanel bottomPanel=getBottomPanel(); cPane.add(底部面板,BorderLayout.SOUTH); //最后,显示框架。 此.setVisible(true); } //没有参数构造函数。 公共新框架() { 这(500300); } //第一个问题使用问题的标签,并且 //通过文本字段获取输入。包含所有 //这些东西被归还了。问题是 //度假目的地:地点越有异国情调, //分数越高。 JPanel第一个问题() { //我们将把所有东西打包成一个面板,然后将面板退回。 JPanel subpanel=新的JPanel(); //我们将把东西放在一列中,所以 //具有一列的GridLayout是合适的。 子面板设置布局(新网格布局(8,1)); JLabel L1=新的JLabel(“问题1:”); L1.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L1); JLabel L2=新JLabel(“选择度假目的地”); L2.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L2); JLabel L3=新的JLabel(“1.巴尔的摩”); L3.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L3); JLabel L4=新JLabel(“2.迪士尼乐园”); L4.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L4); JLabel L5=新JLabel(“3.大峡谷”); L5.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L5); JLabel L6=新JLabel(“4.法国里维埃拉”); L6.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L6); JLabel L7=新的JLabel(“在下面输入1、2、3或4:”); L7.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L7); //下面是获取用户输入的文本字段。 tf=新的JTextField(); tf.addActionListener( 新建ActionListener(){ //此接口只有一个方法。 已执行的公共无效操作(操作事件a) { 字符串q1String=a.getActionCommand(); 如果(q1String.equals(“2”)) Q1得分=2分; 如果(q1String.equals(“3”)) Q1得分=3分; 如果(q1String.equals(“4”)) Q1得分=4分; 其他的 Q1得分=1; } } ); 子面板添加(tf); 返回子面板; } //对于第二个问题,一组复选框 //将使用。可以进行多个选择。 //每个复选框都需要一个侦听器。状态 //记录每个复选框的名称。 JPanel第二个问题() { JPanel subpanel=新的JPanel(); 子面板设置布局(新网格布局(7,1)); JLabel L1=新的JLabel(“问题2:”); L1.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L1); JLabel L2=新的JLabel(“选择一个或多个符合条件的内容”); L2.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L2); JLabel L3=新的JLabel(“你放进午餐三明治里”); L3.setFont(新字体(“SansSerif”,Font.ITALIC,15)); 子面板添加(L3); //将选择初始化为false。 q2Option1=q2Option2=q2Option3=q2Option4=false; //第一个复选框。 JCheckBox c1=新JCheckBox(“火腿、牛肉或火鸡”); c1.addItemListener( 新的ItemListener(){ 公共无效itemStateChanged(ItemEvent i) { JCheckBox c=(JCheckBox)i.getSource(); q2Option1=c.isSelected(); } } ); 子面板添加(c1); //第二个复选框。 JCheckBox c2=新的JCheckBox(“奶酪”); c2.addItemListener( 新的ItemListener(){ //这是我们将对复选框中的更改作出反应的地方。 公共无效itemStateChanged(ItemEvent i) { JCheckBox c=(JCheckBox)i.getSource(); q2Option2=c.isSelected(); } } ); 子面板添加(c2); //第三个复选框。 JCheckBox c3=新JCheckBox(“晒干芝麻菜叶”); c3.addItemListener( 新的ItemListener(){ 公共无效itemStateChanged(ItemEvent i) { JCheckBox c=(JCheckBox)i.getSourc
new GridLayout(8,1)
new GridLayout(0, 1);