我尝试将Jmenubar与ActionListner和PaintComponent一起作为第一个面板,第二个面板的滑块和



这是一个具有Menubar的作业,其中包括用户可以从乘坐乘车后选择的骑行类型,第二个面板上会出现有关安全性和安全性和也是骑手身高的滑块。我尝试将所有内容放在一起,以将所有内容放在面板1和2中,因为Jmenubar,牵引和动作效果,但是除了滑块以外,没有其他内容。

标题

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 ##

我认为您首先必须使用默认值初始化msg(例如,对于空字符串)。否则Java将抛出 nullpoInterException (线程中的异常" Awt-Eventqueue-0" Java.lang.nullpoInterException:字符串为null)。如果变量是初始化的,则显示黑面板。

相关内容

  • 没有找到相关文章