我尝试将带有actionlistner和paintComponent的JMenuBar作为第一个面板,一个滑块和第二个面板的String,但没有显示任何内容

时间:2015-05-07 07:06:44

标签: java jmenu jmenuitem jmenubar

这是一个具有菜单栏的作业,其中包括用户可以选择的游乐设施类型,在骑行被挑选后,第二个面板上会出现关于安全性的信息以及骑行者身高的滑块。我尝试将所有内容放在panel1和2中,作为JMenuBar,drawString和actionperformed,但除了滑块之外什么都没有显示。

标题

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


public class Ride extends JFrame
{
        private JMenu type;
        private JSlider slider;
        private GridLayout grid;
        private JMenuBar menu;
        private JMenuItem Roller;
        private JMenuItem Horror;
        private int sliderVal;
        private int allow;
        private String msg;
        private String yesno;
        private BorderLayout b;



        public static void main(String[]args)
        {
                Ride ride = new Ride();
        }

        public Ride()
        {
                super("Ride");
                setSize(1200,1000);
                Panel1 p1 = new Panel1();
                Panel2 p2 = new Panel2();
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setLayout(new GridLayout(1,2,0,0));
                add(p1);
                add(p2);
                setVisible(true);
        }

        class Panel1 extends JPanel implements ActionListener
        {
                public Panel1()
                {
                    setSize(600,1000);
                        setBackground(Color.BLACK);
                        //Direction direction = new Direction();
                        type = new JMenu("type of rides");
                        Roller = new JMenuItem("Roller Coaster");
                        Roller.addActionListener(this);
                        type.add(Roller);
                        Horror = new JMenuItem("Scary Ride");
                        Horror.addActionListener(this);
                        type.add(Horror);
                        menu = new JMenuBar();
                        menu.add(type);
                       // setLayout(new GridLayout(2,1,0,20));
                        add(menu);
                        //add(direction);
                        setVisible(true);
                }

                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==Roller)
                        {
                                msg = new String("Rough Ride, height must be >42");
                                allow = 42;
                                repaint();
                        }
                        else if(e.getSource()==Horror)
                        {
                                msg = new String("Scary Ride, height must be >32");
                                allow = 32;
                                repaint();
                        }
                }

                public void paintComponent(Graphics g)
                {
                        super.paintComponent(g);
                        g.drawString("Pick Ride on the menu above,",10,600);
                        g.drawString("select height on the slidr.",10,700);
                }
        }

        class Panel2 extends JPanel implements ChangeListener
        {
            public Panel2()
            {
                setSize(600,1000);
                    setLayout(new FlowLayout());
                    slider = new JSlider(JSlider.VERTICAL,30,45,30);
                    slider.addChangeListener(this);
                    add(slider);
                    setVisible(true);

            }


            public void stateChanged(ChangeEvent e)
            {
                            sliderVal = slider.getValue();//get the value of the slider
                            repaint();//reset
                            if(sliderVal > allow)
                                    yesno = new String("allowed");
                            else if (sliderVal < allow)
                                    yesno = new String("not allowed");
            }


            public void paintComponent(Graphics g)
            {
                    super.paintComponent(g);

                    g.drawString(msg,20,600);
                    g.drawString(sliderVal+" :"+yesno,20,800);
            }

        }
}## Heading ##

1 个答案:

答案 0 :(得分:0)

我认为您必须首先使用默认值初始化msg(例如&#34;&#34;表示空字符串)。否则java抛出 NullPointerException (线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.NullPointerException:String为null)。如果初始化变量,则显示黑色面板。