Question: Hello, I am testing this GUI with a GridBagLayout JFrame when I can position several different panels. The panel is showing on the frame but
Hello,
I am testing this GUI with a GridBagLayout JFrame when I can position several different panels. The panel is showing on the frame but it is not showing at the X,Y coordinate. Can someone please help me?
import javax.swing.JFrame; import javax.swing.*; import java.awt.*;
public class NoLayoutGUITest{ JFrame frame = new JFrame(); JPanel panel = new JPanel(); TrafficLightIntersectionPanel p1 = new TrafficLightIntersectionPanel(); GridBagLayout gblayout; GridBagConstraints gbc; public NoLayoutGUITest() { gblayout = new GridBagLayout(); gbc = new GridBagConstraints(); frame.setLayout(gblayout); gbc.gridx = 0; gbc.gridy = 0; gblayout.setConstraints(p1, gbc); frame.add(p1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setTitle("Project 3 - Arlette Watson - Traffic Light Simulator"); frame.setSize(screenSize.width, screenSize.height); frame.getContentPane(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public static void main(String[] args) { new NoLayoutGUITest();
}
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class TrafficLightIntersectionPanel extends JPanel{
//CONSTRUCTOR------------------------------------------------------------- public TrafficLightIntersectionPanel() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBackground(Color.green); setPreferredSize(new Dimension(100, 150)); //getPreferredSize(); } /* * @Override public Dimension getPreferredSize() { return new * Dimension(150,200); } */ }// end TrafficLightIntersectionPanel class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
