Adding an image to the JFrame - java

I want to add an Image to the JFrame, the add( ) method is generating an error which says "cannot find symbol: method add(JLabel)" ... How can I fix that?
** I still haven't called the ImageLoading( ) method from the main method.
import javax.swing.*;
public class NetworkingGame {
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
}
public void ImageLoading(){
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
add(jl); //The error is in this line
}
public static void main(String[] args) throws Exception{
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new NetworkingGame();
}
});
}
}

Visibility of JFrame jfrm is limited by constructor of NetworkingGame. So add method does not exist in NetworkingGame. Make your JFrame member of NetworkingGame.
public class NetworkingGame {
private JFrame jfrm;
NetworkingGame(){
jfrm = new JFrame("Angry Painters");
...
JLabel jl = new JLabel(i);
jfrm.add(jl);

Probably you should write
jfrm.add(jl);
and have jfrm as a member.
In you case you really calling this.add() which is not exist.

You are calling the add method as if it was an instance method in your class NetworkingGame and your class doesn't have any method, so you are getting the compiler error as the compiler can't find any method of such a name in your class NetworkingGame, so add it to the Jframe instead.
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
jfrm.add(jl);
}

Related

Calling different screen thru button from a screen

Hello I would like to ask how can I call my Main menu screen from MainScreen? and kindly explain a little more details about Listener.
below is my prepared code:
public class MainScreen {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel WelcomeNote = new JLabel("Welcome");
panel.add(WelcomeNote);
JButton Start = new JButton("Start");
panel.add(Start);
//Insert action for Start button here
}
}
public class MainMenu {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel menuLbl = new JLabel("Main Menu");
panel.add(menuLbl);
}
}
What is wrong?
You cannot have two main methods in a single file in Java.
Program
Here is a demo program to change windows.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class First extends JFrame
{
JLabel jlb = new JLabel("Label in First Window");
JButton jb = new JButton("Next Window");
First()
{
super("First Windows");
//Set this frame
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener((e)->{
this.setVisible(false);
new Second();
});
setVisible(true);
}
}
class Second extends JFrame implements ActionListener
{
JLabel jlb = new JLabel("Label in Second Window");
JButton jb = new JButton("Prev. Window");
Second()
{
super("Second Window");
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
this.setVisible(false);
new First();
}
}
class StartHere
{
public static void main(String[] args) {
Runnable r = ()->{
new First();
};
r.run();
}
}
Understanding the above program.
The StartHere class has a main method. It is just used for calling the first window you like. I could even call Second using new Second().
First and Second are similar codes.
Both of them have buttons. On each button (or JButton) I have added a method named addActionListner(this). This method fires up an ActionEvent which as you can see in Second class is captured by actionPerformed method. This method is declared in Functional Interface, ActionListener. The 'this' passed in Second class is you telling where the actionPerformed method is present in your code. The parameter is an ActionListener. Hence, you have to implement ActionListener for the class where you define actionPerformed.
Bonus
The First class doesn't seem to follow the norms described above. I passed a strange syntax. It is a new feature included in Java 8.
See this Oracle tutorial about Lambda Expressions.

jlabel and paintComponent in same jframe in java

the code down here only calls the class(class "oefen") whith the code that makes a jframe and a ball(using the painComponent method)and if you press the arrow keys it moves in that direction...it works fine but when i add the target(jlabel) to the jframe only the target shows and nothing else..please help me to put the target and ball in the same frame
public class theFrame implements KeyListener {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static void main(String args[]){
j.setSize(500,600);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
oefen o = new oefen();
j.add(o);
target.setLayout(null);
target.setSize(100,100);
target.setLocation(250,0);
j.add(target);
}
}
thanks
public class theFrame extends JComponent {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static oefen o = new oefen();
public static void main(String args[]){
j.setSize(500,600);
j.setLayout(null);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
target.setLayout(null);
target.setSize(0,0);
target.setLocation(0,0);
j.add(o);
j.add(target);
}
}
if i set the JFrame Layout to null notihng shows...the code above shows how i set the JFrameLayout cuz i don't know if its wrong.
please help
try setting the JFrames layout to null too, because you use absolute positioning.

JFrame opens over and over again

I'm having troubles with this program. Everything works, but the program keeps opening the JFrame over and over again (and obviously, I only want just one JFrame to be opened). What is wrong with my code?
Thank you in advance,
Stefan
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColoredWords {
JFrame frame;
JPanel controlPanel, wordsPanel;
JButton match, nomatch;
ColoredWords() {
SwingUtilities.invokeLater( new Runnable() {
#Override
public void run() {
frame = new JFrame("Colored Words Experiment");
wordsPanel = new JPanel();
controlPanel = new JPanel();
frame.setLayout(new BorderLayout());
frame.add(wordsPanel, BorderLayout.NORTH);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
match = new JButton("Matching");
nomatch = new JButton("Non-Matching");
controlPanel.add(match, BorderLayout.WEST);
controlPanel.add(nomatch, BorderLayout.CENTER);
ClicksReporter clicksreporter;
clicksreporter = new ClicksReporter();
match.addActionListener(clicksreporter);
nomatch.addActionListener(clicksreporter);
}
} );
}
class ClicksReporter extends ColoredWords implements ActionListener {
Labeling labeling = new Labeling();
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Matching")) {
wordsPanel.add(labeling);
} else if (e.getActionCommand().equals("Non-Matching")) {
wordsPanel.add(labeling);
}
}
}
public static void main(String[] arg) {
new ColoredWords();
}
}
class Labeling extends JPanel {
JLabel[] labelsList = new JLabel[20];
int i = 0;
public Labeling() {
while (i < 5) {
labelsList[i] = new JLabel("black");
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
labelsList[i].setOpaque(true);
labelsList[i].setBackground(Color.white);
add(labelsList[i]);
i++;
}
}
}
The problem is when you instantiate ClicksReporter inside main(). This is because it inherits the constructor of ColoredWords, calling it when instantiated. To avoid this, you can take the program code out of the constructor and into another method, say, execute() or run(). You can then adjust your program accordingly to call this method in main().
Your ColoredWords constructor calls clicksreporter = new ClicksReporter(); but ClicksReporter inherits ColoredWords, so the constructor of ColoredWords gets called, wich will again execute clicksreporter = new ClicksReporter(); and so on... You get stuck in an infinite loop. Try to remove inheritance.
You create a new JFrame in your ColoredWords constructor.
You create a new ClicksReporter in your ColoredWords constructor.
ClicksReporter extends ColoredWords.
That means every ColoredWords construction leads to another ColoredWords construction, which also creates a JFrame.

Java: JTextField won't appear

public class HandleUI {
public static void setUpUI(){
JPanel jPan = new JPanel();
FlowLayout flow = new FlowLayout();
jPan.setLayout(flow);
txtFld = new JTextField();
txtFld.setSize(550,5);
jPan.add(txtFld);
jPan.setSize(10,200);
MainClass.mainFrame.add(jPan);
int gapX = MainClass.mainFrame.getX()-(txtFld.getX()/2);
}
//Instance variables.
public static JTextField txtFld;
public JButton [] buttons;
}
public class MainClass {
public static void main (String [] args){
int frameX = Constants.FRAME_WIDTH;
int frameY = Constants.FRAME_HEIGHT;
mainFrame = new JFrame();
mainFrame.setSize(frameX,frameY);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
HandleUI.setUpUI();
}
//Instance variables
public static JFrame mainFrame;
}
It's supposed to show JTextField, but as you might have guessed - JFrame shows nothing. I didn't type in imports on purpose, but they are all there. I can't find the problem. Can anyone help?
1.) Simply write:
JTextField tField = new JTextField(10);
Here In the constructor you are passing the number of columns, which is sufficient for a layout like FlowLayout to set the size of the JTextField
2.) The line mainFrame.setVisible(true); must be the last line of the main method. You need to put the code at main() method, inside SwingUtilities.invokeLater(...) thingy.
3.) Instead of setting size on the JFrame use JFrame.pack(), to set the window to the preferred size.
4.) Creation of unnecessary static members is a design flaw. Try to keep yourself away from such thingies.
5.) Read a bit about Concurrency in Swing
One Example Program for help(Use the order of lines as specified in this answer):
import java.awt.*;
import javax.swing.*;
public class Example {
private void displayGUI() {
JFrame frame = new JFrame("Example Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
JTextField tField = new JTextField(10);
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
You have to call setVisible(true) on your JFrame AFTER having initialised your UI.
Simply pulling the following line:
HandleUI.setUpUI();
... right before:
mainFrame.setVisible(true);
... will do the trick.
As a side note, I'd like to point out that setting the size of your text field won't really work like you did. You probably would use setPreferredSize(Dimension) instead. Or, even better, organise your UI only using Layouts and not manually setting any component's size.

How can i add the JPanel class into JFrame form class in netbeans?

I created two classes in netbeans;One of them is a JPanel form and another is a JFrame form;
How can i add the JPanel class into JFrame form class?
I wrote this code in constructor of JFrame form Class but ,it didnt work.
public JFrameClass() {
initComponents();
this.getContentPane().add(jpc = new JPanelClass());
jpc.setVisible(true);
this.pack();
this.setVisible(true);
}
You need to make sure the JPanelClass is visible from where your JFrameClass is.
Then do the following:
JPanelClass jpc = new JPanelClass()
this.getContentPane().add(jpc);
Also, there is no need to call jpc.setVisible(true);
The resulting code should be:
public JFrameClass() {
initComponents();
JPanelClass jpc = new JPanelClass()
getContentPane().add(jpc);
pack();
setVisible(true);
}
How can i add the JPanel class into JFrame form class in netbeans?
In your JFrame class just set your JPanel and add its to Container.
JPanel panel = new JPanelClass();
controls.add(panel);
Note: You should have some private void method named for example createAndAddCompontents() and call it in your constructor.
public JFrameClass() {
...
createAndAddCompontents();
}
Then when you want to execute your Application so in main() method you should call it similar like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
YouJFrameClass initAndShowComponents = new YouJFrameClass();
initAndShowComponents.setVisible(true);
}
});
set the bounds of the JPanel so that the container knows where to draw it
public void run() {
NewJFrame frame = new NewJFrame();
NewJPanel panel = new NewJPanel();
panel.setBounds(0, 0, 200, 200);
frame.add(panel);
frame.setVisible(true);
}

Categories

Resources