Matthew Bailey

Greenhorn
+ Follow
since Jan 29, 2009
Merit badge: grant badges
For More
South-East England, United Kingdom
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Matthew Bailey

Thank you! It was just the location of the image, now it works.

Thanks also for the other advice, I'll be sure to use the tags in any future posts and I'll put any other swing-related things in the swing forum.
14 years ago
Sorry to bother everyone yet again but I have a problem that is really annoying me. Our teacher wants us to make a calculator, which I have done, and wants us to put as many different features into it as possible. As a suggestion he said we should have an "About" thing with stuff like Version Number, Creator, etc and a picture. Now I am trying to use a JLabel as an icon, which apparently you can do with its constructor "JLabel(Icon image)", I have looked all over for how you acomplish this but I have had no luck.

Short version: How do you put an image into your java app?

My code if it will help:

package mycalculator;

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

public class AboutForm extends JFrame implements ActionListener{


public AboutForm(){
super("About");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
Font f = new Font("Trebuchet MS", Font.PLAIN, 13);
Font f2 = new Font("Trebuchet MS", Font.BOLD, 16);


this.setFont(f);
BorderLayout b1 = new BorderLayout();
GridLayout g = new GridLayout(4, 1);
JButton btn1 = new JButton("CLICK HERE");
btn1.setFont(f);

JButton btn2 = new JButton("CLICK HERE ALSO");
JPanel panel = new JPanel();
JLabel lbl = new JLabel();
JLabel lbl2 = new JLabel();

ImageIcon icon = new ImageIcon("images/b.gif");
JLabel lblImg = new JLabel(icon);

panel.setSize(100,100);
panel.setLayout(g);
panel.add(lblImg);
panel.add(lbl2);
this.getContentPane().add(panel, BorderLayout.CENTER);


}


I have my image in a folder in NetBeansProjects\myCalculator\src\mycalculator\images, is this the right place to put it?

Any help is greatly appreciated.

14 years ago
So if I put the button into a panel I can change the size of it?
14 years ago
Why doesn't setSize() do anything? I have a button but it takes up the size of the whole frame and I have tried using setSize() to make it smaller but it does absolutley nothing. I've read elsewhere that instead of setSize() you should use setPreferredSize or setMinimumSize() but they take (int dimension) as a parameterr and I have no idea what the value should be because every number I have tried just seems to give an error and doesn't work.

Please help.
14 years ago
Thanks for the help everyone. I think I understand it a little better now. So I include the test class to try out the methods of the other classes and to see that they will work, right?
15 years ago
Hello all.

I am reading through "Head First Java" to help me with my studies in college and the book mentions a test class that includes the main method. The book says that the test class is apparently used to make your objects and then, it seems, it contains the code to actually run your program. Why is it called a test class? How is it testing methods when what you are doing in the tester class is the actual thing that you want the program to do?? The book has just completely confused me, could somebody here please try and explain the 'test class', what it is used for and why. I really don't undertsand the concept.

Thank you
15 years ago