在這裏我的問題,我有2個Jframe,一個用於控制和一個用於向公衆顯示的東西。 我需要在全屏顯示框架,以避免公衆看到它不需要的東西,我需要控制框架在其他屏幕上可用來控制顯示框架。Java全屏jframe與雙顯示器
這是我的問題,今天,我在Ubuntu和雙屏模式下使用這種方式沒有問題,但在Windows 7 buisness 64(不要嘗試其他版本)顯示幀實際上全屏,但是當我點擊顯示框外的任何地方(控制框包括),顯示框自動最小化。我訪客這是一個與Windows最大限度地減少問題最大化的問題,但我真的很想讓它工作。 我知道一個非常骯髒的解決方法,即使顯示框架成爲我的屏幕大小,並將顯示框架定位在良好的座標上。
下面是我做的一個示例代碼。
要使顯示框全屏顯示,請將控制框對焦並按下按鍵F11。或使用菜單(法語)
GameFrame.java相當於顯示幀
package ihm;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class GameFrame extends javax.swing.JFrame {
public GameFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
mainPanel = new javax.swing.JTabbedPane();
gamePanel = new javax.swing.JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
if(backgroundImage != null){
g.drawImage(backgroundImage, 0, 0, this.getWidth(),this.getHeight(),null);
}
}
};
votePanel = new javax.swing.JPanel();
countdownPanel = new javax.swing.JPanel();
bienvenuePanel = new javax.swing.JPanel();
adsPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ImproAffichage");
setAlwaysOnTop(true);
setBackground(new java.awt.Color(255, 179, 0));
setForeground(java.awt.Color.white);
setUndecorated(true);
getContentPane().setLayout(new java.awt.GridLayout(1, 1));
mainPanel.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
gamePanel.setBackground(new java.awt.Color(255, 168, 0));
gamePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(40, 40, 40, 40));
gamePanel.setMinimumSize(new java.awt.Dimension(200, 200));
gamePanel.setLayout(new java.awt.GridLayout(2, 2, 70, 70));
mainPanel.addTab("score", gamePanel);
votePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
votePanel.setLayout(new java.awt.GridLayout(1, 0));
mainPanel.addTab("vote", votePanel);
countdownPanel.setLayout(new java.awt.GridLayout(1, 0));
mainPanel.addTab("decompte", countdownPanel);
bienvenuePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
bienvenuePanel.setLayout(new java.awt.GridLayout(1, 0));
mainPanel.addTab("Bienvenue", bienvenuePanel);
adsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
adsPanel.setLayout(new java.awt.GridLayout(1, 0));
mainPanel.addTab("publicité", adsPanel);
getContentPane().add(mainPanel);
mainPanel.setUI(new BasicTabbedPaneUI() {
protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {
return 0;
}
});
pack();
}
public static void main(String args[]) {
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GameFrame().setVisible(true);
}
});
}
private static GameFrame instance;
private Image backgroundImage;
private Image decompteImage;
private Image bienvenueImage;
// Variables declaration - do not modify
private javax.swing.JPanel adsPanel;
private javax.swing.JPanel bienvenuePanel;
private javax.swing.JPanel countdownPanel;
private javax.swing.JPanel gamePanel;
private javax.swing.JTabbedPane mainPanel;
private javax.swing.JPanel votePanel;
// End of variables declaration
public static GameFrame getInstance() {
if (instance == null) {
instance = new GameFrame();
}
return instance;
}
public void refresh() {
this.validate();
this.repaint();
this.pack();
}
}
ControlFrame.java 相當於controling幀
package ihm;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
public class ControlFrame extends javax.swing.JFrame {
public static ControlFrame getInstance() {
if (instance == null) {
instance = new ControlFrame();
}
return instance;
}
private ControlFrame() {
this.gameFrame = GameFrame.getInstance();
this.gameFrame.setBounds(100, 100, 400, 300);
this.gameFrame.pack();
this.gameFrame.setVisible(true);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
transitionPanel = new javax.swing.JPanel();
jbBienvenue = new javax.swing.JButton();
jbScore = new javax.swing.JButton();
jbVote = new javax.swing.JButton();
jbDecompte = new javax.swing.JButton();
jbPublicite = new javax.swing.JButton();
jpPlayerControl = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
mainMenu = new javax.swing.JMenu();
jmScore = new javax.swing.JMenu();
jmiAddTeam = new javax.swing.JMenuItem();
jmiFullscreen = new javax.swing.JMenuItem();
jmiBackground = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
jmiPointPath = new javax.swing.JMenuItem();
jmOtherImage = new javax.swing.JMenu();
jmiVote = new javax.swing.JMenuItem();
jmiDecompte = new javax.swing.JMenuItem();
jmiBienvenue = new javax.swing.JMenuItem();
jmiAds = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ImproContro");
setAlwaysOnTop(true);
setBounds(new java.awt.Rectangle(0, 0, 200, 300));
setForeground(java.awt.Color.white);
setPreferredSize(new java.awt.Dimension(400, 500));
setResizable(false);
getContentPane().setLayout(new java.awt.BorderLayout(25, 25));
transitionPanel.setLayout(new java.awt.GridLayout(1, 6));
jbBienvenue.setText("Bienvenue");
transitionPanel.add(jbBienvenue);
jbScore.setText("Score");
transitionPanel.add(jbScore);
jbVote.setText("Aux votes");
transitionPanel.add(jbVote);
jbDecompte.setText("Decompte");
transitionPanel.add(jbDecompte);
jbPublicite.setText("Publicité");
transitionPanel.add(jbPublicite);
getContentPane().add(transitionPanel, java.awt.BorderLayout.PAGE_START);
jpPlayerControl.setBackground(new java.awt.Color(255, 220, 0));
jpPlayerControl.setLayout(new java.awt.GridLayout(2, 2, 10, 10));
getContentPane().add(jpPlayerControl, java.awt.BorderLayout.CENTER);
mainMenu.setText("File");
jMenuBar1.add(mainMenu);
jmScore.setText("Score");
jmiAddTeam.setText("ajouter equipe");
jmScore.add(jmiAddTeam);
jmiFullscreen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
jmiFullscreen.setText("Plein Ecran");
jmScore.add(jmiFullscreen);
jmiBackground.setText("image \"fond score\"");
jmScore.add(jmiBackground);
jMenuItem1.setText("couleur \"fond score\"");
jmScore.add(jMenuItem1);
jmiPointPath.setText("Icone de point");
jmScore.add(jmiPointPath);
jMenuBar1.add(jmScore);
jmOtherImage.setText("Autre images");
jmiVote.setText("image \"aux votes\"");
jmOtherImage.add(jmiVote);
jmiDecompte.setText("image \"fond Decompte\"");
jmOtherImage.add(jmiDecompte);
jmiBienvenue.setText("image \"Bienvenue\"");
jmOtherImage.add(jmiBienvenue);
jmiAds.setText("Ajouter image annonce");
jmOtherImage.add(jmiAds);
jMenuBar1.add(jmOtherImage);
setJMenuBar(jMenuBar1);
jmiFullscreen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
jmiFullscreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmiFullscreenActionPerformed(evt);
}
});
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/*
* * Create and display the form
*/ java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ControlFrame.getInstance().setVisible(true);
}
});
}
private void jmiFullscreenActionPerformed(java.awt.event.ActionEvent evt) {
GraphicsDevice gd = this.gameFrame.getGraphicsConfiguration().getDevice();
if (gd.getFullScreenWindow() != null) {
gd.setFullScreenWindow(null);
this.gameFrame.setResizable(true);
this.gameFrame.setPreferredSize(new Dimension(400, 300));
} else {
this.gameFrame.setResizable(false);
this.gameFrame.setPreferredSize(new Dimension(gd.getDefaultConfiguration().getBounds().getSize()));
this.gameFrame.enableInputMethods(false);
gd.setFullScreenWindow(this.gameFrame);
}
refresh();
}
private static ControlFrame instance;
private GameFrame gameFrame;
// Variables declaration - do not modify
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JButton jbBienvenue;
private javax.swing.JButton jbDecompte;
private javax.swing.JButton jbPublicite;
private javax.swing.JButton jbScore;
private javax.swing.JButton jbVote;
private javax.swing.JMenu jmOtherImage;
private javax.swing.JMenu jmScore;
private javax.swing.JMenuItem jmiAddTeam;
private javax.swing.JMenuItem jmiAds;
private javax.swing.JMenuItem jmiBackground;
private javax.swing.JMenuItem jmiBienvenue;
private javax.swing.JMenuItem jmiDecompte;
private javax.swing.JMenuItem jmiFullscreen;
private javax.swing.JMenuItem jmiPointPath;
private javax.swing.JMenuItem jmiVote;
private javax.swing.JPanel jpPlayerControl;
private javax.swing.JMenu mainMenu;
private javax.swing.JPanel transitionPanel;
// End of variables declaration
public void refresh() {
System.out.println("refresh()");
this.gameFrame.refresh();
this.validate();
this.repaint();
}
}
EDIT1:
我嘗試全屏兩個窗口,但是當文件選擇器被調用時,重點是因爲一個全屏窗口的總在最前面的行爲失去了。
編輯2:
我也試圖把一個監聽器圖標化顯示的JFrame,但如果我嘗試改變狀態正常,顯示的JFrame請求焦點,而我的第二幀不能被使用,因爲它失去焦點
編輯3:
如果顯示幀沒有被全屏,那麼它不控制框架上點擊時最小化。
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-02-07 15:54:33
我的代碼有點長,我會試着去解釋它。謝謝 – Kiwy 2012-02-07 16:07:30
我發現了一些想法,比如當它被圖標化時設置我的幀的狀態,但問題是我每次都失去焦點,並且無法再使用我的控制幀 – Kiwy 2012-02-10 23:45:48