Code:phương thức setPreferredSize không hoạt động
import java.awt.Dimension;
import javax.swing.*;
public class Game extends JFrame {
private static final long serialVersionUID = -7919358146481096788L;
JPanel a = new JPanel();
public static void main(String[] args) {
new Game();
}
private Game() {
setTitle("Insert name of game here");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setPreferredSize(new Dimension(600, 600));
add(a);
pack();
setVisible(true);
}
}
Vì vậy, tôi thiết lập kích thước ưa thích của JPanel
-600 600 và đóng gói các khung hình, nhưng kích thước của khung vẫn 0 là bằng 0.
Tại sao điều này và làm thế nào để sửa chữa nó?
Xem: [? Tại sao nó tán thành sử dụng một cách bố trí null trong Swing] (http://stackoverflow.com/questions/6592468/why-is- it-frowned-upon-to-use-a-null-bố trí-trong-swing) –