我目前正在嘗試使用四個面板製作程序。使用GridLayout將三個面板組合在JFrame的左側,最後一個面板佔據屏幕的右側。理想情況下,左側面板佔用JFrame左側的2/3,最後三分之一將被第四個面板佔用。我試圖用很多方式做到這一點,但無法弄清楚。當前顯示將屏幕分成兩半,並顯示黃色,紅色,藍色,但只有540個像素長,而綠色覆蓋左側面板的其餘部分。如何使用Java佈局管理器以我想要的方式顯示面板?
這有一個代表最後一個面板的綠色矩形,它與黃色紅色和藍色矩形重疊,其長度應爲720像素。我已經通過重新調整JFrame的大小,直到完成工作,完成了正確顯示我想要的方式。通過重新調整直到矩形排列,我設法使它看起來應該如此。我移動了JFrame的右側,直到兩個相應的面板排成一行,沒有空白或重疊,不幸的是,爲了做到這一點,一邊有大量的空白空間。
這裏是我用來做這個顯示的代碼。我是一個完美的新手,可能完全做錯了。我沒有設置使用GridLayout,我只是想使這個工作正常。
import java.awt.*;
import javax.swing.*;
class testJPanel1 extends JPanel //topLeft
{
int x,y;
//JPanel Tjp1;
public testJPanel1()
{
x=720;
y=250;
setSize(x,y);
setVisible(true);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.fillOval(x,y,50,50);
g.setColor(Color.yellow);
g.fillRect(0, 0, x, y);
g.setColor(Color.black);
g.drawString("1",x,y);
System.out.println("Hey 1 works");
}
}
class testJPanel2 extends JPanel //mid left
{
int x,y;
public testJPanel2()
{
x=720;
y=260;
setVisible(true);
setSize(x,y);
}
@Override
public void paintComponent(Graphics g)
{super.paintComponent(g);
//g.fillOval(x,y,50,50);
g.setColor(Color.red);
g.fillRect(0, 0, x, y);
g.setColor(Color.black);
g.drawString("2",x,y);
System.out.println("Hey 2 works");
}
}
class testJPanel3 extends JPanel //bot left
{
int x,y;
int boundsx;
int boundsy;
public testJPanel3()
{
x=720;
y=250;
boundsx=200;
boundsy=200;
setVisible(true);
setSize(x,y);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.fillOval(x,y,50,50);
g.setColor(Color.blue);
g.fillRect(0, 0, x, y);
g.setColor(Color.black);
g.drawString("3",x,y);
System.out.println("Hey 3 works");
// g.drawRect(0,0,boundsx,boundsy);
}
}
class testJPanel4 extends JPanel //BIG one on the right
{
int x,y;
int boundsx;
int boundsy;
public testJPanel4()
{
x=360;
y=760;
boundsx=200;
boundsy=200;
setVisible(true);
setSize(x,y);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.green);
//g.fillOval(x,y,50,50);
g.fillRect(0,0,x,y);
g.setColor(Color.black);
g.drawString("4",x,y);
System.out.println("Hey 4 works");
//g.drawRect(0,0,boundsx,boundsy);
}
}
class Left_Panel extends JPanel //Combines 1 and 2 and 3
{
testJPanel1 tjp1;
testJPanel2 tjp2;
testJPanel3 tjp3;
public Left_Panel()
{
setLayout(new GridLayout(3,1));
tjp1= new testJPanel1();
tjp2= new testJPanel2();
tjp3= new testJPanel3();
add(tjp1);
add(tjp2);
add(tjp3);
}
}
class combo_Panel extends JPanel //combines left and right panels together
{
Left_Panel lp;
testJPanel4 tjp4;
//GOAL IS TO MAKE THIS PANEL lp is 720,760 pixels and tjp4 360,760...still doesnt work
public combo_Panel()
{
setLayout(new GridLayout(1,2)); //HOW TO make this work the way i want it or use something else i want
lp= new Left_Panel();
lp.setSize(720,760);
tjp4= new testJPanel4();
tjp4.setSize(360,760);
add(lp);
add(tjp4);
}
}
class Paint_Window extends JFrame
{
combo_Panel combo;
Paint_Window(String title)
{
super(title);
//setLayout(new BoxLayout(combo,2));
combo = new combo_Panel();
setBounds(new Rectangle(1080,760));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setLayout(new FlowLayout());
add(combo);
//add(tjp5);
//tjp1.repaint();
}
}
public class testJPanel
{
public static void main(String args[])
{
Paint_Window window = new Paint_Window("Make your choice");
}
首先,不要擰上一個組件的x/y值,他們已經有一個由佈局管理器定義的位置概念 – MadProgrammer