我正在尝试在我的程序中添加封面。所以我使用卡片布局。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import java.lang.Object;
import java.io.*;
import sun.audio.*;
import java.net.URL;
import javax.sound.sampled.*;
import java.applet.*;
import java.util.*;
public class Game extends JFrame{
public static CardLayout cl = new CardLayout();
public static JPanel dasar = new JPanel(cl);
public Game() {
dasar.add(new Opening(),"Opening");
dasar.add(new level1(),"Level 1");
dasar.add(new level2(),"Level 2");
dasar.add(new level3(),"Level 3");
dasar.add(new level4(),"Level 4");
add(dasar,BorderLayout.CENTER);
/*KeyboardListener listener = new KeyboardListener();
opening();
addKeyListener(listener);*/
}
public static void main(String [] args){
Game G = new Game();
G.setTitle("PuzzleGame");
G.setSize(800,550);
G.setLocationRelativeTo(null);
G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
G.setVisible(true);
}
}
我开了这样的开场课:
class Opening extends JPanel{
public Opening(){
AudioClip AudioOp = Applet.newAudioClip(this.getClass().getResource("opening.wav"));
JLabel atasan = new JLabel(new ImageIcon(getClass().getResource("TOP.png")));
JLabel bawahan = new JLabel(new ImageIcon(getClass().getResource("btm.png")));
JLabel nama = new JLabel(new ImageIcon(getClass().getResource("namakelompok.png")));
JLabel background = new JLabel(new ImageIcon(getClass().getResource("background.jpg")));
JPanel panel1 = new JPanel();
panel1.setLayout(null);
panel1.add(atasan);
panel1.add(bawahan);
panel1.add(nama);
panel1.add(background);
add(panel1);
background.setBounds(0,0,800,550);
atasan.setBounds(100,50,600,100);
bawahan.setBounds(232,450,336,46);
nama.setBounds(194,180,411,205);
}
}
为什么我编译此程序时无法显示任何内容?它就像一个空白的窗口 给我一个建议。对不起,我是初学者。
答案 0 :(得分:-1)
将以下内容移动到构造函数中:
G.setTitle("PuzzleGame");
G.setSize(800,550);
G.setLocationRelativeTo(null);
G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
G.setVisible(true);
所以你的课程和主要内容如下:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import java.lang.Object;
import java.io.*;
import sun.audio.*;
import java.net.URL;
import javax.sound.sampled.*;
import java.applet.*;
import java.util.*;
public class Game extends JFrame{
public static CardLayout cl = new CardLayout();
public static JPanel dasar = new JPanel(cl);
public Game() {
super("PuzzleGame");
this.setSize(800,550);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dasar.add(new Opening(),"Opening");
dasar.add(new level1(),"Level 1");
dasar.add(new level2(),"Level 2");
dasar.add(new level3(),"Level 3");
dasar.add(new level4(),"Level 4");
add(dasar,BorderLayout.CENTER);
/*KeyboardListener listener = new KeyboardListener();
opening();
addKeyListener(listener);*/
this.setVisible(true);
}
public static void main(String [] args){
new Game();
}
}
class Opening extends JPanel{
public Opening(){
AudioClip AudioOp = Applet.newAudioClip(this.getClass().getResource("opening.wav"));
JLabel atasan = new JLabel(new ImageIcon(getClass().getResource("TOP.png")));
JLabel bawahan = new JLabel(new ImageIcon(getClass().getResource("btm.png")));
JLabel nama = new JLabel(new ImageIcon(getClass().getResource("namakelompok.png")));
JLabel background = new JLabel(new ImageIcon(getClass().getResource("background.jpg")));
JPanel panel1 = new JPanel();
panel1.setLayout(null);
panel1.add(atasan);
panel1.add(bawahan);
panel1.add(nama);
panel1.add(background);
add(panel1);
background.setBounds(0,0,800,550);
atasan.setBounds(100,50,600,100);
bawahan.setBounds(232,450,336,46);
nama.setBounds(194,180,411,205);
}
}
希望这有帮助。
让我知道结果