简体   繁体   中英

In Java my JButton won't center, and graphics to left and right of JButton do not display

When I try to center my Jbutton it does not show on the screen. I think this may be because it is appearing under my other background graphics.This is the code for the button:

JFrame frame = new JFrame(title);
Dimension size = new Dimension(width, height);
frame.setPreferredSize(size);
frame.setMaximumSize(size);
frame.setMinimumSize(size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);

JPanel p = new JPanel();
JButton start = new JButton("Start");
start.setVisible(true);
start.setFont(new Font("TimesNewRoman", Font.BOLD, 25));
p.add(start);
frame.add(p, BorderLayout.SOUTH);


frame.add(game, BorderLayout.CENTER);
frame.setVisible(true); 
game.start();

And here is the code for my graphics:

 `Graphics g = bs.getDrawGraphics();
    g.setColor(new Color(0,100,0));
    g.fillRect(0, 400, 950, 500);
    g.setColor(new Color(135,206,235));
    g.fillRect(0, 0, 950, 500);
    //Cloud display code 
    g.setColor(Color.WHITE);
    g.fillOval(newX, randY, randWidth, randHeight);
    newX++; 
    g.setColor(Color.WHITE);
    g.fillOval(newXi, randYi, randWidthi, randHeighti);     
    newXi++;
    g.setColor(Color.WHITE);
    g.fillOval(newXii, randYii, randWidthii, randHeightii);     
    newXii++;   
    g.setColor(Color.WHITE);
    g.fillOval(newXiii, randYiii, randWidthiii, randHeightiii);     
    newXiii++;  
    g.setColor(Color.WHITE);
    g.fillOval(newXiiii, randYiiii, randWidthiiii, randHeightiiii);     
    newXiiii++; 
    if (newX==width){
        newX=0-301;
    }
    if (newXi==width){
        newXi=0-301;
    }
    if (newXii==width){
        newXii=0-301;
    }
    if (newXiii==width){
        newXiii=0-301;
    }
    if (newXiiii==width){
        newXiiii=0-301;
    }
    g.setColor(Color.GREEN);
    g.fillRect(300, 140, 340, 100);
    g.setFont(new Font("TimesNewRoman", Font.BOLD, 50));
    g.setColor(Color.BLACK);
    g.drawString("Pen Hero", 360, 200 );
    g.dispose();
    bs.show();`
panel.setVisible(false);

这摆脱了你想要消失的空白,还有按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM