问题
I have a JFrame with the JDesktopPane and inside the JDesktopPane, I launch with the constructor a JInternalFrame. (Its a app like the authentification users, with the textbox user and textbox pass)
I lauch the internal like this:
MyInternalFrame internalF = new MyInternalFrame();
desktopPane.add(internalF);
I try with:
internalF.setVisible(true);
internalF.setSelected(true);
desktopPane.getDesktopManager().activateFrame(internal);
desktopPane.setSelectedFrame(internal);
How I can lauch the JInternalFrame and Its selected by default? When I run the aplication, internalframe is like in background, Its not selected, Its not focused.
回答1:
You can make the internal frame visible AFTER the desktop is created and the main frame is visible. In that case the frame will be selected by default.
So, one example of what you can do:
- Create main frame
- Create desktop
- Create internal frame but don't make it visible
- Start thread that sets visible to true on the internal frame, but the thread can wait until the desktop is shown
- Make the main frame visible
- In the thread call internalFrame.setVisible(true) and exit from the thread.
In such case the internal frame will appear on the desktop and it will be selected as you want it.
You might think of other solution without using threads, but writing handlers to the main frame's events. In any case, to make the internal frame visible after it shows, you have to show it AFTER desktop with main frame is displayed.
Here is the example, that you can use:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private Internal internalFrame;
public Main() throws HeadlessException {
super("Internal Frame Test");
setBounds(10, 10, 600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
add(createDesktop(), BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
internalFrame.setVisible(true);
}
});
setVisible(true);
}
private Component createDesktop() {
JDesktopPane d = new JDesktopPane();
internalFrame = new Internal("first");
d.add(internalFrame);
return d;
}
public static class Internal extends JInternalFrame {
private static final long serialVersionUID = 1L;
public Internal(String title) {
super(title);
setBounds(10, 10, 300, 100);
}
}
public static void main(String[] a) {
new Main();
}
}
回答2:
Have a look at the How To Use Internal Frames java tutorial. It gives a nice example and uses the following;
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
来源:https://stackoverflow.com/questions/4088223/how-i-can-selected-by-default-an-internalframe-on-a-jdesktoppane