我应该为我的软件工程类在java中创建一个GUI,但是中间两个摄像头的布局不是我想要的。现在它们是名为pCamera1和pCamera2的标签。我希望将标签更改为流动站的实时摄像机流。(如果有人知道如何做到这一点,我会很感激那些提示)。
我试图插入图片,但显然你需要10个代表才能做到这一点......所以我会尽力向你描述输出。基本上有两个按钮和一个标签,顶部标有Disconnect,connect,status。底部是两个箭头,用于改变未来的摄像机。中间是我的问题。有一个大的黄色盒子是一个面板,我把它染成黄色用于调试目的。盒子里面有两个红色的标签,它们将显示相机流。这些标签卡在中间,这就是我的问题。
基本上相机(或标签)没有达到我想要的水平。我将gridx和gridy设置为0,将camera1设置为0并不意味着标签应该位于黄色框的左上角,因为黄色框是面板?它没有。我还将gridwidth设置为2以尝试使其跨越两列。它没有。 gridx和gridy适用于其他标签和按钮,因此我不知道为什么它不起作用。我把ipady和ipadx设置为400,但这只是暂时的,因为一旦你调整它,它看起来很糟糕。好吧无论如何继承我的代码:
package rover.gui;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Matt
*/
public class RoverGUI {
private JFrame f; //The window
private JPanel p; //Stuff inside the window(Its a div essentially)
private JPanel pCameras;
private JPanel pPanCamera;
private JLabel pCamera1;
private JLabel pCamera2;
private JButton connect;
private JButton disConnect;
private JButton cameraR;
private JButton cameraL;
private JLabel lab;
private JButton temp;
String connected = "Inactive";
public RoverGUI() {
gui();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new RoverGUI();
/* String fn = JOptionPane.showInputDialog("Enter first number"); //Entered number is stored in string fn.
JOptionPane.showMessageDialog(null, "The answer is " + fn, "The title",""); //p(position on screen, null = center, message, title bar)
// TODO code application logic here*/
}
public void gui() {
f = new JFrame("Rover");
f.setVisible(true);
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //allows you to close the GUI.
p = new JPanel(new GridBagLayout());
pCameras = new JPanel(new GridBagLayout());
pPanCamera = new JPanel(new GridBagLayout());
//pCameras = new JPanel(new GridBagLayout());
pCameras.setBackground(Color.YELLOW);
//pCamera1.setBackground(Color.RED);
//pCamera2.setBackground(Color.RED);
GridBagConstraints c = new GridBagConstraints();//GridBagConstraint is a specific way of arranging the objects in java.
c.fill = GridBagConstraints.HORIZONTAL;
c.fill = GridBagConstraints.VERTICAL;
connect = new JButton("Connect");
disConnect = new JButton("Disconnect");
cameraR = new JButton(">>");
cameraL = new JButton("<<");
lab = new JLabel("Status: " + connected);
temp = new JButton("Camera feed goes here");
pCamera1 = new JLabel("Camera 1");
pCamera1.setBackground(Color.red);
pCamera1.setOpaque(true);
pCamera2= new JLabel("Camera 2");
pCamera2.setBackground(Color.red);
pCamera2.setOpaque(true);
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(null, "connect");
connected = "Active";
lab.setText("Status: " + connected);
}
});
disConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(null, "disConnect");
connected = "Inactive";
lab.setText("Status: " + connected);
}
});
cameraR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Right");
}
});
cameraL.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Left");
}
});
//Add button & label to panel; add panel to frame.
c.insets = new Insets(10,10,10,10);//makes buttons have margins(top, left, right, bottom)
c.gridx = 0; //Set the x axis for the object
c.gridy = 0; //Set the y axis for the object
p.add(disConnect, c);
c.gridx = 1; //Set the x axis for the object
c.gridy = 0; //Set the y axis for the object
p.add(connect, c);
c.gridx = 0; //Set the x axis for the object
c.gridy = 0; //Set the y axis for the object
pPanCamera.add(cameraL, c);
c.gridx = 1; //Set the x axis for the object
c.gridy = 0; //Set the y axis for the object
pPanCamera.add(cameraR, c);
c.gridx = 2;
c.gridy = 0;
p.add(lab, c);
//Problem starts here
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
pCameras.add(pCamera1, c);
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 2;
pCameras.add(pCamera2, c);
//Problem ends here
f.add(p,BorderLayout.NORTH);
f.add(pPanCamera, BorderLayout.SOUTH);
//f.add(pCamera1, BorderLayout.WEST);
//f.add(pCamera2, BorderLayout.EAST);
f.add(pCameras, BorderLayout.CENTER);
}
}
感谢您的帮助!
答案 0 :(得分:1)
基本上,您需要指定如何将剩余空间分配给组件以及组件应如何填充该空间,请尝试将以下内容添加到两个相机的约束中...
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;