是否可以使用keylistener显示以前隐藏的JFrame

时间:2010-05-16 17:00:22

标签: java swing hide visibility show

这是我的代码,我基本上只是为最常见的监听器做了一个测试器,我可能会在以后的项目中使用,主要问题是在底层的keylistener,我试图重新显示框架但是我认为不能这样做,请帮忙 ps:不知道为什么进口没有正确显示。

package newpackage;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class NewClass1 extends JFrame {


private JLabel item1,infomouse,infoclicks,infoKeys,writehere;
private JButton button1,button2,button3;
private JTextArea text1,status,KeyStatus;
private JTextField text2,text3,mouse,clicks,test;
private JSeparator sep1;
private int clicknumber;

public NewClass1() {
    super("Listener Tests");
    setLayout(null);
    sep1 = new JSeparator();

    button1 = new JButton("Button1");
    button2 = new JButton("Button2");
    button3 = new JButton("Button3");
    item1 = new JLabel("Button Status :");
    infomouse = new JLabel("Mouse Status :");
    infoclicks = new JLabel("Nº of clicks :");
    infoKeys = new JLabel("Keyboard status:");
    writehere = new JLabel("Write here: ");


    text1 = new JTextArea();
    text2 = new JTextField(20);
    text3 = new JTextField(20);
    status = new JTextArea();
    mouse  = new JTextField(20);
    clicks = new JTextField(4);
    KeyStatus = new JTextArea();
    test = new JTextField(3);
    clicks.setText(String.valueOf(clicknumber));

    text1.setEditable(true);
    text2.setEditable(false);
    text3.setEditable(false);
    status.setEditable(false);
    mouse.setEditable(false);
    clicks.setEditable(false);
    KeyStatus.setEditable(false);

    text1.setBounds(135, 310, 150, 20);
    text2.setBounds(135, 330, 150, 20);
    text3.setBounds(135, 350, 150, 20);
    status.setBounds(15, 20, 240, 20);
    infomouse.setBounds(5,45,120,20);
    infoKeys.setBounds(5,90,120,20);
    KeyStatus.setBounds(15,115,240,85);
    test.setBounds(15,225,240,20);
    mouse.setBounds(15,70,100,20);
    infoclicks.setBounds(195, 45, 140, 20);
    clicks.setBounds(195, 70, 60, 20);

    item1.setBounds(5, 0, 120, 20);
    button1.setBounds(10, 310, 115, 20);
    button2.setBounds(10, 330, 115, 20);
    button3.setBounds(10, 350, 115, 20);
    sep1.setBounds(5, 305, 285, 10);

    sep1.setBackground(Color.BLACK);
    status.setBackground(Color.LIGHT_GRAY);

    button1.addActionListener(new button1list());
    button2.addActionListener(new button1list());
    button3.addActionListener(new button1list());

    button1.addMouseListener(new MouseList());
    button2.addMouseListener(new MouseList());
    button3.addMouseListener(new MouseList());
    getContentPane().addMouseListener(new MouseList());
    test.addKeyListener(new KeyList());
    this.addKeyListener(new KeyList());
    test.requestFocus();

    add(item1);
    add(button1);
    add(button2);
    add(button3);
    add(text1);
    add(text2);
    add(text3);
    add(status);
    add(infomouse);
    add(mouse);
    add(infoclicks);
    add(clicks);
    add(infoKeys);
    add(KeyStatus);
    add(test);
    add(sep1);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){System.out.println("Error");}
    SwingUtilities.updateComponentTreeUI(this);
    setSize(300, 400);
    setResizable(false);
    setVisible(true);
    test.setFocusable(true);
    test.setFocusTraversalKeysEnabled(false);
    setLocationRelativeTo(null);
}

public class button1list implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        String buttonpressed = e.getActionCommand();
        if (buttonpressed.equals("Button1")) {
            text1.setText("just");
        } else if (buttonpressed.equals("Button2")) {
            text2.setText(text2.getText()+"testing ");
        } else if (buttonpressed.equals("Button3")) {
            text3.setText("this");
        } 
    }
}
    public class MouseList implements MouseListener{
        public void mouseEntered(MouseEvent e){
        if(e.getSource()==button1){
                status.setText("button 1 hovered");
            }
        else if(e.getSource()==button2){
                status.setText("button 2 hovered");
            }
        else if(e.getSource()==button3){
                status.setText("button 3 hovered");
            }
        }
        public void mouseExited(MouseEvent e){
                status.setText("");
        }
        public void mouseReleased(MouseEvent e){
            if(!status.getText().equals("")){
                status.replaceRange("", 0, 22);
            }
        }
        public void mousePressed(MouseEvent e){
            if(e.getSource()==button1){
                status.setText("button 1 being pressed");
            }
        else if(e.getSource()==button2){
                status.setText("button 2 being pressed");
            }
        else if(e.getSource()==button3){
                status.setText("button 3 being pressed");

            }
        }
        public void mouseClicked(MouseEvent e){
            clicknumber++;
            mouse.setText("mouse working");
            clicks.setText(String.valueOf(clicknumber));
      }
 }
    public class KeyList implements KeyListener{
        public void keyReleased(KeyEvent e){}
        public void keyPressed(KeyEvent e){
            KeyStatus.setText("");
            test.setText("");
            String full = e.paramString();
            String [] temp = null;
            temp = full.split(",");
            for(int i=0; i<7 ;i++){
            KeyStatus.append(temp[i] + "\n");
            }
            if(e.getKeyChar()=='h'){setVisible(false);
                test.requestFocus();
            }
            if(e.getKeyChar()=='s'){setVisible(true);}
        }
        public void keyTyped(KeyEvent e){}
    }

}

3 个答案:

答案 0 :(得分:2)

可见组件可以具有键盘焦点,并接收键盘和其他输入事件,但隐形组件不能具有焦点,也不能接收输入事件。当您使框架不可见时,它会停止接收输入事件,就像所有孩子一样,包括您要添加test的{​​{1}}组件。

要使其工作,您必须拥有一个可见组件,并将键盘事件转发到您的隐形框架。或者更好的是,从已经可见的组件中调用KeyListener

或者,您可以找到一些其他方式来触发显示框架。例如,带有上下文菜单的系统托盘组件是隐藏和显示应用程序框架的常见模式(对于某些用户来说,在此过程中引起了很多懊恼!)

答案 1 :(得分:0)

作为一般规则,您不应使用KeyListener。当只有AWT出现时,那是旧的做事方式。

使用Swing时,您应该使用Key Bindings。绑定不会解决您的隐形组件和框架的问题,但这是所有Swing组件使用的方法。

答案 2 :(得分:0)

是的,但是您需要使用带有本机库的外部Java库。搜索“JIntellitype”。但这只适用于Windows。如果要在linux上使用此功能,请使用“JXGrabKey”