JAVA I have the following code i need to make a list with buttons. I need the ou
ID: 3849706 • Letter: J
Question
JAVA I have the following code i need to make a list with buttons. I need the ourJPanel file to be a verticle box layout so that i can add more ourJPanel buttons to it. All of the ourJPanel.java files that are numbered are clones of ourJPanel1.java.
app,java
public class app
{
public static void main(String[] args)
{
MyJFrame jf;
jf = new MyJFrame();
}
}
MyJFrame.java
import javax.swing.*;
public class MyJFrame extends JFrame
{
public MyJFrame()
{
super ("Our Frame");
ourJPanel ojp = new ourJPanel();
getContentPane().add(ojp,"Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (640, 480);
setVisible(true);
}
}
ourJPanel.java
import java.awt.*;
import javax.swing.*;
public class ourJPanel extends JPanel
{
public ourJPanel()
{
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
student student1=new student("John","Smith",22,"Student");
ourJPanel1 jp1 = new ourJPanel1(student1);
ourJPanel2 jp2 = new ourJPanel2(student1);
ourJPanel3 jp3 = new ourJPanel3(student1);
ourJPanel4 jp4 = new ourJPanel4(student1);
add(jp1,"North");
add(jp2,"Center");
add(jp3,"East");
add(jp4,"South");
}
}
ourJPanel1.java
import java.awt.*;
import javax.swing.*;
public class ourJPanel1 extends JPanel
{
JButton jb1;
public ourJPanel1(student student1)
{
super();
setBackground(Color.yellow);
jb1 = new JButton(student1.getName());
add(jb1);
}
}
Explanation / Answer
/** ourJPanel.java **/
import java.awt.*;
import javax.swing.*;
public class ourJPanel extends JPanel
{
public ourJPanel()
{
super();
setBackground(Color.gray);
// use BoxLayout to add vertically use Y_AXIS, to add Horizontally use X_AXIS
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
student student1=new student("John","Smith",22,"Student");
ourJPanel1 jp1 = new ourJPanel1(student1);
ourJPanel2 jp2 = new ourJPanel2(student1);
ourJPanel3 jp3 = new ourJPanel3(student1);
ourJPanel4 jp4 = new ourJPanel4(student1);
add(jp1);
add(jp2);
add(jp3);
add(jp4);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.