1 (a). Use the attached code to implement a button panel in the capture panel. W
ID: 3779900 • Letter: 1
Question
1 (a). Use the attached code to implement a button panel in the capture panel. When you finish and run this program, a GUI as shown in Figure 1, will be displayed on screen. Screenshot and source code have to be included in your solution. Write comments.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Capture
{
private JFrame frame;
public Capture()
{
frame = new JFrame("Capture example");
makeFrame();
}
private void makeFrame()
{
// main container
Container mainContainer = frame.getContentPane( );
mainContainer.setLayout(new BorderLayout());
// center containter to hold Capture and Display Panel
JPanel centerContainer = new JPanel ();
centerContainer.setLayout(new BorderLayout());
// put the exit button in the south
mainContainer.add(new JButton("Exit"), BorderLayout.SOUTH);
// Capture Panel with BorderLayout
JPanel capturePane= new JPanel();
capturePane.setLayout(new BorderLayout());
//set the border with a title
capturePane.setBorder(new TitledBorder(new EtchedBorder(), "Capture Pane"));
//put the top panel into the Capture panel
capturePane.add(creatTopPanel(), BorderLayout.NORTH);
//put radio button panel into the capture panel
capturePane.add(creatRadioPanel(), BorderLayout.CENTER);
//put button panel into the capture panel
Insert your statement here which is very similar to the previous statement.
//put the Capture panel into center panel
centerContainer.add(capturePane, BorderLayout.CENTER);
// Display Panel with GridLayout
JPanel displayPane= new JPanel();
displayPane.setLayout(new GridLayout(4,1));
//set the border with a title
displayPane.setBorder(new TitledBorder(new EtchedBorder(), "Display Pane"));
// put the 4 label panels in Display Panel
displayPane.add(creatLabelPanel("Name"));
displayPane.add(creatLabelPanel("Surname"));
displayPane.add(creatLabelPanel("Occupation"));
displayPane.add(creatLabelPanel("Sex"));
//put the Display panel into center panel
centerContainer.add(displayPane, BorderLayout.SOUTH);
//put the center panel into main container to display
mainContainer.add(centerContainer, BorderLayout.CENTER);
frame.setSize(450,425);
frame.setVisible(true);
}
private JPanel creatTopPanel()
{
JPanel topPane = new JPanel();
topPane.setLayout(new GridLayout(3,1));
// make three panels with FlowLayout
JPanel namePane = new JPanel();
namePane.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel surnamePane = new JPanel();
surnamePane.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel jobPane = new JPanel();
jobPane.setLayout(new FlowLayout(FlowLayout.LEFT));
//put the label "Name:" and textField into the name panel
namePane.add(new JLabel("Name:"));
namePane.add(new JTextField("", 20));
//put the label "Surname:" and textField into the surname panel
surnamePane.add(new JLabel("Surname:"));
surnamePane.add(new JTextField("", 30));
//put the label "Occupation:" and comebox into the job panel
jobPane.add(new JLabel("Occupation:"));
String[] comboTypes = { "Lecturer", "Staff", "Student"};
JComboBox comboTypesList = new JComboBox(comboTypes);
comboTypesList.setSelectedIndex(0);
jobPane.add(comboTypesList);
//put the three panels into top panel
topPane.add(namePane);
topPane.add(surnamePane);
topPane.add(jobPane);
return topPane;
}
private JPanel creatRadioPanel()
{
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
JRadioButton male=new JRadioButton("male");
JRadioButton female=new JRadioButton("female");
ButtonGroup sex=new ButtonGroup();
sex.add(male);
sex.add(female);
male.setSelected(true);
panel.add(male);
panel.add(female);
panel.setBorder(new TitledBorder(new EtchedBorder(), "Sex"));
return panel;
}
//Implement the button panel here
private JPanel creatButtonPanel()
{
}
private JPanel creatLabelPanel(String labelTitle)
{
JPanel labelPane = new JPanel();
labelPane.setLayout(new FlowLayout(FlowLayout.LEFT));
// put label in the label panel
labelPane.add(new JLabel(labelTitle + ": "));
return labelPane;
}
}
Insert your statement here which is very similar to the previous statement.
Explanation / Answer
creating a button panel:
capturePane.add(creatButtonPanel(), BorderLayout.BOTTOM);
implementation of the button panel:
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
JButtonPanel submit=new JButtonPanel("submit");
submit.setSelected(true);
panel.setBorder(new TitledBorder(new EtchedBorder(), "submit"));
return panel;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.