Consider the following the code. import javax.swing.*; import java.awt.*; import
ID: 666954 • Letter: C
Question
Consider the following the code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyLayout extends JFrame
{
private Container c;
private JPanel p3, p4, p5, p6, buttonPanel, labelPanel;
private JButton [ ] buttons;
private JLabel [ ][ ] labels;
}
Above is a figure representing the final result of the GUI we are building.
public MyLayout( )
{
// for questions a to f,
// you are coding inside the Password constructor
// Assume that c, buttonPanel, labelPanel, p3, p4, p5, p6
// have been instantiated.
Write the code to set the (correct) layout manager of the content pane.
Write the code to set the (correct) layout manager of the panel labelPanel (containing the green and blue labels numbered 0 through 11).
Write the code to set the (correct) layout manager of the panel buttonPanelm(containing the 3 buttons "button0", "button1", and "button2").
Write the code to instantiate the array buttons, all the buttons of the array and adds them to buttonPanel.
Write the code to instantiate the array labels, all the labels of the array and adds them to labelPanel. (DO NOT WORRY ABOUT THE COLORS AND OPACITY, DO NOT WORRY ABOUT CENTERING THE TEXT)
Write the code to add all the panels p3, p4, p5, p6, labelPanel and buttonPanel to the content pane; assume that they all have been instantiated and that the steps above have taken place; the order in which you add them is important (it should match the figure). (DO NOT WORRY ABOUT SETTING THE SIZE/VISIBILITY OF THE FRAME)
Explanation / Answer
Hi,
Below is the solution to your problem:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyLayout extends JFrame
{
private Container c;
private JPanel p3, p4, p5, p6, buttonPanel, labelPanel;
private JButton [ ] buttons;
private JLabel [ ][ ] labels;
}
Above is a figure representing the final result of the GUI we are building.
public MyLayout( )
{
ResultsPanel myPanel = new ResultsPanel(p3); //pnlResults is an existing JPanel
myPanel.addLabel(p3);
JPanel p3;
public ResultsPanel(JPanel p4 ) {
p3 = p4;
}
public void addLabel(JPanel myResults) {
JLabel P5 = new JLabel("Green", JLabel.LEFT);
JLabel P6 = new JLabel("Green", JLabel.LEFT);
myPanel.setLayout(new FlowLayout());
add(labels);
}
C.Write the code to set the (correct) layout manager of the panel buttonPanelm(containing the 3 buttons "button0", "button1", and "button2").
pane.setLayout(null);
JButton b1 = new JButton("button0");
JButton b2 = new JButton("button1");
JButton b3 = new JButton("button2");
pane.add(b1);
pane.add(b2);
pane.add(b3);
Insets insets = pane.getInsets();
Dimension size = b1.getPreferredSize();
b1.setBounds(25 + insets.left, 5 + insets.top,
size.width, size.height);
size = b2.getPreferredSize();
b2.setBounds(55 + insets.left, 40 + insets.top,
size.width, size.height);
size = b3.getPreferredSize();
b3.setBounds(150 + insets.left, 15 + insets.top,
size.width + 50, size.height + 20);
...//In the main method:
Insets insets = frame.getInsets();
frame.setSize(300 + insets.left + insets.right,
125 + insets.top + insets.bottom);
d:Write the code to instantiate the array buttons, all the buttons of the array and adds them to buttonPanel.
Already covered in solution to question c.
e.Write the code to instantiate the array labels, all the labels of the array and adds them to labelPanel. (DO NOT WORRY ABOUT THE COLORS AND OPACITY, DO NOT WORRY ABOUT CENTERING THE TEXT)
Already covered in b.
f.Write the code to add all the panels p3, p4, p5, p6, labelPanel and buttonPanel to the content pane; assume that they all have been instantiated and that the steps above have taken place; the order in which you add them is important (it should match the figure). (DO NOT WORRY ABOUT SETTING THE SIZE/VISIBILITY OF THE FRAME)
Covered in b,c,d solution.
Hope that helps.HAPPY ANSWERING!!!!!!!!!!!!!!
ResultsPanel myPanel = new ResultsPanel(p3); //pnlResults is an existing JPanel
myPanel.addLabel(p3);
JPanel p3;
public ResultsPanel(JPanel p4 ) {
p3 = p4;
}
public void addLabel(JPanel myResults) {
JLabel labels = new JLabel("test", JLabel.LEFT);
myPanel.setLayout(new FlowLayout());
add(labels);
}