2
public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
});
}
}
面板代碼決定了MyPanel的外觀。Java面板不顯示
public class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JTextField txtUsername;
public MyPanel()
{
setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setBackground(SystemColor.control);
panel.setBounds(0, 0, 500, 500);
add(panel);
ImageIcon icon= new ImageIcon("C:/Users/Admin/Desktop/testPic.jpg");
JLabel wlabel = new JLabel(icon);
wlabel.setBounds(20, 10, 400, 222);
panel.add(wlabel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(36, 244, 614, 159);
panel.add(panel_1);
panel_1.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(40, 40, 100, 20);
panel_1.add(lblUsername);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 18));
txtUsername = new JTextField();
txtUsername.setBounds(179, 52, 195, 30);
panel_1.add(txtUsername);
txtUsername.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.setBounds(424, 65, 145, 44);
panel_1.add(btnSubmit);
btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
}
}
爲什麼我沒有看到實際的面板?該代碼編譯並運行,但我沒有看到我的屏幕上的任何東西。
你能告訴我怎麼加呢? –
@MikeJohn:我可能會嗤之以鼻,問你是否看過教程,但是,嚴重的是,你只需要在你的Test實例上調用'add(...)'。是的,請閱讀教程。這就是他們在那裏。 –
@MikeJohn:請在上面的答案中查看我的關鍵教程鏈接。 –