Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm beginning to learn Java but I'm programming for almost six years with c, python, assembly, c++ and I've a problem with this example. I found that gives me this error in Eclipse:
MyFrame cannot be resolved to a variable
this is the example code:
MyFrame
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton tree=new JButton("3");
JButton four=new JButton("4");
JButton five = new JButton("5");
public MyFrame() {
super("FlowÃLayout");
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(one);
c.add(two);
c.add(tree);
c.add(four);
c.add(five);
setSize(300,100);
setVisible(true);
}
}
Application
public class Application {
public static void main(String args[]) {
/* THAT IS THE ERROR */ MyFrame = new MyFrame();
}
}
Just change
MyFrame = new MyFrame();
to
new MyFrame();
You forgot to declare the variable
MyFrame myFrame = new MyFrame();
answering as a community wiki since this is very basic java knowledge, and the question should be closed as a typo error.
Inside your main() method, you have this line:
MyFrame = new MyFrame();
The above line should instead be
MyFrame frame = new MyFrame();
Since you are not using the variable anywhere else in the program, you could as well use
new MyFrame();
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am trying to add a GIF to the frame. Here is my code but when I run it cannot see GIF. Is there a function to add GIF to java?
Any help would be appreciated.
public class confetti extends JFrame
{
private static JFrame frame;
public confetti() {
frame = new JFrame();
frame.setSize(510, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
addConfetti();
}
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("confetti-40.gif"));
frame.add(jpCenter,BorderLayout.CENTER);
lblgif.setVisible(true);
}
public static void main(String args[]) {
new confetti();
}
}
You never add your label to any frame/panel. In your code it might be something like this:
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("path/to/your/confetti-40.gif"));
frame.add(jpCenter, BorderLayout.CENTER);
jpCenter.add(lblgif);
lblgif.setVisible(true);
}
Also check your absolute path from your projecto to your gif file, your code in works for me on my PC.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm new to java and I wanted to create an app that opens a window and has two buttons that say differnt things. But there seems to be a problem and I do not know how to fix it.
Thanks for your help in advance.
package fenster;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Fenster {
public static void main(String[] args) {
JFrame myFrame=new JFrame ("This is my window.");
myFrame.setSize(300,400);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JPanel panel= new JPanel();
myFrame.add(panel);
JButton button= new JButton("Hello World!");
panel.add(button);
button.addActionListener(new Action1()); // there's an error here
JButton button2= new JButton("Hello again!");
panel.add(button2);
button2.addActionListener (new Action2()); // there's an error here
}
}
package fenster;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Fenster {
public static void main(String[] args) {
JFrame myFrame=new JFrame ("This is my window.");
myFrame.setSize(300,400);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
public Fenster(){
JPanel panel= new JPanel();
myFrame.add(panel);
JButton button= new JButton("Hello World!");
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
//code here
}
}
JButton button2= new JButton("Hello again!");
panel.add(button2);
button2.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
//code here
}
}
}
}
First don't add all your code under the main method its better to create a constructor or other methods instead of the main.
Second the reason you got an error because new action2, here you have to create a class called Action2 and class called Action1 . Both these classes should implement ActionListener.
Or
You can create an anonymous listener like the one that I did new ActionListener that way you dont have to create a class every time.
Check this for more info:
ActionListener
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Whilst following through the Java in Easy Steps book I came across a error whilst creating my first GUI.
import javax.swing.*;
public class Main extends JFrame{
JPanel pnl = new JPanel();
public Window() {
super("Menu Window");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(pnl);
setVisible(true) ;
}
public static void main(String[] args) {
Main gui = new Window();
}
}
And the error I get is:
'Error: Could not find or load main class main'
I am using Eclipse and this is the only thing that shows in the console.
The constructor name should be the same as the class name (thus Main not Window), So we have:
import javax.swing.*;
public class Main extends JFrame{
JPanel pnl = new JPanel();
public Main() {//Watch this line carefully (Window changed to Main)
super("Menu Window");
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(pnl);
setVisible(true) ;
}
public static void main(String[] args) {
Main gui = new Main();//Watch here too
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to write a small GUI program that consist of one JComboBox that has to paint the ContentPane with a color chosen from the combobox.
I do not know why it doesn't work as it is supposed to. When I start my main method it produces an empty JFrame even without the comboBox, which I know that I had added to the JFrame.
This is my main method:
import javax.swing.JFrame;
public class TestRGBComboBox {
public static void main(String[] args) {
JFrame frame = new RgbComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
this is my JFrame class
public class RgbComboBoxFrame extends JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;
private JComboBox colorComboBox;
private ActionListener listener;
public void RgbComboBoxFrame() {
colorComboBox = new JComboBox();
colorComboBox.addItem("RED");
colorComboBox.addItem("GREEN");
colorComboBox.addItem("BLUE");
colorComboBox.setEditable(true);
listener = new AddListener();
paintContentPane();
createPanel();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent event){
paintContentPane();
}
}
private void paintContentPane(){
Color c = (Color)colorComboBox.getSelectedItem();
getContentPane().setBackground(c);
}
private void createPanel(){
JPanel controlPanel = new JPanel();
colorComboBox.addActionListener(listener);
controlPanel.add(colorComboBox);
add(controlPanel,BorderLayout.SOUTH);
}
}
Although all the code is there to create the panel and add things to it you never call them. public void RgbComboBoxFrame(){} should be a constructor and therefore written as public RgbComboBoxFrame(){}
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'm wondering how to initiate an action if a jbutton is clicked on in my JFrame.
I've tried searching for answers but haven't had much luck.
This is all i have right now, i basically just want some text to be displayed upon clicking the button.
public class Slots {
public static void main(String[] args){
Slots();
}
public static void Slots(){
//JFRAME
JFrame f = new JFrame("Slots Game");
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
//JButton
JButton Button = new JButton("Start");
f.add(Button, BorderLayout.PAGE_END);
f.setVisible(true);
}
}
There are 3 ways to do that.
Create a class that implements the ActionListener interface. And then add an instance of that class as an action listener to the button.
Making the current class (in your case Slots) implement the ActionListener interface. And then adding "this" as the action listener to the button.
The third method, which is probably the most convenient/efficient method, is using an anonymous inner class like below.
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// your code goes here
}
});
For more details see ActionListener API