Reputation: 895
I'm supposed to create a GUI in java for my Software Engineering class but the layout for the middle two cameras is not what I want. Right now they are labels called pCamera1 and pCamera2. I hope to change the labels to live camera streams for a rover.(If anybody has any idea how to do that I would appreciate some tips there as well).
I tried to insert images but apparently you need 10 rep to do that...so I will try my best to describe the output to you. Basically there is two buttons and one label at the top labeled Disconnect, connect, status. At the bottom are two arrows which will be used to change the cameras i the future. In the middle is my problem. There is a large yellow box which is a panel, i colored it yellow for debugging purposes. There are two labels colored red inside the box which will be where the camera streams will be displayed. These labels are stuck in the middle and herein lies my problem.
Basically the cameras(or labels) arent going what I want them to. I set the gridx and gridy to 0 for camera1 to 0 doesn't that mean the label should go to the top left of the yellow box since the yellow box is the panel? It doesn't. I also set the gridwidth to 2 to try to make it span two columns. It doesn't. The gridx and gridy works for the other labels and buttons so i have no idea why this isn't working. I went and set the ipady and ipadx to 400 and that worked but its only temporary since once you resize it, it looks terrible. Well anyway heres my code:
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);
}
}
Thank you for the help!
Upvotes: 0
Views: 432
Reputation: 347194
Basically, you need to specify how the remaining space is allocated to your components and how the components should fill that space, try adding the following to your constraints for both cameras...
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
Upvotes: 1