JSlider 在 10 秒后隐藏,就像在电视机中一样

发布于 2024-11-18 07:30:15 字数 7060 浏览 2 评论 0原文

我制作了一个程序,其中左右箭头显示 JSlider 上的音量减小,而向上和向下箭头显示正在更改的通道,即屏幕上显示不同的颜色。我希望只要屏幕稳定 10 秒或更长时间,“音量是”和“频道是”文本以及 JSlider 就会消失,就像电视机中发生的情况一样。我使用 Java Eclipse 和 VisualSwing 作为我的 GUI。我当前的代码是:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


public class TVPanel extends JPanel {

    private static JLabel vollab;
    private int ChannelNo;
    private static final long serialVersionUID = 1L;
    private JLabel jLabel0;
    private int VolumeMax=10;
    private JButton jButton0;
    private JSlider jSlider0;
    private JMenuItem jMenuItem0;
    private JPopupMenu jPopupMenu0;
    private JLabel jLabel1;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public TVPanel() { 
        ChannelNo=0;
        initComponents();
    }

    private void initComponents() {
        setLayout(new GroupLayout());
        add(getJButton0(), new Constraints(new Leading(100, 176, 10, 10), new Leading(39, 72, 10, 10)));
        add(getJSlider0(), new Constraints(new Leading(46, 10, 10), new Leading(162, 10, 10)));
        add(getJLabel1(), new Constraints(new Leading(111, 10, 10), new Leading(129, 12, 12)));
        add(getJLabel0(), new Constraints(new Leading(37, 68, 12, 12), new Leading(129, 12, 12)));
        addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent event) {
                keyKeyTyped(event);
            }

            public void keyPressed(KeyEvent event) {
                keyKeyPressed(event);
            }
        });
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent event) {
                mouseMouseClicked(event);
            }
        });
        setSize(478, 240);
    }

    private JLabel getJLabel1() {
        if (jLabel1 == null) {
            jLabel1 = new JLabel();
            jLabel1.setText("10");
        }
        return jLabel1;
    }

    private JSlider getJSlider0() {
        if (jSlider0 == null) {
            jSlider0 = new JSlider();
            jSlider0.setMajorTickSpacing(1);
            jSlider0.setMaximum(10);
            jSlider0.setPaintLabels(true);
            jSlider0.setPaintTicks(true);
            jSlider0.setValue(10);
            jSlider0.setAlignmentX(1.0f);
            jSlider0.setInheritsPopupMenu(true);
            jSlider0.setValueIsAdjusting(true);
        }
        return jSlider0;
    }

    private JButton getJButton0() {
        if (jButton0 == null) {
            jButton0 = new JButton();
            jButton0.setText("");
            jButton0.setSize(150, 150);
        }
        return jButton0;
    }

    private JLabel getJLabel0() {
        if (jLabel0 == null) {
            jLabel0 = new JLabel();
            jLabel0.setText("Volume Is");
        }
        return jLabel0;
    }
    private static void installLnF() {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if (lnfClassname == null)
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
            System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage());
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     */
    public static void main(String[] args) {
        installLnF();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("TVPanel");
                //JLabel volLab= new JLabel();
                vollab= new JLabel("test");
                frame.getContentPane().add(vollab);
                frame.requestFocus();
                frame.isFocusable();
                vollab.isVisible();
                TVPanel content = new TVPanel();
                content.setPreferredSize(content.getSize());
                frame.add(content, BorderLayout.CENTER);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    //Arrows
    private void keyKeyPressed(KeyEvent event) {
        jLabel0.setVisible(true);
        jLabel1.setVisible(true);
        Color colorarr[]= new Color[] {Color.BLACK,Color.WHITE,Color.BLUE,Color.CYAN,Color.RED,Color.GREEN,Color.GRAY,Color.MAGENTA,Color.ORANGE,Color.YELLOW};
        //int Volume=10;
        //int ChannelNo=10;
            //jLabel0.setText(Integer.toString(event.getKeyCode()));


            if(event.getKeyCode()== 37){
                VolumeMax--;
                jSlider0.setValue(VolumeMax);
                jLabel0.setText("Volume Is");
                jLabel1.setText(Integer.toString(jSlider0.getValue()));
            }
            else if(event.getKeyCode()==38)//UP{
            {
                ChannelNo++;
                for(int i=0; i<ChannelNo;i++){
                    if(i<10){
                    jButton0.setBackground(colorarr[i]);
                    jLabel0.setText("Channel Is");
                    jLabel1.setText(Integer.toString(i+1));

                    }
                }
            }
            else if(event.getKeyCode()==39){
                //RIGHT
                VolumeMax++;
                jSlider0.setValue(VolumeMax);
                jLabel1.setText(Integer.toString(jSlider0.getValue()));
            }
            else if(event.getKeyCode()==40){
                ChannelNo--;
                    if(ChannelNo>0){
                jButton0.setBackground(colorarr[ChannelNo-1]);
                jLabel0.setText("Channel Is");
                jLabel1.setText(Integer.toString(ChannelNo-1));
                }
            }

    this.requestFocus();
    }

    private void mouseMouseClicked(MouseEvent event) {
        //jLabel0.setText("mouse");
    this.requestFocus();
    }

    //Other keys
    private void keyKeyTyped(KeyEvent event) {
        if(event.getKeyCode()==37){
            //jLabel0.setText("uparrow");
            jSlider0.setValue(9);
        }
        jLabel0.setText("keyType");


        this.requestFocus();
    }
}

I have made a program where the right and left arrows show the volume on the JSlider decreasing while the Up and Down arrow show the Channel being changed i.e different colours being shown on screen. I wanted that whenever the screen is stable for 10seconds or more, the "Volume is" and "Channel Is" text along with JSlider should disappear, as it happens in a Television Set. I am using Java Eclipse with VisualSwing as my GUI. My current code is:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


public class TVPanel extends JPanel {

    private static JLabel vollab;
    private int ChannelNo;
    private static final long serialVersionUID = 1L;
    private JLabel jLabel0;
    private int VolumeMax=10;
    private JButton jButton0;
    private JSlider jSlider0;
    private JMenuItem jMenuItem0;
    private JPopupMenu jPopupMenu0;
    private JLabel jLabel1;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public TVPanel() { 
        ChannelNo=0;
        initComponents();
    }

    private void initComponents() {
        setLayout(new GroupLayout());
        add(getJButton0(), new Constraints(new Leading(100, 176, 10, 10), new Leading(39, 72, 10, 10)));
        add(getJSlider0(), new Constraints(new Leading(46, 10, 10), new Leading(162, 10, 10)));
        add(getJLabel1(), new Constraints(new Leading(111, 10, 10), new Leading(129, 12, 12)));
        add(getJLabel0(), new Constraints(new Leading(37, 68, 12, 12), new Leading(129, 12, 12)));
        addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent event) {
                keyKeyTyped(event);
            }

            public void keyPressed(KeyEvent event) {
                keyKeyPressed(event);
            }
        });
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent event) {
                mouseMouseClicked(event);
            }
        });
        setSize(478, 240);
    }

    private JLabel getJLabel1() {
        if (jLabel1 == null) {
            jLabel1 = new JLabel();
            jLabel1.setText("10");
        }
        return jLabel1;
    }

    private JSlider getJSlider0() {
        if (jSlider0 == null) {
            jSlider0 = new JSlider();
            jSlider0.setMajorTickSpacing(1);
            jSlider0.setMaximum(10);
            jSlider0.setPaintLabels(true);
            jSlider0.setPaintTicks(true);
            jSlider0.setValue(10);
            jSlider0.setAlignmentX(1.0f);
            jSlider0.setInheritsPopupMenu(true);
            jSlider0.setValueIsAdjusting(true);
        }
        return jSlider0;
    }

    private JButton getJButton0() {
        if (jButton0 == null) {
            jButton0 = new JButton();
            jButton0.setText("");
            jButton0.setSize(150, 150);
        }
        return jButton0;
    }

    private JLabel getJLabel0() {
        if (jLabel0 == null) {
            jLabel0 = new JLabel();
            jLabel0.setText("Volume Is");
        }
        return jLabel0;
    }
    private static void installLnF() {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if (lnfClassname == null)
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
            System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage());
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     */
    public static void main(String[] args) {
        installLnF();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("TVPanel");
                //JLabel volLab= new JLabel();
                vollab= new JLabel("test");
                frame.getContentPane().add(vollab);
                frame.requestFocus();
                frame.isFocusable();
                vollab.isVisible();
                TVPanel content = new TVPanel();
                content.setPreferredSize(content.getSize());
                frame.add(content, BorderLayout.CENTER);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    //Arrows
    private void keyKeyPressed(KeyEvent event) {
        jLabel0.setVisible(true);
        jLabel1.setVisible(true);
        Color colorarr[]= new Color[] {Color.BLACK,Color.WHITE,Color.BLUE,Color.CYAN,Color.RED,Color.GREEN,Color.GRAY,Color.MAGENTA,Color.ORANGE,Color.YELLOW};
        //int Volume=10;
        //int ChannelNo=10;
            //jLabel0.setText(Integer.toString(event.getKeyCode()));


            if(event.getKeyCode()== 37){
                VolumeMax--;
                jSlider0.setValue(VolumeMax);
                jLabel0.setText("Volume Is");
                jLabel1.setText(Integer.toString(jSlider0.getValue()));
            }
            else if(event.getKeyCode()==38)//UP{
            {
                ChannelNo++;
                for(int i=0; i<ChannelNo;i++){
                    if(i<10){
                    jButton0.setBackground(colorarr[i]);
                    jLabel0.setText("Channel Is");
                    jLabel1.setText(Integer.toString(i+1));

                    }
                }
            }
            else if(event.getKeyCode()==39){
                //RIGHT
                VolumeMax++;
                jSlider0.setValue(VolumeMax);
                jLabel1.setText(Integer.toString(jSlider0.getValue()));
            }
            else if(event.getKeyCode()==40){
                ChannelNo--;
                    if(ChannelNo>0){
                jButton0.setBackground(colorarr[ChannelNo-1]);
                jLabel0.setText("Channel Is");
                jLabel1.setText(Integer.toString(ChannelNo-1));
                }
            }

    this.requestFocus();
    }

    private void mouseMouseClicked(MouseEvent event) {
        //jLabel0.setText("mouse");
    this.requestFocus();
    }

    //Other keys
    private void keyKeyTyped(KeyEvent event) {
        if(event.getKeyCode()==37){
            //jLabel0.setText("uparrow");
            jSlider0.setValue(9);
        }
        jLabel0.setText("keyType");


        this.requestFocus();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2024-11-25 07:30:15
else if(event.getKeyCode()==39){

切勿使用带有幻数的代码。如果需要,定义静态变量。然而,在这种情况下,您不需要这样做,因为它已经为您完成了:

KeyEvent.VK_RIGHT

要让面板消失,您需要启动一个 Swing 计时器,以便在面板显示后 20 秒内触发。然后,每当按键事件或鼠标事件更改面板上的值时,您都可以重新启动计时器。

阅读 Swing 教程中关于如何使用计时器的部分更多信息。

如果这是您正在使用的模式对话框,您甚至可以使用 应用程序不活动来帮助你摆脱困境。

else if(event.getKeyCode()==39){

Never use code with magic numbers. Define static variables if you need to. However, in this case you don't need to since it has already been done for you:

KeyEvent.VK_RIGHT

To have the panel disappear you need to start a Swing Timer to fire in 20 seconds once the panel is displayed. Then whenever a key event or mouse events changes a value on the panel you can restart the Timer.

Read the section from the Swing tutorial on How to Use Timers for more information.

If this is a modal dialog that you are using than maybe you can even use the Application Inacdtivity to help yoo out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文