【问题标题】:JMenuItems are bad draw [duplicate]JMenuItems 画不好[重复]
【发布时间】:2015-02-16 00:25:30
【问题描述】:

我已经构建了一个 UI,但是当我尝试选择 MenuBar 的一个选项时,它的绘制很糟糕。

这是我的主要 启动器.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 不是线程安全的而出现问题?

【问题讨论】:

  • 您能否添加相关代码,即您实际构建 GUI 的位置?
  • 是的,我尝试添加它,但论坛建议我需要更多细节来添加它
  • 可能是你的显卡设置。你的电脑是什么显卡?
  • 集成英特尔高清显卡 4000 和 Nvidia GeForce 720M 专用
  • 就是这样。在 Java、Swing 和 NVidia geforce 上搜索此站点。例如,请查看these hits

标签: java jframe jmenuitem jmenubar


【解决方案1】:

另外,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);
}

【讨论】:

  • 感谢您的建议,但它不能解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多