[英]JFrame contents not fully displayed
我正在做一個JFrame
,其中的標簽是從數據庫中動態添加的。 在這種情況下,有時將要顯示3個數據,有時將要顯示5個數據。
該程序會顯示內容,但只會顯示適合屏幕的數據。 這意味着當我向下滾動時,什么也看不到。 有人知道我哪里出問題了嗎?
public class Trip extends JFrame implements ActionListener {
ConnectionDB database = new ConnectionDB();
public Trip() throws SQLException
{
super("Trip");
Container c = getContentPane();
c.setLayout(new BorderLayout());
Definition a =new Definition();
int NoOfAS = a.travelTime(User.UserID);
Panel p = new Panel(new GridLayout(1,2));
Panel p1 = new Panel(new GridLayout(NoOfAS,1));
Label title = new Label("Itinerary");
title.setFont(new Font("Serif", Font.BOLD, 48));
c.add(title,"North");
JScrollPane scroll = new JScrollPane(p);
c.add(scroll,"Center");
for(int i=0;i<NoOfAS;i++)
{
String ASNAME = database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);
Label no = new Label("No:"+ (i+1) +" "+ASNAME);
no.setFont(new Font("Times New Noman",Font.BOLD,20));
Label descp = new Label(database.retrieveAS_Des(ASNAME,"AS_Description"));
Label SugTime = new Label("Suggested Time:"+database.retrieveAS_Des(ASNAME,"AS_Time"));
Label bus = new Label("Bus:"+database.retrieveAS_Des(ASNAME,"AS_Transport"));
Label fee = new Label("Fees:"+database.retrieveAS_Des(ASNAME,"AS_Price"));
Label line = new Label("---------------------------------------------------");
line.setForeground (Color.red);
Panel p2 = new Panel(new GridLayout(6,1));
p2.add(no);
p2.add(descp);
p2.add(SugTime);
p2.add(bus);
p2.add(fee);
p2.add(line);
p1.add(p2);
p.add(p1);
}
// setSize(900,1700);
pack();
show();
}
public static void main(String args[]) throws SQLException
{
StdPlan app = new StdPlan( );
app.addWindowListener(new WindowAdapter( ){});
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
突然出現在我眼前的問題是您正在混合重量級和輕量級的組件(將Label
& Panel
到JScrollPane
)-這很少是一個好主意。
請改用JLabel
和JPanel
。
在for-next-loop
內部for-next-loop
您會反復向p
添加p1
,實際上並沒有實現任何效果,實際上, p
可能應該使用BorderLayout
,因為它只包含一個組件。
我還建議不要直接從JFrame
擴展它,它在服務器上限制了UI組件的可重用性。 最好使用JPanel
作為基礎並將其添加到JFrame
的實例(或您可能要使用的任何其他容器)中。
通常不建議直接實現偵聽器接口,因為它傾向於公開通常不希望人們調用的公共方法。 最好改用內部或匿名類-恕我直言
問題是您正在混合awt(重量級)和swing(輕量級)組件。 嘗試僅使用JLabel
, JPanel
等。
嘗試一下,您可以檢查下面的代碼,並且在您的代碼僅顯示當前視圖中的內容時,它可以正確顯示:
public class Test extends JFrame implements ActionListener {
//ConnectionDB database = new ConnectionDB();
public Test() throws SQLException
{
super("Trip");
Container c = getContentPane();
c.setLayout(new BorderLayout());
// Definition a =new Definition();
int NoOfAS = 15;//a.travelTime(User.UserID);
JPanel p = new JPanel(/*new GridLayout(1,2)*/);
JPanel p1 = new JPanel(new GridLayout(NoOfAS,1));
JLabel title = new JLabel("Itinerary");
title.setFont(new Font("Serif", Font.BOLD, 48));
c.add(title,BorderLayout.NORTH);
JScrollPane scroll = new JScrollPane(p);
c.add(scroll,BorderLayout.CENTER);
for(int i=0;i<NoOfAS;i++)
{
String ASNAME = i + "";//database.retrieveASNameItinerary(User.UserID,"AS_Name",i+1);
JLabel no = new JLabel("No:"+ (i+1) +" "+ASNAME);
no.setFont(new Font("Times New Noman",Font.BOLD,20));
JLabel descp = new JLabel(ASNAME);
JLabel SugTime = new JLabel(ASNAME);
JLabel bus = new JLabel(ASNAME);
JLabel fee = new JLabel(ASNAME);
JLabel line = new JLabel("---------------------------------------------------");
line.setForeground (Color.red);
JPanel p2 = new JPanel(new GridLayout(6,1));
p2.add(no);
p2.add(descp);
p2.add(SugTime);
p2.add(bus);
p2.add(fee);
p2.add(line);
p1.add(p2);
p.add(p1);
}
// setSize(900,1700);
pack();
show();
}
public static void main(String args[]) throws SQLException
{
Test app = new Test( );
app.addWindowListener(new WindowAdapter( ){});
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.