hope to get andy indications and reply, many thanks to you!Now, as for the UIs we want to get, we require that when users press one
button on UI1 with "Enter" in keyboard(don't use mouse), another UI2 would
appear in front. Moreover, when users press "ESC" in keyboard, it can also
return back from UI2 to UI1.However, we now have some following problems as below:
1. Now we use the following way for ui programming, however, we always find
that UI would be dead without any response for any key press. What's wrong
with the following porgramming style and way:[code]
//UI1 class
...
dispse();
UI2 ui2 = new UI2();
ui2.setVisible(true);
...//UI2 class
...
dispse();
UI1 ui1 = new UI1();
ui1.setVisible(true);
[/code]2. as for our requirement, could you please provide one similar sample for
reference.3. Many thanks for you! Hope it would not cost your much time!
best wishes for you!Jerry
button on UI1 with "Enter" in keyboard(don't use mouse), another UI2 would
appear in front. Moreover, when users press "ESC" in keyboard, it can also
return back from UI2 to UI1.However, we now have some following problems as below:
1. Now we use the following way for ui programming, however, we always find
that UI would be dead without any response for any key press. What's wrong
with the following porgramming style and way:[code]
//UI1 class
...
dispse();
UI2 ui2 = new UI2();
ui2.setVisible(true);
...//UI2 class
...
dispse();
UI1 ui1 = new UI1();
ui1.setVisible(true);
[/code]2. as for our requirement, could you please provide one similar sample for
reference.3. Many thanks for you! Hope it would not cost your much time!
best wishes for you!Jerry
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import javax.swing.JButton;
import javax.swing.JFrame;public class ButtonTest implements ActionListener,KeyListener
{
private static final long serialVersionUID = 1L; private static final int width = 200; private static final int height = 100;
private JFrame frame;
private class DialogTest extends JFrame implements KeyListener
{
private static final long serialVersionUID = 1L; public DialogTest()
{
super("UI2");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((dimension.width + width) / 2, (dimension.height + height) / 2);
setSize(width, height);
JButton button = new JButton("Test");
JButton button2 = new JButton("Test2");
add(button,"North");
add(button2,"South");
button.addKeyListener(this);
button2.addKeyListener(this);
pack();
setVisible(true);
} public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
dispose();
frame.setVisible(true);
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
public ButtonTest()
{
frame = new JFrame("UI1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(width, height);
frame.setLocation((dimension.width - width) / 2, (dimension.height - height) / 2);
JButton button = new JButton("Test");
JButton button2 = new JButton("Test2");
button.addActionListener(this);
button.addKeyListener(this);
button2.addActionListener(this);
button2.addKeyListener(this);
frame.getContentPane().add(button2,"South");
frame.getContentPane().add(button,"North");
frame.pack();
frame.setVisible(true);
button.requestFocusInWindow();
} public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JButton)
{
// JOptionPane.showMessageDialog(null, "This is a button test!", "Test", JOptionPane.INFORMATION_MESSAGE);
new DialogTest();
frame.setVisible(false);
}
} public static void main(String[] args)
{
new ButtonTest();
} public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
new DialogTest();
frame.setVisible(false);
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
pack();
setVisible(true);
後加上:
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(frame != null && !frame.isVisible())
{
frame.dispose();
System.exit(0);
}
}
});