(Using the FlowLayout manager) Write a program that meets the following requirem
ID: 3620962 • Letter: #
Question
(Using the FlowLayout manager) Write a program that meets the following requirements (see picture):
1. Create a frame and set its layout to FlowLayout.
2. Create two panels and add them to the frame.
3. Each panel contains three buttons. The panel uses FlowLayout.
Example To help you with the above problem:
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
public class ShowFlowLayout extends JFrame {
public ShowFlowLayout() {
// Set FlowLayout, aligned left with horizontal gap 10
// and vertical gap 20 between components
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
// Add labels and text fields to the frame
add(new JLabel("First Name"));
add(new JTextField(8));
add(new JLabel("MI"));
add(new JTextField(1));
add(new JLabel("Last Name"));
add(new JTextField(8));
}
/** Main method */
public static void main(String[] args) {
ShowFlowLayout frame = new ShowFlowLayout();
frame.setTitle("ShowFlowLayout");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation / Answer
//here is test.java import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.*; public class test { public static void main(String args[]) { JFrame frame = new JFrame("FlowLayout Test"); frame.setLayout(new FlowLayout()); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JButton b1 = new JButton("B1"); JButton b2 = new JButton("B2"); JButton b3 = new JButton("B3"); JButton b4 = new JButton("B4"); JButton b5 = new JButton("B5"); JButton b6 = new JButton("B6"); p1.add(b1); p1.add(b2); p1.add(b3); p2.add(b4); p2.add(b5); p2.add(b6); frame.add(p1); frame.add(p2); frame.setSize(500,100); // frame.pack(); frame.setVisible(true); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.