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
}
}
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have stucked to 4 lines of code.
They supposed to let me create my own widget.
But it seems that the main method is missing.
I have tried to write a main class on my-own and create an object in paintComponent() method with no luck.
I know that i am not supposed to call the method myself, but when i run the program i get the error Main method not found in class....
Can someone please explain me how this works or give me a link to read?
Here is the simple code that I tried:
import javax.swing.*;
import java.awt.*;
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
}
Try this simple code, it should be helpful to you:
import javax.swing.*;
import java.awt.*;
public class MyDrawPanel extends JPanel {
private static final int WIDE = 300;
private static final int HIGH = 200;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
public MyDrawPanel() {
setBackground(Color.white);
// Set a initial size for the program window
this.setPreferredSize(new Dimension(WIDE, HIGH));
}
private void display() {
// Some statements to let the JFrame appear in a good way
JFrame f = new JFrame("MyDrawPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
// Main method is called when the program is runned
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MyDrawPanel().display();
}
});
}
}
Output:
If you are new to java, I would suggest to just give 4-5 days on Tutorials | Javatpoint.
Java rule is that the java file name should match the class name containing main method.
So a simple code of main method:
class Simple {
public static void main(String args[]) {
System.out.println("Hello Java");
}
}
In this case this code should be in Simple.java file.
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();
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