在程序文件中创建JButton,JSlider和Graphics

时间:2013-12-28 15:48:33

标签: java swing jframe jpanel jcomponent

帮助我创建一个包含JButton和JSlider以及Java Graphics的类文件。

我尝试了很多方法,但没有使用netbeans 7.2:Jpanel,Jcomponent,paint(),paintComponent()。

任何人都可以给我一个类文件,例如创建那个吗?

我的代码在这里:

    package floating;
    import designs.*;
    import java.awt.*;
    import javax.swing.*;

    public class Test extends JFrame
    {
        String frame_name;
        boolean START,PASS;
        int SPEED=1;
        JSlider jSlider1 = new javax.swing.JSlider();
        JSeparator js=new JSeparator();
        JToggleButton jToggleButton1 = new javax.swing.JToggleButton("START");
        JToggleButton jToggleButton2 = new javax.swing.JToggleButton("PASS");

        Test(String a)
        {

           JFrame jf=new JFrame(a);
            Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
            jf.setSize(dim.width,dim.height-30);
            jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); 


            js.setOrientation(SwingConstants.VERTICAL);
            js.setForeground(Color.red);
            js.setLocation(dim.width/2,0);
            js.setPreferredSize(dim);
            js.setSize(10,dim.height);
            jf.add(js);


            jSlider1.setLocation(dim.width-300,20);
            jSlider1.setSize(250,50);
            jSlider1.setToolTipText("Set to Seconds");
            jSlider1.setMinimum(1);jSlider1.setMaximum(5);jSlider1.setMajorTickSpacing(1);jSlider1.setMinorTickSpacing(1);jSlider1.setValue(1);
            jSlider1.setPaintLabels(true);
            jSlider1.setPaintTicks(true);
            jSlider1.setPaintTrack(true);
            jSlider1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jSlider1MouseClicked(evt);
                }
            });
            jf.add(jSlider1);

            jToggleButton1.setLocation((dim.width/2)+30,25);
            jToggleButton1.setSize(100,30);
            jToggleButton1.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    jToggleButton1ItemStateChanged(evt);
                }
            });


            jToggleButton2.setLocation((dim.width/2)+50+100,25);
            jToggleButton2.setSize(100,30);
            jToggleButton2.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    jToggleButton2ItemStateChanged(evt);
                }
            });


            jf.add(jToggleButton1);
            jf.add(jToggleButton2);


            jf.setLayout(null);  \\if layout is null the only all buttons and slider at positions and showing in JFrame 
            jf.setVisible(true);

        }

        public void jToggleButton1ItemStateChanged(java.awt.event.ItemEvent evt) 
        {
            if(jToggleButton1.getActionCommand()=="START")
            {
                System.out.printf(jToggleButton1.getText());
                jToggleButton1.setText("STOP");
                this.START=true;
                System.out.println(this.START);
            }
            else
            {
                System.out.printf(jToggleButton1.getText());
                jToggleButton1.setText("START");
                this.START=false;
                System.out.println(this.START);
            }
        }
        public void jToggleButton2ItemStateChanged(java.awt.event.ItemEvent evt) 
        {
            if( jToggleButton2.getActionCommand()=="PASS")
            {
                System.out.printf(jToggleButton2.getText());
                jToggleButton2.setText("RESUME");
                this.PASS=true;
                System.out.println("   "+this.PASS);
            }
            else
            {
                System.out.printf(jToggleButton2.getText());
                jToggleButton2.setText("PASS");
                this.PASS=false;
                System.out.println( "  " +this.PASS);

            }
        }
        private void jSlider1MouseClicked(java.awt.event.MouseEvent evt) 
        {

           this.SPEED=jSlider1.getValue();
           System.out.println(SPEED);
        }
        public static void main(String args[])
        {
            Test a=new Test("Test");

        }
        public void paint(Graphics g) // for example but not shwowing in JFrame.
        {
             g.setColor(Color.red);
            RRectangle a=new RRectangle(60,100,250,190,60,60);
           a.drawShape(g);
           System.out.println(a);
        }

    }

1 个答案:

答案 0 :(得分:2)

您的类扩展了JFrame,但是在您的类中,您创建了一个JFrame实例,并将所有组件添加到此JFrame实例中并使此框架可见。永远不会使用或显示扩展类。

因此,您覆盖的paint()方法不执行任何操作。

  1. 你不应该扩展JFrame。
  2. 通过覆盖JPanel的paintComponent()方法完成自定义绘制,然后将面板添加到框架
  3. 我在上面的评论中给了你一个Swing教程的链接。阅读Custom Painting部分的工作示例。