Adding GIF to frame [closed] - java

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.

Related

Error while creating a GUI using Java Swing [closed]

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
}
}

Images in a JFrame [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone please give me an example code wich has a JFrame with an image and a something else in it, I just can't get the image working and I need an example that works. I tried some thing with buffered image and image icons but nothing works.
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test Image");
frame.setLayout(new BorderLayout());
try {
frame.add(new JLabel(new ImageIcon(new URL(YOUR_IMAGE_URL))),
BorderLayout.NORTH);
} catch (MalformedURLException e) {
e.printStackTrace();
}
frame.add(new JButton("Button"), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}

MyFrame cannot be resolved to a variable [closed]

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();

Adding components one after another to JPanel horizontally [closed]

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 8 years ago.
Improve this question
I am having problems with the components alignment added to JPanel. I am working on a chat application. Which layout should be used be used to get the intended aim. I tried all the layouts but I didn't get the wants. Mostly problems occur when window is resized. Moreover get the idea from the included image about what I want to be achieved.
Thanks in advance.
enter image description here
I whipped up a prototype using BoxLayout, for no other reason than I rarely use it and felt like trying it. Normally, GridBagLayout would be my first choice for this.
EDIT: Added a pic (thanks trashgod!)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MessageAppDemo implements Runnable
{
private String[] messages = new String[] {
"Hello?",
"Hey, what's up?",
"Where are you?",
"Right behind you.",
"Stop following me!",
"But you owe me money.",
"I'll gladly repay you on Tuesday.",
"You said that last week!",
"But now I actually have a job."
};
private int msgCounter = 0;
private JPanel panel;
private JScrollPane scrollPane;
private Timer timer;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new MessageAppDemo());
}
public void run()
{
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setAutoscrolls(true);
JFrame frame = new JFrame("Message App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(260, 180);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer = new Timer(1500, new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (msgCounter < messages.length)
{
addMessage(messages[msgCounter]);
}
else
{
timer.stop();
}
}
});
timer.start();
}
private void addMessage(String text)
{
boolean rightAligned = msgCounter % 2 != 0;
Color color = rightAligned ? Color.CYAN : Color.ORANGE;
JLabel label = new JLabel(text);
label.setOpaque(true);
label.setBackground(color);
label.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.BLUE),
BorderFactory.createEmptyBorder(2,4,2,4)
));
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setBorder(BorderFactory.createEmptyBorder(2,4,2,4));
if (rightAligned)
{
p.add(Box.createHorizontalGlue());
p.add(label);
}
else
{
p.add(label);
p.add(Box.createHorizontalGlue());
}
panel.add(p);
panel.revalidate();
int x = panel.getPreferredSize().width;
int y = panel.getPreferredSize().height;
panel.scrollRectToVisible(new Rectangle(x-1 ,y-1, 1, 1));
msgCounter++;
}
}

JFrame appears empty and minimised [closed]

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(){}

Categories

Resources