【问题标题】:My JPanel is appearing gray我的 JPanel 显示为灰色
【发布时间】:2017-12-09 02:40:15
【问题描述】:

我正在尝试制作带有按钮的标题屏幕,并使用JFrame 对象将背景作为图片 (BufferedImage)。但是,当我将鼠标悬停在按钮上时,屏幕显示为灰色并且仅更新部分屏幕。

JFrame 类:

//import statements
public class StartGame extends JFrame{
    private JLabel title=new JLabel("Title");
    private JButton start=new JButton("New Game");
    private JButton quit=new JButton("Quit");
    public static final int WIDTH = 1280;
    public static final int HEIGHT = 720;

    public static void main(String [] args){
        StartGame g1=new StartGame();
        g1.play();
    }

    public StartGame(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle( "Title" );
        setResizable( false );
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize( WIDTH, HEIGHT );
        setPreferredSize( new Dimension(WIDTH, HEIGHT) );
        setLocation(dim.width/2-getSize().width/2, dim.height/2-getSize().height/2);
        setUndecorated(true);
        setVisible(true);

        StartGamePanel sgp = new StartGamePanel();
        sgp.setSize( StartGame.WIDTH,StartGame.HEIGHT );
        sgp.setLayout( null );
        sgp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        sgp.setVisible(true);

        start.setBackground(Color.BLACK);
        start.setBorder(BorderFactory.createEmptyBorder());
        start.setBounds( WIDTH/2-105, HEIGHT/9*6, 210, 40 );
        start.setForeground(Color.WHITE);

        quit.setBackground(Color.BLACK);   
        quit.setBorder(BorderFactory.createEmptyBorder());
        quit.setBounds( WIDTH/2-75, HEIGHT/9*8, 150, 40 );
        quit.setForeground(Color.WHITE);

        title.setBounds( WIDTH/2-216, HEIGHT/8*2, 432, 100 );
        title.setForeground(Color.WHITE);

        ButtonListener spy = new ButtonListener();
        start.addActionListener( spy );
        quit.addActionListener( spy );

        sgp.add(title);
        sgp.add(start);
        sgp.add(quit);
        getContentPane().add( sgp );
        pack();
    }

    private void play(){
        EventQueue.invokeLater(new Runnable() {
                public void run() {
                    setVisible(true);
                }
            });
    }
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(quit)) {
                System.exit(0);
            } else if (e.getSource().equals(start)){
                //start
            dispose();
            }
        }
    }
}

还有JPanel 类:

//import statements
public class StartGamePanel extends JPanel{
    private BufferedImage bg;
    public StartGamePanel(){
        setBackground( Color.WHITE );
        setSize( StartGame.WIDTH, StartGame.HEIGHT );
        setPreferredSize( new Dimension(StartGame.WIDTH, StartGame.HEIGHT) );
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setLayout( null );

        try {
            bg = ImageIO.read(new File("pics/SplashScreen.png"));
        } catch (IOException e) {
            System.out.println ( "No splash screen image in StartGamePanel" );
        }
    }

    @Override
    public void paintComponent( Graphics g ) {
        super.paintComponent( g );

        g.drawImage( bg, 0, 0, null );
    }

    private void play(){
        EventQueue.invokeLater(new Runnable() {
                public void run() {
                    setVisible(true);
                }
            });
    }
}

我相当确定问题出在 JPanel 类中,因为我可以将它替换为来自我程序不同部分的另一个 JPanel 类,并且它有效。

【问题讨论】:

    标签: java graphics jframe jpanel


    【解决方案1】:

    不要把setVisible(true);放在开头,否则你需要在后面加上repaint();。

    因为它有时间更新底部的那些添加,这就是为什么你的程序是灰色或Java应用程序的默认颜色。

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 2020-11-26
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多