我正在關注一本名爲「JFC Swing教程(第二版)」的書,我幾乎在開始時我已經遵循這個代碼,它應該顯示按鈕和內容窗格中的標籤,但所有即時消息都是空白屏幕。有任何想法嗎? 謝謝。Java Swing空白屏幕而不是窗口顯示
import java.awt.GridLayout;
import javax.swing.*;
public class m extends JFrame
{
void UserFrame()
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hellow You");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new GridLayout(0,1));
//makes label
JLabel label = new JLabel("Sup ");
//adds to the frames content pane a label
frame.getContentPane().add(label);
JButton button = new JButton("Hai");
frame.getContentPane().add(button);
jp.add(button);
jp.add(label);
jp.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
//pack set the window to what it needs AKA to display all components
frame.pack();
//frame.setSize(250, 250);
//shows window
frame.setVisible(true);
}
public static void main(String[] args)
{
final m window = new m();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
window.UserFrame();
}
});
}
}
你的空白屏幕至少是合適的尺寸? –
不,它的最小尺寸可以是 – Definity
我想你錯過了讓第一個窗口可見,比如m.setVisible(true); –