当我运行此代码时:
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public void main(String args[]){
new Menu();
}
Menu(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setSize(500, 300);
frame.setVisible(true);
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
}
public void actionPerformed(ActionEvent e){
}
}
窗口打开但没有显示任何内容......我的标签在哪里?我很失落,因为我以前做了各种各样的GUI,要么我是愚蠢的,要么我不知道!对不起,如果它是一个愚蠢的问题,我只是如此卡住我必须发布这个。
答案 0 :(得分:1)
您的代码适用于我,但我认为它并不适合您,因为您在设置框架可见后添加了一个组件。在
之后致电frame.setVisible(true)
(和setSize
)
frame.add(north, BorderLayout.NORTH);
north.add(logo);
所以你的代码应该是这样的(也正确格式化):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public static void main(String args[]) {
new Menu();
}
public Menu() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
frame.setSize(500, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
}