Java没有正确显示gui

时间:2014-10-09 01:14:43

标签: java ubuntu user-interface jframe

我有一个简单的gui,当我跑步时它是完美的,但偶尔它不会显示正确。我在JPanel中有一个圆圈。当它正确运行时,它显示在JFrame的中间,就像我想要的那样,但是当它不能正常工作时,圆圈看起来更靠近屏幕的底部。我怎样才能解决这个问题,以便每次都正确显示?我的代码是不正确的(我希望不是!:)),或者它是java中的一些错误。所以这是我的代码:

更新:似乎窗口高度正在变化。

Run.java -------------------------------------------- ------------------------------------

import javax.swing.JFrame;

public class Run {
    public static void main(String args[]) {
        Window w = new Window();
        w.setSize(800, 500);
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setVisible(true);
    }
}

Window.java -------------------------------------------- --------------------------------

import javax.swing.JFrame;

public class Window extends JFrame {
    public Window() {
        super("Wheel");
        Gui g = new Gui();
        add(g);
    }
}

Gui.java -------------------------------------------- ------------------------------------

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Gui extends JPanel {
    private Color wheelColor = new Color(0, 0, 255);

    public Gui() {
        setOpaque(true);
        setBackground(new Color(255, 0, 0));
    }

    public void paintComponent(Graphics g) {
        g.setColor(wheelColor);
        g.fillOval(40,  40, 420, 420);
    }
}

我也在使用Ubuntu,我不知道这是否会影响它。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 确保在Event Dispatching Thread的上下文中创建UI,有关详细信息,请参阅Initial Threads
  2. 确保在进行任何自定义绘画之前在super.paintComponent方法中调用paintComponent,有关详细信息,请参阅Painting in AWT and Swing
  3. 不要依赖魔术数字,窗口的大小是它的内容大小+它的框架装饰。您应该使用getWidthgetHeight来确定Gui面板的大小并覆盖getPreferredSize,以允许窗口确定需要多少空间(最佳)显示它
  4. 例如......

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Run {
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    Window w = new Window();
                    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    w.pack();
                    w.setLocationRelativeTo(null);
                    w.setVisible(true);
                }
            });
        }
    
        public static class Window extends JFrame {
    
            public Window() {
                super("Wheel");
                Gui g = new Gui();
                add(g);
            }
        }
    
        public static class Gui extends JPanel {
    
            private Color wheelColor = new Color(0, 0, 255);
    
            public Gui() {
                setOpaque(true);
                setBackground(new Color(255, 0, 0));
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(440, 440);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(wheelColor);
                int width = getWidth();
                int height = getHeight();
                int x = (width - 420) / 2;
                int y = (height - 420) / 2;
                g.fillOval(x, y, 420, 420);
            }
        }
    }
    

    还要注意在默认API中已经存在的类之后命名您的类,java.awt.Window已经存在,并且不仅会给自己造成混淆,而且会导致其他开发人员混淆;)