I've recently begun learning Java. I've taken the usual approach that I take, to dissect other code and learn how it works. My first goal is to simply animate a GIF, but I can't make heads or tails of this problem.
I followed the answer in this question. But when I compile and run the program, I get a blank window. This window does have the title assigned to it, but with no GIF inside. Here is my current code:
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
class giftest {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://i.imgur.com%2FTdRNfPP.jpg");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("j0in Dedsec!");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
I get no errors when compiling or running this code, and here is a screenshot of the window that shows:
Any help whatsoever is highly appreciated.
-Defalt
Related
so i am using intellij and am trying to make a jbutton with text. it works fine without the text but when i put the text on it takes up the whole jframe and i do not know why. if you could help me i would greatly appreciate it. here is my code. Edit thank you Manchi for your answer it worked perfectly and i am no longer looking for answers but i do not know how to close the question.
package com.company;
import javax.swing.*;
import java.awt.*;
class Fantasyrpglifesim implements JButton {
Fantasyrpglifesim() {
}
public static void main(String[] args) {
MouseInputAdapter();
//Frame//
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.setSize(1500000, 1500000);
frame.getContentPane();
frame.setVisible(true);
//Buttons//
frame.add(BUTTON).setBounds(570,500,150,150);
BUTTON.setText("Age up");
}
private static void MouseInputAdapter() {
}
}
You just need to change the layout of the JFrame. Add the next line to your code:
frame.setLayout(new FlowLayout());
Say I have got a image file, which shows a dog (just for example). During execution, I want to display the image located at "C:\image.png" (again, just saying) to be displayed by using System.out.println or basic GUI by using Swing class. I am curious to know about it, so if it is possible, it'd be so helpful if you could kindly explain. Same for if not possible. Thanks a ton!
EDIT: Is it impossible to include images in System.out.println?
This should work for images of dogs as well as for images of cats.
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ShowImage
{
public static void main(String[] args)
{
showImage("C:/image.png");
}
private static void showImage(final String fileName)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().setLayout(new GridLayout(1,1));
f.getContentPane().add(new JLabel(new ImageIcon(fileName)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
For images of horses, slight adjustments might be necessary.
I used the following code:
JLabel jLabel = new JLabel(new ImageIcon(someImage));
I don't get it.. sometimes the image appears when I run the code and sometimes not.. I'm not always getting the same output. Anyone can explain why this could happen?!
Without more code for context it's hard to know for sure, but whenever I hear about a Swing problem that sometimes works, I tend to suspect threading problems; if your GUI is, say, a dialog that is not being built on the Event Dispatch Thread then this sort of randomness is common. If you are not sure about your threading, put this at the top of your method where this code is being executed:
System.out.println(String.format("This code %s running on the Event Dispatch Thread.", (javax.swing.SwingUtilities.isEventDispatchThread() ? "IS" : "IS NOT"));
and see what you get.
Here is simple example for you with JLabel and Icon, examine that :
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Example extends JFrame {
public Example() {
URL resource = getClass().getResource("image.png");
ImageIcon icon = new ImageIcon(resource);
JLabel l = new JLabel(icon);
add(l);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String... s){
new Example();
}
}
image.png is my image, that is placed in the same folder as the class.
After done a LFS(linuxfromscratch) system, I has 2 problems with input, some applications like google docs (presentations edit in browsers) and some java apps apparently do not recognize the keyboard input, the first one was resolved adding UTF-8 locale, but the java no way.
So I do some research and limited it in a awt scope. It means all apps writed in java awt do not recognize the keyboard inputs.
I tried http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/awt.html , but no success to solve the issue.
I too recompiled the libXt after the new locale.
I don't have Qt, awt is qt dependent?
Using eclipse(which is java app and do not have the issue), I created a little app using awt to reproduce the problem. The problem is here but it's don't give a stack trace nor a message warning.
From this moment I haven't idea how to solve or track this issue.
Some help/tip?
Here a simple program to reproduce the problem (jdk1.7.0_21)
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
public class testmain extends java.applet.Applet{
public void init()
{
Panel p;
setLayout(new BorderLayout());
p = new Panel();
TextArea x = new TextArea();
x.setFocusTraversalKeysEnabled(true);
x.setText("asdf");
x.setEditable(true);
p.add(x);
add("Center", p);
p = new Panel();
p.add(new Button("One"));
p.add(new Button("Two"));
Choice c = new Choice();
c.addItem("one");
c.addItem("two");
c.addItem("three");
p.add(c);
add("South", p);
}
public static void main(String [] args)
{
Frame f = new Frame("Example 4");
testmain ex = new testmain();
ex.init();
f.add("Center", ex);
f.pack();
f.show();
}
}
Please see my image below at the link and then read below it for more details on my problem.
Imagine that is a Basic frame splited into two with a JSplitPane, by default when you resize your frame the gray part changes it's size, but I would like the white part to resize accordingly to the frame resizing.
Any help into the right direction would be appreciated as I am working on a project now and I am trying out all kind of weird stuff to be prepared for my biggest project set in the new year. :)
Regards
Theron
You need to use setResizeWeight to get the left and right take the extra space or reduce in size on JFrame resize, sample code below:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class TestJSplitPane {
private void init(){
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setRightComponent(new JButton("Here I Am"));
splitPane.setLeftComponent(new JButton("Me Too"));
splitPane.setResizeWeight(0.5);
frame.add(splitPane, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new TestJSplitPane().init();
}
}
Java Doc for setResizeWeight.
Hope this helps.