Need a little help on this one..... Consider the following the code. import java
ID: 662446 • Letter: N
Question
Need a little help on this one.....
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.
a.Write the code to set the (correct) layout manager of the content pane..
b.Write the code to set the (correct) layout manager of the panel labelPanel (containing the green and blue labels numbered 0 through 11)..
c.Write the code to set the (correct) layout manager of the panel buttonPanelm(containing the 3 buttons "button0", "button1", and "button2")..
d.Write the code to instantiate the array buttons, all the buttons of the array and adds them to buttonPanel..
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).
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)
Explanation / Answer
package com.tutai;
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;
public MyLayout(){
FlowLayout f=new FlowLayout();
c.setLayout(f);
labelPanel.setLayout(f);
buttonPanel.setLayout(f);
buttons[0]=new JButton("button0");
buttons[1]=new JButton("button1");
buttons[2]=new JButton("button2");
buttonPanel.add(buttons[0]);
buttonPanel.add(buttons[1]);
buttonPanel.add(buttons[2]);
labels[0][0]=new JLabel("green");
labels[1][0]=new JLabel("red");
labels[2][0]=new JLabel("yellow");
labels[3][0]=new JLabel("white");
labels[4][0]=new JLabel("black");
labels[5][0]=new JLabel("pink");
labels[6][0]=new JLabel("purple");
labels[7][0]=new JLabel("cyan");
labels[8][0]=new JLabel("magenta");
labels[9][0]=new JLabel("brown");
labels[10][0]=new JLabel("grey");
labels[10][0]=new JLabel("blue");
labelPanel.add(labels[0][0]);
labelPanel.add(labels[1][0]);
labelPanel.add(labels[2][0]);
labelPanel.add(labels[3][0]);
labelPanel.add(labels[4][0]);
labelPanel.add(labels[5][0]);
labelPanel.add(labels[6][0]);
labelPanel.add(labels[7][0]);
labelPanel.add(labels[8][0]);
labelPanel.add(labels[9][0]);
labelPanel.add(labels[10][0]);
labelPanel.add(labels[11][0]);
c.add(p3);
c.add(p4);
c.add(p5);
c.add(p6);
c.add(labelPanel);
c.add(buttonPanel);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.