我已经构建了一个UI但是当我尝试选择MenuBar的选项时,它被绘制得很糟糕。
这是我的主要内容 Launcher.java
public class Launcher
{
public static void main(String[] args) throws IOException
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Gui();
}
});
}
}
这是管理UI的类 GUI.java
public class Gui extends JFrame
{
private JMenuBar menuBar=new JMenuBar();
private JMenu info=new JMenu("Info");
private JMenu help=new JMenu("Aiuto");
private JMenu tool=new JMenu("Strumenti");
private JMenuItem update=new JMenuItem("Controlla aggiornamenti");
private JMenuItem resetTable=new JMenuItem("Reset tabella");
private JMenuItem resetTextArea=new JMenuItem("Reset area di testo");
private JMenuItem forum=new JMenuItem("Vai al forum");
private JMenuItem guide=new JMenuItem("Guida");
private JMenuItem information=new JMenuItem("Informazioni");
private JMenuItem trouble=new JMenuItem("Segnala un problema");
private final int GUI_WIDTH=1270;
private final int GUI_HEIGHT=700;
/**
* Costruttore dell'interfaccia principale
*/
public Gui()
{
setMenuBar();
setTitle("WP 0.2");
setGUI();
}
/**
* Metodo che setta l'interfaccia grafica principale
*/
private void setGUI()
{
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(GUI_WIDTH,GUI_HEIGHT);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int width=(int) screenSize.getWidth();
int height=(int) screenSize.getHeight();
setLocation((width-GUI_WIDTH)/2,(height-GUI_HEIGHT)/2);
setResizable(true);
}
/**
* Metodo che aggiunge i vari componenti al menubar
*/
public void setMenuBar()
{
setJMenuBar(menuBar); // Creazione del Menu
menuBar.add(info);
menuBar.add(tool);
menuBar.add(help);
info.add(update);
tool.add(resetTable);
tool.add(resetTextArea);
help.add(forum);
help.add(trouble);
}
}
错误的例子:
问题是因为Swing不是线程安全的吗?
答案 0 :(得分:0)
此外,setVisible(true)
需要追求一切。当您将JFrame设置为可见时,它会绘制当前组件。如果您在setVisible之后调整大小并移动JFrame,则需要重绘JFrame。我通常把setvisible放在我的构造函数的末尾。
private void setGUI()
{
//setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(GUI_WIDTH,GUI_HEIGHT);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int width=(int) screenSize.getWidth();
int height=(int) screenSize.getHeight();
setLocation((width-GUI_WIDTH)/2,(height-GUI_HEIGHT)/2);
setResizable(true);
setVisible(true);
}