我有一些需要使用setBounds()的組件,因此我使用setLayout(null).
但是我的一些組件不在視窗(在 Y 軸下方)。我想知道是否有辦法添加滾動條來向下導航視窗以便查看所有剩余的組件。我的視窗截圖如下。
我的視窗影像的輸出:
uj5u.com熱心網友回復:
使用布局生成該 GUI 將很簡單。JTable將顯示串列的組件(考慮到每行/行的兩條資料,看起來非常適合作為JScrollPane. 將滾動窗格CENTER放入BorderLayout. 將紅色標簽放入PAGE_START邊框布局中。然后..哦等等,作業完成了!
這就是它的樣子(使用 aJTextArea而不是表格)。
你可以請張貼此代碼的副本。
嘗試根據上面的說明實作它。如果有問題,請發布您嘗試的最小可重現示例。
uj5u.com熱心網友回復:
由于您將滾動區域中的專案稱為組件,而不是 JTextArea 中的文本,因此請查看以下內容。
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Mainframe {
private JFrame f;
Box box;
JScrollPane scrollPane;
Random rand = new Random();
public static void main(String[] args) {
new Mainframe().go();
}
private void go() {
box = new Box(BoxLayout.Y_AXIS);
JLabel label = new JLabel("Possible Paths and Total Distances");
label.setForeground(Color.RED);
for (int i = 0; i < 200; i ) {
box.add(Box.createRigidArea(new Dimension(0, 2)));// creates space between the components
box.add(new JLabel(i " : " rand.nextInt(10000)));
}
scrollPane = new JScrollPane(box);
Dimension dim = new Dimension(box.getComponent(0).getPreferredSize());
scrollPane.getVerticalScrollBar().setUnitIncrement(dim.height * 2); // adjusts scrolling speed
//scrollPane.getViewport().setBackground(Color.WHITE);
f = new JFrame();
f.getContentPane().add(label, BorderLayout.NORTH);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(640, 480);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/420486.html
標籤:
上一篇:使用標簽文本更新JPanel
下一篇:使用托管標識呼叫APIM端點
