I will launch Window2 after I click a button. It's not launched immediately but after 3 seconds. What I want is to show it on the top of the screen. It works if the Window1 is still on the screen but fails if I minimize Window1.
The code is here:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TopTest {
public static void main(String[] args) {
TopTest test = new TopTest();
test.createUI();
}
public void createUI(){
JFrame frame = new JFrame("Window1");
JPanel panel = new JPanel();
JButton button = new JButton("show");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Window window = new Window();
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
window.createUI();
}
});
panel.add(button);
frame.add(panel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class Window {
public void createUI(){
JFrame frame = new JFrame("Window2");
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.add(new MainPanel());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@SuppressWarnings("serial")
class MainPanel extends JPanel{
@Override
public Dimension getPreferredSize() {
// TODO Auto-generated method stub
return new Dimension(800,600);
}
protected void paintComponent(Graphics g){
g.setFont(new Font("Arial", Font.BOLD, 50));
g.drawString("I am Window2", 200, 200);
}
}
}
}
Thanks for your help in advance.