我制作了一个程序,其中左右箭头显示音量JSlider
减小,而向上和向下箭头显示正在更改的频道,即屏幕上显示不同的颜色。我希望只要屏幕稳定 10 秒或更长时间,“Volume is”和“Channel Is”文本以及 JSlider 就会消失,就像在电视机中发生的那样。我正在使用带有 VisualSwing 的 Java Eclipse 作为我的 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();
}
}