Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

ContainedButtons

  Asked By: Rainhard    Date: Oct 09    Category: Java    Views: 483
  

I have taken some code
from 'java examples' which is a frame with contained buttons and i
created a main. Only that when I compile it only gives me the fram
without the contained buttons? Can you help?



import javax.swing.*;
import java.awt.*;

public class ContainersButton extends JPanel
{
public static void main(String []args)
{
JFrame frame = new JFrame("ShowContainedButtons");

frame.setTitle("Show contained buttons in frame");
//frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
}



public ContainersButton()
{
this.setBackground(Color.white);
this.setFont(new Font("Dialog", Font.BOLD, 24));

JPanel p1 = new JPanel();
p1.setBackground(new Color(200, 200, 200));
this.add(p1);
p1.add(new JButton("#1"));

JPanel p2 = new JPanel();
p2.setBackground(new Color(150, 150, 150));
p1.add(p2);
p2.add(new JButton("#2"));

JPanel p3 = new JPanel();
p3.setBackground(new Color(100, 100, 100));
p2.add(p3);
p3.add(new JButton("#3"));

JPanel p4 = new JPanel();
p4.setBackground(new Color(150, 150, 150));
p1.add(p4);
p4.add(new JButton("#4"));
p4.add(new JButton("#5"));

this.add(new JButton("#6"));


}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Bellona Lopez     Answered On: Oct 09

import javax.swing.*;
import java.awt.*;


public class  ContainersButton extends JPanel
{
public static  void main(String []args)
{
ContainersButton containersButton = new ContainersButton();
JFrame frame  = new JFrame("ShowContainedButtons");

frame.setTitle("Show contained  buttons in frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.getContentPane().add(containersButton,
BorderLayout.CENTER);
frame.setVisible(true);
}



public ContainersButton()
{
this.setBackground(Color.white);
this.setFont(new Font("Dialog", Font.BOLD, 24));

JPanel p1 = new JPanel();
p1.setBackground(new Color(200, 200, 200));
this.add(p1);
p1.add(new JButton("#1"));

JPanel p2 = new JPanel();
p2.setBackground(new Color(150, 150, 150));
p1.add(p2);
p2.add(new JButton("#2"));

JPanel p3 = new JPanel();
p3.setBackground(new Color(100, 100, 100));
p2.add(p3);
p3.add(new JButton("#3"));

JPanel p4 = new JPanel();
p4.setBackground(new Color(150, 150, 150));
p1.add(p4);
p4.add(new JButton("#4"));
p4.add(new JButton("#5"));

this.add(new JButton("#6"));


}
}

 
Didn't find what you were looking for? Find more on ContainedButtons Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 

Related Post