これが私のコードです: 私は必要ないと感じたものをいくつか取り出しました。括弧もいくつか取り除いたかもしれませんが、私が持っているコンテンツを表示しようとしているだけです。
プログラムを実行すると、背景画像が描画され(リソースのPNGです)、最初のボタンである1つのボタン(再生ボタン)のみが表示されます-自動選択されます。
実際には 4 つのボタンがありますが、コードには PLAY と INSTRUCTIONS のみを含めました。他の 3 つは、マウスオーバーしない限り表示されません。おそらくペイント方法がおかしいことはわかっていますが、修正方法がわかりません。
別のボタンを選択してウィンドウを最小化してから再度開くと、選択したボタンだけが表示されます。他のボタンを表示するには、マウスオーバーする必要があります。
私super.paint()
も paint メソッドに追加しました。すべてのボタンを取得しましたが、背景は灰色です。問題はsuper.paint()
、すべてのボタンをペイントしg.drawImage(bg, 0, 0, null)
、背景のみをペイントすることであり、他のボタンをペイントせずに一方を実行できないことだと思います。
これが混乱した場合は申し訳ありません。私は Java の初心者で、自分が言おうとしていることを明確にするのに苦労しています。
public class MainMenu extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
//variables
public static Image bg;
public static void main(String[] args) {
MainMenu mainFrame = new MainMenu();
mainFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle ("Zumby");
mainFrame.setLayout(null);
// Loads the background image and stores in bg object.
try {
bg = ImageIO.read(new File("zumby.png"));
} catch (IOException e) {
}
mainFrame.setVisible(true);
}
/**
* Overrides the paint method.
* MONDAY
*/
public void paint(Graphics g)
{
// Draws the img to the BackgroundPanel.
System.out.println("paint");
g.drawImage(bg, 0, 0, null);
}
/**
*/
public MainMenu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setOpaque(false);
setContentPane(contentPane);
contentPane.setLayout(null);
//create buttons
JButton btnPlay = new JButton("PLAY");
btnPlay.setBackground(Color.BLACK);
btnPlay.setForeground(Color.WHITE);
btnPlay.setFont(font);
btnPlay.setBorder(border);
btnPlay.setFocusPainted(false);
//if "Play" is clicked
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent click) {
setVisible(false);
new GamePlay(); //opens up GamePlay window
}
});
btnPlay.setBounds(600, 64, 141, 61);
contentPane.add(btnPlay);
JButton btnInstructions = new JButton("INSTRUCTIONS");
btnInstructions.setBounds(600, 160, 141, 61);
btnInstructions.setBackground(Color.BLACK);
btnInstructions.setFocusPainted(false);
// btnInstructions.setEnabled(true);
contentPane.add(btnInstructions);
repaint();
pack();
setVisible(true);
}
}