I want to know is it possible to re factor my code in such a way to remove duplication of almost identical statements, bar the variable name and what it is initialized to by using methods. Here is the code in question:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Menu extends JFrame {
private JButton jbtChoc1 = new JButton("cross.gif");
private JButton jbtChoc2 = new JButton("nought.gif");
private JButton jbtChoc3 = new JButton("cross.gif");
private JButton jbtChoc4 = new JButton("nought.gif");
private JButton jbtChoc5 = new JButton("cross.gif");
private JButton jbtChoc6 = new JButton("nought.gif");
private JLabel foodLabelChoice = new JLabel("Main Dishes");
private ImageIcon food1Image = new ImageIcon("cross.gif");
private ImageIcon food2Image = new ImageIcon("nought.gif");
private ImageIcon food3Image = new ImageIcon("cross.gif");
private ImageIcon food4Image = new ImageIcon("nought.gif");
private ImageIcon food5Image = new ImageIcon("cross.gif");
private ImageIcon food6Image = new ImageIcon("nought.gif");
/**
* Constructor for the Menu.
*/
public Menu() {
Container cont = getContentPane();
cont.setLayout(new BorderLayout(5, 5));;
cont.setBackground(Color.white);
cont.add(foodLabelChoice, BorderLayout.NORTH);
JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5));
jbtChoc1.setIcon(food1Image);
girdSetup.add(jbtChoc1);
jbtChoc1.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc1.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc2.setIcon(food2Image);
girdSetup.add(jbtChoc2);
jbtChoc2.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc2.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc3.setIcon(food3Image);
girdSetup.add(jbtChoc3);
jbtChoc3.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc3.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc4.setIcon(food4Image);
girdSetup.add(jbtChoc4);
jbtChoc4.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc4.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc5.setIcon(food5Image);
girdSetup.add(jbtChoc5);
jbtChoc5.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc5.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc6.setIcon(food6Image);
girdSetup.add(jbtChoc6);
jbtChoc6.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc6.setHorizontalTextPosition(AbstractButton.CENTER);
cont.add(girdSetup, BorderLayout.CENTER);
}
/**
* Main method for test.
*
* #param args Initial setup.
*/
public static void main(String[] args) {
Menu frame = new Menu();
frame.setTitle("Menu");
frame.setSize(950, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I want to remove the duplication of the JButton and ImageIcon declaration and their settings within the constructor so that I simply call a single method instead of copy/paste of code.
You can do that by wrapping them in arrays, like this:
private static final String[] imageNames = {"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif"};
private JButton[] jbtChocs = new JButton[imageNames.length];
private ImageIcon[] foodImages = new ImageIcon[imageNames.length];
public Menu() {
/* ... */
for(int i = 0; i < imageNames.length; i++){
jbtChocs[i] = new JButton(imageNames[i]);
foodImages[i] = new ImageIcon(imageNames[i]);
jbtChocs[i].setIcon(foodImages[i]);
girdSetup.add(jbtChocs[i]);
jbtChocs[i].setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChocs[i].setHorizontalTextPosition(AbstractButton.CENTER);
}
}
Since the behavior is the same for all of them, this lets you simply iterate over each object, applying the same actions to each one.
Related
I want to be able to call the Introduction.Intro() method into my main file code, but it tells me I am unable to call a non-static method intro from a static context. Since I am still fairly new to coding I'm not entirely sure what the problem is. I've added my codes down below. I've tried countless online methods but sadly none have seemed to work.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Start extends JFrame implements ActionListener
{
private JFrame Main;
private JPanel PanelA, PanelB, PanelC;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public Start ()
{
//Button
Button = new JButton("Start");
Button.addActionListener(new ButtonListener());
//Text
Text = new JLabel("Welcome To The Game"); //ADD NAME OF THE GAME
//Image
Image = new ImageIcon(getClass().getResource("download.jfif")); //ADD THE IMAGE FOR WELCOME
ImageL = new JLabel(Image);
//Top Panel (PanelA) - Image
PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEmptyBorder(0,200,150,200));
PanelA.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelA.add(ImageL);
//Middle Panel (PanelB) - Text
PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEmptyBorder(50,200,10,200));
PanelB.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelB.add(Text);
//Bottom Panel (PanelC) - Buttons
PanelC = new JPanel();
PanelC.setBorder(BorderFactory.createEmptyBorder(0,200,20,200));
PanelC.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelC.add(Button);
//Main Frame
Main = new JFrame ();
Main.add(PanelA, BorderLayout.NORTH);
Main.add(PanelB, BorderLayout.CENTER);
Main.add(PanelC, BorderLayout.SOUTH);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main.setTitle("GAME TITLE"); //ADD THIS LATER
Main.pack();
Main.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae)
{
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button)
{
Introduction.Intro1(); //THESE LINE RIGHT HERE
return null; //THESE LINE RIGHT HERE
}
}
}
public static void main(String[] args)
{
new Start();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Introduction
{
private JFrame Main;
private JPanel PanelD;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public void Intro()
{
Image = new ImageIcon(getClass().getResource("guy.jfif"));
ImageL = new JLabel(Image);
PanelD = new JPanel();
PanelD.setBorder(BorderFactory.createEmptyBorder(0,100,10,100));
PanelD.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelD.add(ImageL);
PanelD.setVisible(true);
Main.add(PanelD, BorderLayout.NORTH);
}
}
EDIT: So I made another method in the Introduction class where I added this line of code, it managed to fix the error, however, the panel isn't being saved and my JFrame is outputting blank.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
If you are looking to initialize the Introduction class in main method of Start class, You can add belo code in main method after Start()
Introduction M = new Introduction();
You main method becomes :
public static void main(String[] args)
{
new Start();
Introduction M = new Introduction();
m.Intro
}
Looking at this set of code, It looks like there is incompatible issue, as you have declare JFrame as return type, while you are returning instance of Introduction.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
I am learning JRadioButtons and I do not know why it is working in the tutorial I am watching and not in my code. Could someone please take a look at it?
Main Class:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Screen screen = new Screen();
screen.setVisible(true);
}
}
Here is the Screen Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class Screen extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JRadioButton b1, b2;
ButtonGroup group;
JTextArea tb;
public Screen(){
super("First GUI");
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel p1 = new JPanel();
JTextArea tb = new JTextArea("Text Area");
group = new ButtonGroup();
group.add(b1);
group.add(b2);
b1 = new JRadioButton("Hello");
b1.setActionCommand("HELLO!");
b1.addActionListener(this);
b2 = new JRadioButton("Goodbye");
b2.setActionCommand("Goodbye! =)");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(tb);
add(p1);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tb.setText(e.getActionCommand());
}
}
In my new Java head this should work perfectly. I initialize the buttons, initialize the group. I am getting an error after I click one of the buttons: AWT-EventQueue-0. I do not know what that means so I do not know how to fix this issue.
You have declared twice the same variable. If you declare the same variable (JTextArea tb) in the global and local scope, it will be a separate object. Remove the declaration in the local scope from the Screen() constructor so it will work.
Try this
tb = new JTextArea("Text Area");
instead of
JTextArea tb = new JTextArea("Text Area");
Because of your current code, tb is still not initialized in the global scope.
Im trying to figure out how to add a JPanel into my main Frame. However, the Panel is in a different class. Essentially, I need the user to press the start button,once pressed it needs to create an object of a class (this class creates a JPanel) and add to main frame. My issue is that once I press the start button nothing happens.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Display extends JFrame{
private JPanel right,left,center,south;
private JButton start, stop,car1,car2,car3,car4;
private JTextArea text1,text2;
private TitledBorder title1,title2;
private JLabel label,label2,label3;
private RaceDisplay rd;
private Environment env;
public Display() {
super("CAR GAME");
/* ---------------------------------
* BOARD PANELS
-----------------------------------*/
//right panel uses a different layout
right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
right.setBackground(Color.GRAY);
//center panel uses default layout
center = new JPanel();
//left panel uses a different layout
left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
left.setBackground(Color.GRAY);
//south panel
south = new JPanel();
south.setBackground(Color.GRAY);
/* ---------------------------------------
* Text area used to diaply the results.
------------------------------------------*/
text1 = new JTextArea();
text2 = new JTextArea();
// ------------>car images to be used in the Car class<------------------
ImageIcon img = new ImageIcon("./images/Car1-small.gif");
ImageIcon img2 = new ImageIcon("./images/car2-small.gif");
ImageIcon img3 = new ImageIcon("./images/car3-small.gif");
ImageIcon img4 = new ImageIcon("./images/car4-small.gif");
ImageIcon imgFlag = new ImageIcon("./images/flag1.png");
ImageIcon imgFlag2 = new ImageIcon("./images/flag2.png");
label2 = new JLabel(imgFlag);
label3 = new JLabel(imgFlag2);
center.add(label3);
label = new JLabel("BEST TEAM EVER RACE GAME");
label.setFont(new Font("Georgia", Font.CENTER_BASELINE, 16));
/* ----------------------------------------------------
* creates the buttons and adds the proper image to them
--------------------------------------------------------*/
car1 = new JButton("BRITISH MOTOR COMPANY",img);
car2=new JButton("FAST AND FURIOUS",img2);
car3=new JButton("SCOOBY GANG",img3);
car4=new JButton("SPEEDY CADDY",img4);
start=new JButton("START");
stop = new JButton("STOP");
/* ----------------------------------------------------
* creates the title border and adds them to panels
--------------------------------------------------------*/
title1 = new TitledBorder("RESULTS");
title2 = new TitledBorder("CHOOSE YOUR RACER!");
//adds the title borders to the Panels.
right.setBorder(title1);
left.setBorder(title2);
/* ----------------------------------------------------
* This TextArea is added to the right Panel and it where
* the result will be displayed
--------------------------------------------------------*/
text1 = new JTextArea(" ",100,30);
right.add(text1);
text1.setLineWrap(true);
/* ----------------------------------------------------
* adds the buttons to the proper panels
--------------------------------------------------------*/
south.add(start);
south.add(stop);
left.add(car1);
left.add(car2);
left.add(car3);
left.add(car4);
left.add(label);
left.add(label2);
/* ----------------------------------------------------
* adds the panels to the main Frame at proper location
--------------------------------------------------------*/
add(right,BorderLayout.EAST);
add(left,BorderLayout.WEST);
add(south,BorderLayout.SOUTH);
add(center,BorderLayout.CENTER);
/* -------------------------------------------------
* Gives actions to the buttons
---------------------------------------------------- */
car1.addActionListener(new Car1Button());
car2.addActionListener(new Car2Button());
car3.addActionListener(new Car3Button());
car4.addActionListener(new Car4Button());
start.addActionListener(new Start());
/* ----------------------------------------------------
* sets up the main frame's components
--------------------------------------------------------*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1900,700);
setVisible(true);
}//end of constructor
/**
*
*/
private class Start implements ActionListener{
public void actionPerformed(ActionEvent event){
rd = new RaceDisplay();
add(rd);
}
}
This is the other class where the panel is created.
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class RaceDisplay extends JPanel implements ActionListener{
private Image img1,img2,img3,img4;
private int velX;
private int x;
private Timer tm;
private Environment env;
private Car car;
public RaceDisplay(){
tm = new Timer(30,this);
x=0;
//velX=car.getSpeed();
velX=x;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon car1 = new ImageIcon("./images/Car1-small.gif");
ImageIcon car2 = new ImageIcon("./images/car2-small.gif");
ImageIcon car3 = new ImageIcon("./images/car3-small.gif");
ImageIcon car4 = new ImageIcon("./images/car4-small.gif");
img1 = car1.getImage();
img2 = car2.getImage();
img3= car3.getImage();
img4= car4.getImage();
g.drawImage(img1,x,100,null);
g.drawImage(img2,x,200,null);
g.drawImage(img3,x,300,null);
g.drawImage(img4,x,400,null);
tm.start();
}
//method runs the images from left to right
public void actionPerformed(ActionEvent e) {
x = x+velX;
if(x>=600){
x=0;
x=x+velX;
// // this.wait();
// } catch (InterruptedException ex) {
// Logger.getLogger(RaceDisplay.class.getName()).log(Level.SEVERE, null, ex);
// }
repaint();
}
repaint();
}
public int getX(){
return x;
}
}
When you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint();
What exactly do you want? I mean you want the added cars to start running??? for that case you need to write a thread. your start button works perfectly.
So I am trying to pass a variable from a button press in one class to another class, and can't quite figure it out. The button press creates a random number to simulate a dice roll, adds it to a variable which then is suppose to be passed to the board class which has the game board built on it and will then use said variable to determine which space on the board the player is on. Thank you in advance.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Game extends JPanel{
private JLabel lblP1Name, lblP2Name, lblRules, lblDiceRoll;
private JTextField txtP1Name, txtP2Name;
private JButton diceRoll;
private JRadioButton rdP1, rdP2;
private int dice;
private static int countP1;
private int countP2;
private JPanel panelNorth;
private void groupButton( ) {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(rdP1);
bg1.add(rdP2);
}
public Game() throws FileNotFoundException {
setLayout (new BorderLayout());
rdP1 = new JRadioButton("Player 1");
rdP2 = new JRadioButton("Player 2");
ButtonListener listener = new ButtonListener();
Player1 player1 = new Player1(countP1);
Player2 player2 = new Player2(countP2);
Board board = new Board();
Rules rules = new Rules();
JButton diceRoll = new JButton("Roll the dice!");
panelNorth = new JPanel();
panelNorth.setLayout(new GridLayout(1,3));
lblRules = new JLabel(rules.toString());
add(panelNorth, BorderLayout.NORTH);
panelNorth.add(rdP1);
panelNorth.add(diceRoll);
panelNorth.add(rdP2);
Card card = new Card();
add(player1, BorderLayout.WEST);
add(player2, BorderLayout.EAST);
add(lblRules, BorderLayout.SOUTH);
add(board, BorderLayout.CENTER);
diceRoll.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent diceRoll){
Random random = new Random();
dice = random.nextInt(6)+1;
if(rdP1.isSelected()){
countP1 = countP1+dice;
if(countP1>48){
countP1=countP1-48;
}
}else if(rdP2.isSelected()){
countP2 = countP2+dice;
if(countP2>48){
countP2=countP2-48;
}
}
}
}
}
It's simple; just use references.
Instance your classes and pass the reference:
Board board = new Board();
YourClass yourClass = new YourClass(board);
This way you can set Board attributes from the class YourClass.
It's really easy, you could have learned how to do this just by reading basic Java books.
I'm trying to create an Onscreen telepad where people can press the keypad buttons and itll come up in the text box I haven't made the ActionListner for the keypad yet but I want it to show up in the view... Here's the code for the Keypad Panel and the View there is also a duration timer which I've managed to get working but can't put them into one view
Here's the Keypad Panel
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class KeypadPanel extends JPanel {
private JButton noStar;
private JButton noHash;
private JButton[] buttons;
private JButton C;
private JButton add;
private JPanel keypadPanel;
public KeypadPanel(TelepadController controller) {
buttons = new JButton[10];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("" + i);
// buttons[i].addActionListener(controller.new NumberButtonListener());
}
//noStar.addActionListener(controller.new noStarActionListener);
//noHash.addActionListener(controller.new noHashActionListener);
//C.addActionListener(controller.new CActionListener);
//add.addActionListener(controller.new addActionListener);
noStar = new JButton("*");
noHash = new JButton("#");
C = new JButton("C");
add = new JButton("+");
JPanel keypadPanel = new JPanel();
keypadPanel.setLayout(new GridLayout(4, 3));
for (int i = 1; i <= 9; i++) {
keypadPanel.add(buttons[i]);
add(noStar);
add(noHash);
add(C);
add(add);
}
}
}
And here's the code for the main View
package londontelepad2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import org.apache.commons.beanutils.BeanUtils;
public class TelepadView implements Observer {
private StopwatchPanel stopwatchPanel;
private KeypadPanel keypadPanel;
private JFrame frame;
/**
*
* #param controller
*/
public TelepadView(TelepadController controller) {
super();
this.setResources();
frame = new JFrame("London Telepad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stopwatchPanel = new StopwatchPanel(controller);
//stopwatchPanel = new StopwatchPanel2(controller);
keypadPanel = new KeypadPanel(controller);
frame.getContentPane().add(stopwatchPanel);
frame.getContentPane().add(keypadPanel);
frame.pack();
}
public void show() {
frame.setVisible(true);
}
#Override
public void update(Observable observable, Object arg) {
if (arg.equals(Properties.TIME)) {
try {
stopwatchPanel.setTime(BeanUtils.getProperty(observable,
Properties.TIME));
} catch (Exception e) {
System.out.println(e);
}
}
}
public void setResetState() {
stopwatchPanel.setButtons(true, false, false);
}
public void setStoppedState() {
stopwatchPanel.setButtons(false, false, true);
}
public void setRunningState() {
stopwatchPanel.setButtons(false, true, false);
}
private void setResources() {
ColorUIResource defaultBackground = new ColorUIResource(Color.white);
ColorUIResource defaultForeground = new ColorUIResource(Color.black);
ColorUIResource disabledColor = new ColorUIResource(Color.lightGray);
FontUIResource smallFont = new FontUIResource(
new Font("Dialog", Font.BOLD, 12));
FontUIResource bigFont = new FontUIResource(
new Font("Dialog", Font.BOLD, 14));
UIManager.put("Button.background",
defaultBackground);
UIManager.put("Button.foreground",
defaultForeground);
UIManager.put("Button.disabledText",
disabledColor);
UIManager.put("Button.font", smallFont);
UIManager.put("Label.background",
defaultBackground);
UIManager.put("Label.foreground",
defaultForeground);
UIManager.put("Label.font", bigFont);
UIManager.put("Panel.background",
defaultBackground);
UIManager.put("Panel.foreground",
defaultForeground);
}
}
If I do the .add seperately I get an error to do with (actual and formal argument lists differ in length)
and if i do it together I get java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
And I can't find what it is im doing wrong!!
All the help in the world would be very appreciated seeing as I'm an Uber noob at java!
Thank you
Bilal
UPDATE
Here's a log of the error I reciveve when I put them in the same .add field
Exception in thread "main" java.lang.NullPointerException
at londontelepad2.KeypadPanel.<init>(KeypadPanel.java:48)
at londontelepad2.TelepadView.<init>(TelepadView.java:46)
at londontelepad2.TelepadController.<init>(TelepadController.java:33)
at londontelepad2.LondonTelepad2.main(LondonTelepad2.java:19)
Java Result: 1
Wait, KeypadPanel is just a plain object. Why doesn't it extend JPanel?
you are calling the add()-method of a JFrame to add your components to the frame? You need to call
frame.getContentPane().add(comp);