-1

私はプログラムのメインメニューに取り組んでおり、JButton に .png ファイルを適用するのが難しいと感じています。これが私がたどり着いた場所です。

public class Menu extends JFrame implements ActionListener {
public static void main(String[] args) {
    Color b = new Color(0,89,255);
    Color t = new Color(255,0,0);
    Color bttn = new Color (255,255,0);


    final JFrame frame = new JFrame ("Main Menu");
    frame.setVisible(true);
    frame.setSize(1000,750);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setLayout(null);
    frame.setBackground(b);


    JButton start = new JButton ("Start");
    start.setBounds(300, 300, 150, 75);
    start.setForeground(t);


    JButton exit = new JButton ("Exit");
    exit.setBounds(550, 300, 150, 75);
    exit.setBackground(bttn);
    exit.setForeground(t);

急いでいるので、何か答えを残してください

4

1 に答える 1

2
myButton.setIcon(new ImageIcon("/path/to/image.png"));  

を作成した後にやりたいことを行う方法ですJButton。の作成時に画像を追加する場合JButtonは、適切なコンストラクターを使用できます。

JButton myButton = new JButton("What The Heck?", new ImageIcon("/path/to/image.png"));  

nullレイアウトを使用しないでください。LayoutManager代わりにaを使用してください。

于 2013-11-05T15:52:02.943 に答える