getContentPane() not recognized when java.awt is imported - java

ANSWERED / CLOSED
to make things simple, I've just pulled code directly from the java applet tutorial.
The code is:
private void createGUI() {
JLabel label = new JLabel(
"You are successfully running a Swing applet!");
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
getContentPane().add(label, BorderLayout.CENTER);
}
The only thing is, getContentPane() isn't recognized even though I have the following imports
import java.applet.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;
could someone please tell me what I have to do to get Eclipse Kepler to recognize getContentPane()?
Thanks

Related

Program works fine in the compiler (Eclipse), but when I export it, it doesn't work

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Frame");
JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.setBounds(0,0,300,300);
panel.setLayout(null);
button.setBounds(120,140,60,20);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.GREEN);
}
});
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
So this is my code. It works perfectly until I export it as a Runnable JAR file. I'm using Eclipse as IDE and I'm on the Windows 10. When I right-click on the project and click export, I choose Runnable JAR file as an option. After that, I choose right Main class and location where I want to save my file. This is where the problem starts: When I'm choosing Library Handling option, I have 3 options.
Extract required libraries into generated JAR
Package required libraries into generated JAR
Copy required libraries into a sub-folder next to the generated JAR
And so, when I choose the option 1), this is the error message that I get: Error: A JNI error has occurred, please check your installation and try again, and then, it's followed by: A Java Exception has occurred. After that, it just closes.
When I choose the second and third option, I don't get any error message while running it, but my cursor looks like it's loading something for 1 second and that's it.
Note that this code works in Eclipse, and the problem only occurs when I export the program.
Also, this happens with every program that I try to export.
So, if you have any advice or the solution for this problem, please let me know. Thanks in advance.

The constructor BorderStroke(Color, BorderStrokeStyle, null, null) is undefined

I am trying to give my Hbox a Border but in my project, it gives an error back. DonĀ“t understand because in my friend's project it works perfectly.
`HBox hbox= new HBox();
hbox.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, null, null)));
hbox.setSpacing(50);
hbox.setPadding(new Insets(20));
hbox.getChildren().add(imv);
hbox.getChildren().add(label);`
I found that if you import the wrong Color you will get the same error back:
//WRONG IMPORTS
import java.awt.Color;
//OR
import com.sun.prism.paint.Color;
The constructor BorderStroke(Color, BorderStrokeStyle, null, null) is undefined
This is easy to mess up because Eclipse will automatically recommend any of the 3 Color imports as options.
Remove the incorrect import for Color that you currently have, and instead make sure you manually import all the correct Classes at the top of your current Class so you do not use the wrong one:
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
Notice this is now javafx.scene.paint.Color instead.

nothing appear in my applet?

Why there is no thing appear in my applet ?
I started to learn java using eclipse before two days, but when I come to image part there is no thing appear in my applet
My code is as follows:
import java.awt.*;
import javax.swing.*;
public class CityScape extends JApplet {
setSize(800,500);
super.paint(g);
Image img=getImage(getCodeBase(),"aaa.png");
g.drawImage(img,0,0,this);
}}

How do I import a jpg image in java?

I wanted to import a image in java, and I have no idea how to do it. I need someone to show me how. I wanted to import a jpg image I found on google, I tried and a lot example code online but none of them works for me. The application I use for programming my code is by using eclipse. P.S I use a mac.
Given that you've tagged JPanel in your question, I'm going to assume that you're using swing. You can add an image to a JLabel like so:
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.net.URL;
// inside your class constructor/method
JPanel panel = new JPanel();
ImageIcon img = new ImageIcon(new URL("http://image...."));
JLabel jlPic = new JLabel(img);
panel.add(jlPic);
If your image is on your computer instead of from a URL, you can simply pass a String path to that image to the constructor of ImageIcon instead.

How to link javascript functions with applet?

I am trying to make a form page in a java applet. Now after making textbox and submit button, how can I validate this text-box? Anyone if can tell me if I can link JS code to applet and if yes, then how?
package com.tcs.applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JButton;
public class MyApplet extends Applet {
TextField inputLine = new TextField(15);
private JButton button = new JButton("Submit");
public MyApplet() {
add(inputLine);
add(button);
}
}
Package netscape.javascript provides Java code(your applet code) the ability to access the JavaScript engine in the web browser.
for more info you can check following
Netscape Doc
On the action performed of the JButton do your validation.
If there is wrong input in textfield then throw a acception.

Categories