![](/img/trans.png)
[英]Xming: why JFrame lost focus when it is setUndecorated(true)?
[英]setUndecorated(true) content of the JFrame not displaying
我正在創建一個帶有java swing的gui,它應該在LCD觸摸屏上實現。 gui將采取所有屏幕,不應該是可關閉的。 所以“setUndecorated(true)”是必不可少的。 問題是,當我使用它時,框架的所有內容都沒有顯示,當我將鼠標移到它們上面時,只顯示按鈕。 然后,當我最大化並最小化幀時,所有內容都會出現。 這是我正在使用的代碼:
public static void main(String[] args) {
//Création de notre JFrame
fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setSize(800, 600);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
support = new JPanel();
///////Adding content to the JPanel()/////////////
fenetre.setContentPane(support);
fenetre.setUndecorated(true);
fenetre.setVisible(true);
}
到目前為止,我發現的唯一解決方案是在設置框架可見后更改框架的大小,如下所示:
fenetre.setSize(801, 601);
fenetre.setSize(800, 600);
但我並沒有真正糾正這個問題,只是繞過它。
(編輯)我對該不完整代碼的批評。 這是一個完整的例子,你可以自己測試。 首先用未修飾的假測試它,然后嘗試未修飾為真,並通過最大化和最小化gui來可視化窗口的內容。 最后,嘗試使用代碼底部的快速修復程序。
編碼 :
package test;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
//Declarations
private static JFrame fenetre;
private static JPanel support;
private static JLabel l1, l2;
private static JComboBox<String> c1, c2;
private static JButton b1, b2;
//List to initiate the comboBoxes
private static String[] listC1 = {"Peter", "Alex", "Jhon", "Bran"};
private static String[] listC2 = {"10", "20", "32", "45"};
public static void main(String[] args) {
//Initialisation of the JFrame
fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setSize(800, 600);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
//Initialisation of the JPanel
support = new JPanel();
support.setBackground(Color.red);
support.setPreferredSize(fenetre.getSize());
//Initialisation of the components
l1 = new JLabel("What's my name");
l2 = new JLabel("What's my age");
c1 = new JComboBox<String>(listC1);
c2 = new JComboBox<String>(listC2);
b1 = new JButton("validate name");
b2 = new JButton("validate age");
//Creating layout
support.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Adding components
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20, 20, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
support.add(l1, gbc);
gbc.gridy = 1;
support.add(c1, gbc);
gbc.insets = new Insets(20, 20, 20, 0);
gbc.gridy = 2;
support.add(b1, gbc);
gbc.insets = new Insets(20, 20, 0, 20);
gbc.gridx = 1;
gbc.gridy = 0;
support.add(l2, gbc);
gbc.gridy = 1;
support.add(c2, gbc);
gbc.insets = new Insets(20, 20, 20, 20);
gbc.gridy = 2;
support.add(b2, gbc);
//Adding Listeners
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("My name is " + c1.getSelectedItem());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("My age is " + c2.getSelectedItem());
}
});
//Adding the JPanel to the content pane of the JFrame
fenetre.setContentPane(support);
//Setting undecorated true (switch between true and false for testing)
fenetre.setUndecorated(false);
fenetre.setVisible(true);
//Quick fix, uncomment to test
//fenetre.setSize(801, 601);
//fenetre.setSize(800, 600);
}
}
您沒有向JPanel
添加任何按鈕,那么您希望它如何顯示它們?
無論哪種方式,這是一個顯示按鈕的簡單示例:
JFrame fenetre = new JFrame();
fenetre.setTitle("IHM");
fenetre.setExtendedState(JFrame.MAXIMIZED_BOTH);
fenetre.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
fenetre.setLocationRelativeTo(null);
JPanel support = new JPanel();
JButton button = new JButton("button");
support.add(button);
fenetre.setContentPane(support);
fenetre.setVisible(true);
請注意,您應該使用適當的布局 。
我還添加了setExtendedState(JFrame.MAXIMIZED_BOTH)
來填充屏幕,並添加了setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
因為您不希望屏幕可以關閉。
問題是,當我使用它時,框架的所有內容都沒有顯示,當我將鼠標移到它們上面時,只顯示按鈕。
在框架可見之前,應將組件添加到面板/框架中。
如果將組件添加到可見框架,則基本邏輯為:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // make sure panel is repainted
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.