我在第 103 行收到 java.awt.IllegalComponentStateException
,即
guiFrame.setLocationByPlatform(true)
关于如何解决这个问题有什么想法吗?在实例创建期间,我将其封装在 SwingUtilities.invokeLater
方法中,但它仍然会弹出。
代码粘贴在下面:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CardLayoutExample {
JFrame guiFrame = new JFrame();
CardLayout cards = new CardLayout();
JPanel cardPanel = new JPanel();
JPanel firstPanel = new JPanel();
private JPanel tabsPanel = new JPanel();
static boolean isYoutube = false;
static boolean isGmail = false;
private ImageIcon animatedGif;
private static final String FIRST_PANEL = "First Panel";
/*
* public static void main(String[] args) {
*
* // EventQueue.invokeLater(new Runnable() {
*
* @Override public void run() {
*
* new CardLayoutExample(); } });
*
* }
*/
public CardLayoutExample() {
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Services");
guiFrame.setSize(400, 300);
guiFrame.setLocationRelativeTo(null);
guiFrame.setLayout(new BorderLayout());
guiFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
Border outline = BorderFactory.createLineBorder(Color.black);
tabsPanel.setBorder(outline);
tabsPanel.setLayout(cards);
JLabel services = new JLabel("Please choose from the services below :-");
JButton youtube = new JButton();
youtube.setName("youtube");
ImageIcon youtubeIcon = new ImageIcon("images/youtube_icon.jpg");
youtube.setIcon(youtubeIcon);
youtube.setBorder(outline);
youtube.setRolloverEnabled(true);
animatedGif = new ImageIcon("images/plnttm.gif");
youtube.setRolloverIcon(animatedGif);
youtube.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
isGmail = false;
isYoutube = true;
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardPanel.removeAll();
cardPanel.add(createContentYoutube(),
BorderLayout.NORTH);
guiFrame.setSize(800, 600);
guiFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
guiFrame.setLocationByPlatform(true);
guiFrame.setVisible(true);
}
});
}
});
JButton gmail = new JButton();
gmail.setName("gmail");
ImageIcon gmailIcon = new ImageIcon("images/Gmail_Icon.png");
gmail.setIcon(gmailIcon);
gmail.setBorder(outline);
gmail.setRolloverEnabled(true);
animatedGif = new ImageIcon("images/plnttm.gif");
gmail.setRolloverIcon(animatedGif);
gmail.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
isGmail = true;
isYoutube = false;
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardPanel.removeAll();
cardPanel.add(createContentGmail(), BorderLayout.NORTH);
guiFrame.setSize(800, 600);
guiFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
guiFrame.setLocationByPlatform(true);
guiFrame.setVisible(true);
}
});
}
});
Box box = Box.createVerticalBox();
box.add(services);
box.add(Box.createVerticalStrut(20));
box.add(youtube);
box.add(Box.createVerticalStrut(20));
box.add(gmail);
firstPanel.setBackground(Color.white);
firstPanel.setLayout(new FlowLayout());
firstPanel.add(box);
tabsPanel.add(firstPanel, FIRST_PANEL);
cardPanel.setLayout(cards);
cards.show(cardPanel, "Web Browser");
guiFrame.setVisible(true);
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.add(tabsPanel, BorderLayout.WEST);
guiFrame.add(cardPanel, BorderLayout.CENTER);
guiFrame.setVisible(true);
}
public static JComponent createContentYoutube() {
JPanel contentPane = new JPanel(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory
.createTitledBorder("Web Browser"));
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://youtube.com");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
contentPane.add(webBrowserPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
contentPane.add(buttonPanel, BorderLayout.SOUTH);
return contentPane;
}
public static JComponent createContentGmail() {
JPanel contentPane = new JPanel(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory
.createTitledBorder("Web Browser"));
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://gmail.com");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
contentPane.add(webBrowserPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
contentPane.add(buttonPanel, BorderLayout.SOUTH);
return contentPane;
}
}
最佳答案
这是expected exception在调用 setVisible(true)
之后调用 setLocationByPlatform(true)
时。 单次调用 setVisible()
就足够了。
关于java.awt.IllegalComponentStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742890/