type cannot be resolved [closed] - java

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
heres my code:
import javax.swing.*;
import java.awt.*;
public class FirstGui extends JFrame {
private JLabel label;
private JButton button;
public FirstGui() {
setLayout(new FlowLayout());
button = new JButton("Click for sex");
add(button);
label = new JLabel("");
add(label);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("how you can see wors here");
}
}
public static void main(String [] args) {
FirstGui gui = new FirstGui();
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setSize(200, 200);
gui.setTitle("Title");
gui.setVisible(true);
}
}
And it generates a errors:
ActionEvent cannot be resolved to a type FirstGui.java /Test/src line 26 Java Problem
ActionListener cannot be resolved to a type FirstGui.java /Test/src line 24 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (FirstGui.event) FirstGui.java /Test/src line 21 Java Problem
what is wrong with it??? im new to java.

Import the following:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

ActionEvent and ActionListener are located into the java.awt.event package.
Importing java.awt.* is not enough.

Both of these classes require you to import them. You can do so by importing everything in java.awt.event:
import java.awt.event.*;
or you may just want to import specifically what you're using:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Remember that it's considered good practice to import individual classes (the latter option) instead of importing whole packages.
If you ever get stuck like this again, looking at The Docs for any Java Class will tell you exactly what you need to import with a little diagram that looks like this:
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ActionEvent

Related

How do I add an image with this code?

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.sound.midi.Patch;
import javax.swing.*;
this is were the code start
public class graficCar extends JComponent
{
private ImageIcon image1=null;
private JLabel label1=null;
private ImageIcon image2=null;
private JLabel label2=null;
graficCar(){
setLayout(new FlowLayout());
the problem here
image1=new ImageIcon(getClass().getResource("4596067.png"));
label1=new JLabel(image1);
add(label1);
}
the main
public static void main(String[] args)
{
graficCar g=new graficCar();
g.setDebugGraphicsOptions(JFrame.EXIT_ON_CLOSE);
g.setVisible(true);
}
As you mentioned, your png file is in C:\Users\user\Downloads folder. I bet class is somewhere else.
The problem is getResource() finds file in classpath by default. Here you could find details if you want to go deeper.
One solution is to put .png right next to your .java class but it's bad practice. More convenient way described here. The best practice is to create special folder for your resources in the project and then use relative path to it. How to do this is out of scope although it's described in many sites and in Stackoverflow too. Don't hesistate to use Search :)

Can't add a simple checkbox in frame 'java AWT'

I'm learning GUI programming in java AWT and am a bit stuck. I can't add a couple of check-boxes in a frame the code i'm trying is-
package awt2;
import java.awt.*;
import java.awt.event.*;
public class Checkbox {
public static void main(String args[]) {
Frame mainFrame= new Frame("Checkbox test");
Checkbox checkBox1= new Checkbox();
Checkbox checkBox2= new Checkbox();
checkBox1.setBounds(100,100,50,50);
checkBox2.setBounds(150,120,50,50);
mainFrame.add(checkBox1);
mainFrame.add(checkBox2);
mainFrame.setVisible(true);
}
}
The error reckons this on checkBox1.setBounds() and checkBox2.setBounds()-
The method setBounds(int, int, int, int) is undefined for the type Checkbox
And on mainFrame.add()-
The method add(Component) in the type Container is not applicable for the arguments (Checkbox)
Can someone explain what these errors are all about and how can i fix them? Also i'm using eclipse IDE and javac version 1.8.0_144
You got a problem because your class is named Checkbox,
which is hiding the java.awt.Checkbox class.
Just choose another class name, for example CheckboxTest:
package awt2;
import java.awt.*;
import java.awt.event.*;
public class CheckboxTest {
// your code
}

Why the "+" operator is undefined? JAVA [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I made this code in eclipse=
Strart CLASS(i made a mistace)
import javax.swing.JFrame;
public class strart {
public static void main(String args[])
{
Window object = new Window();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(300,250);
object.setVisible(true);
}
}
Window CLASS
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.TextField;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JFrame {
TextField kimeno = new TextField(25);
TextField dkimeno = new TextField(25);
TextField n1 = kimeno;
TextField n2 = dkimeno;
private JButton plus;
public Window()
{
super("Math engine");
setLayout(new FlowLayout());
plus = new JButton("+");
plus.setFocusable(false);
plus.addActionListener(new EnableButton());
add(plus);
add(kimeno);
add(dkimeno);
}
public class EnableButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(n1+n2);
}
}
}
But i am still getting an error in line 32 of the Window class
the error is "The operator + is undefined for the argument type(s) java.awt.TextField, java.awt.TextField"
The + operator makes no sense for TextField objects. You want
System.out.println(n1.getText() + n2.getText());
What would it mean to add two TextField Objects? The + operator in Java can only be used with operands that are numbers or strings. (Well, that's simplifying it a bit you can look up the exact rules in the Java language specification.)
If you want to calculate the sum of two numeric strings stored in the TextFields (I assume integers) you can use something like this:
Integer.parseInt(n1.getText()) + Integer.parseInt(n2.getText())
Of course, you should add some error handling.
Have you tried
System.out.println(n1.getText() + n2.getText());
The '+' opperator when used with Strings will concatenate them together.
In this case you are not dealing with Strings, but TextField's, which have the .getText() function attached to get the String representation of their value.

Display class in content pane in java

I have a general question about java. Because I want to create StronaGlowna.java (class) where I have place all buttons, check box and other GUI component which I want to display in main class. The first question is this right way, it's correct ? or maybe is better way to do this thing. My code look this:
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = -4575271483481196192L;
Container pane;
CardLayout layout;
public Main() throws FileNotFoundException, IOException {
layout = new CardLayout();
setLayout(layout);
pane = this.getContentPane();
/*Page: Strona główna */
JPanel newPanel = new JPanel();
pane.add("New", newPanel);
JButton przycisk = new JButton("Przycisk");
newPanel.add(przycisk);
...
In "pane.add("New", newPanel);" I want to display elements from:
package aplikacja.glowna;
import javax.swing.JButton;
import javax.swing.JPanel;
public class StronaGlowna {
public void StronaGlownaDisplay() {
JPanel newPanel = new JPanel();
JButton przycisk2 = new JButton("Przycisk");
newPanel.add(przycisk2);
}
}
Can I import/display all class StronaGlowna in main() something like a include in PHP ? What do You thing about my idea, it's correct or I'm wrong ? Thanks for help and discussion.
It sounds like the way Netbeans handling GUI. You may view the article in http://netbeans.org/kb/docs/java/quickstart-gui.html, it may help you understand how the GUI works since Netbeans can generate code for you. You can always import class and create object to access methods (often public methods) . I think it is not like a include in PHP. PHP include is like to directly include the source code, but jave is not.
First - Never, never, never, code in Main class. Call a method from it and then start your staff in another class. And, of course, don't extend it. And the constructor is neither a good idea. All of these are bad practices. Now, going into your problem, my suggestion is that you make StronaGlowna extend JPanel, and then obtain an instance of it through a public constructor, and use that instance as the parameter for the constructor of JScrollPane. That will make the scrollPane act as a 'screen' inside which you can see the contents of StronaGlowna, which is what I understand you're after.

Java syntax error on token ";" expected [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a small problem in my java code. Error is
Syntax error on token ";", , expected
Here is my code:
package natchly.chest;
import natchly.chest.blocks.BlockStoneChest;
import net.minecraft.block.Block;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.event.FMLInitializationEvent;
#Mod(modid="chestsplus", name="Chests+", version="1.4.6_01")
#NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class ModChests {
public int idBlockStoneChest = 250;
public static Block blockStoneChest; // <-------- Error here
blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
#Init
public void init(FMLInitializationEvent e) {
GameRegistry.registerBlock(blockStoneChest);
LanguageRegistry.addName(blockStoneChest, "Stone Chest");
}
}
Either do this:
public static Block blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
Or this:
public static Block blockStoneChest; <-------- Error here
static {
blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
}
Combine these two lines into one declaration and instantiation step. The way you're doing it isn't permitted in Java unless that's inside of a method.
public static BlockStoneChest blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);

Categories