我刚刚开始为一项任务开发一个小型GUI,但在启动它时,除了标题之外什么都不可见。
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Property extends JFrame
{
/*private String propertyType;
private String propertyAddress;
private double propertyArea;
private int numOfBedrooms;
private int numOfGarages;
private int numOfToilets;
private String ownerGivenName;
private String ownerSurname;
private String ownerdateOfBirth;*/
JButton PropertySaleButton = new JButton("Add New Property");
JButton PurchaseOfferButton = new JButton("Submit Purchase Offer");
public Property()
{
setLayout (new FlowLayout());
add(PropertySaleButton);
add(PurchaseOfferButton);
}
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new JFrame("CQ Real Estate");
/*Image img = new ImageIcon("icon.gif").getImage();
setIconImage(img);*/
frame.setSize(450, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
如果有人能告诉我我错过了什么,我将不胜感激。
答案 0 :(得分:1)
尝试将代码更改为:
this.getContentPane.add(PropertySaleButton);
this.getContentPane.add(PurchaseOfferButton);
答案 1 :(得分:0)
Property
是保持您的" GUI设计的类#34;但没有使用它。您构建了JFrame
。而是创建一个Property
instace
JFrame frame = new Property();
您需要调整一下代码(添加标题)。