我在Java中並沒有真正與Swing合作。我正在試驗它。我想做一個尺寸不能改變的佈局佈局。我已經看到很多建議使用佈局管理器將多個Jpanel添加到Jframe中的事情。不過,我所看到的所有涉及佈局管理器的教程都表示,它允許用戶調整屏幕大小。我想要的佈局有一個沿着左側的矩形,沿着底部的一個薄矩形,以及佔據剩餘空間的第三個矩形。我嘗試使用絕對佈局,但它只是不想爲我工作。使用JPanels設置佈局
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test extends JFrame {
public Test() {
JPanel rect1 = new JPanel();
rect1.setBounds(101, 650, 900, 50);
rect1.setBackground(Color.RED);
getContentPane().add(rect1);
JPanel rect2 = new JPanel();
rect2.setBounds(0, 650, 100, 1000);
rect2.setBackground(Color.BLUE);
getContentPane().add(rect2);
JPanel rect3 = new JPanel();
rect3.setBounds(101, 700, 900, 950);
rect3.setBackground(Color.GREEN);
getContentPane().add(rect3);
setTitle("TEST");
setSize(1000, 700);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test ex = new Test();
ex.setVisible(true);
}
});
}
}
有人可以幫助我在這種佈局(所有不同的顏色)在Jframe中正確地製造三個Jpanels嗎?
回答我1問題。你什麼時候睡覺 ? – Sage
我是一個18個月的驕傲的父親,我不睡覺...永遠...... – MadProgrammer
現在,我真的感到難過,因爲我不能發佈這樣的問題,我可以upvote並接受答案。 :P。無論如何挑戰接受。 :| :)) – Sage