所以我一直在尋找其他解決方案來解決這個問題,但似乎他們都沒有幫助。有人能幫我嗎?java - 在位置設置按鈕和TextAreas
代碼:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container cont = frame.getContentPane();
JPanel buttonpanel = new JPanel();
JButton[] button = new JButton[12];
JTextArea score1, score2;
score1 = new JTextArea(100, 200);
score2 = new JTextArea(100, 200);
frame.setSize(800, 600);
frame.setVisible(true);
score1.setLayout(null);
score1.setBackground(Color.BLUE);
score2.setLayout(null);
score2.setBackground(Color.RED);
score1.setLocation(0, 100);
score2.setLocation(700, 100);
frame.add(score1);
for (int i = 0; i < button.length/2; i++) {
button[i].setBounds(100 * (i + 1), 100, 100, 100);
button[i].setBackground(Color.GRAY);
buttonpanel.add(button[i]);
}
frame.add(buttonpanel);
frame.add(score2);
// frame.add(panel);
}
這給了我完全窗口藍色。
此外,如果您嘗試配置GUI,請顯示一張圖片,顯示您想要實現的內容。 99%的時間,最好的解決方案是不要像你這樣做。不要使用絕對定位,因爲在99%的時間內製作GUI是一種可怕的方式。 –
雖然null佈局和'setBounds()'對於Swing新手來說似乎是創建複雜GUI的最簡單和最好的方式,但是更多的Swing GUI會創建使用它們時遇到的更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。 –