My image for logo doesnt show up on mac. there is no java logo in Jframe to begin with?
what am I missing?
package test;
import javax.swing.*;
public class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("this is the frame title");
frame.setSize(420,420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
ImageIcon image = new ImageIcon("logo.png");
frame.setIconImage(image.getImage());
}
}
the mac OS will not show your icon, there's nothing wrong with your code, what you can do is enable the look and feel for cross platform. here at the docs you can see how achieve this and configure it. Basically before start your jframe do this:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName();
// you can even set a theme on it
MetalLookAndFeel.setCurrentTheme(new OceanTheme()); // since Metal is the L&F for cross platforms.
JFrame.setDefaultLookAndFeelDecorated(true);
Related
I'm fairly new to coding and was trying to make a simple GUI with JAVA Swing. I wanted to place an image in a label but for some reason does the image does not show when I run the Code. Are there some requirements I don't know of or did I do something wrong?
package Part2Lables;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Label = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("Kakashi.png");
JLabel label = new JLabel(); // create a label
label.setText("This is text in a label"); // set text to the label
label.setIcon(image);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);
}
}
This is the code I used (I did use imports but they are not visible in the pic.)
I am trying to display an image icon PNG, I have properly referred to the file called "logo.png" inside the project folder and set the icon as seen below. I'm not sure where I went wrong as I followed this tutorial starting around minute 14 (https://www.youtube.com/watch?v=Kmgo00avvEw&t=298s), I have made sure the code was correct but I'm still unable to get the image to appear beside the text when I run the code in Eclipse.
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class mainClass {
public static void main(String[] args) {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
ImageIcon image = new ImageIcon("logo.png");
JLabel label = new JLabel(); //creates JLabel
label.setText("Welcome to Magic Shape"); //sets text for label
label.setIcon(image);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(500, 500); //sets size of JFrame (can resize)
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(label); //adds label
}
}
package be.project.jframe;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws IOException {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
InputStream stream = Main.class.getResourceAsStream("/be/project/jframe/icons/400.png");
ImageIcon icon = new ImageIcon(ImageIO.read(stream));
JLabel jLabelObject = new JLabel("Welcome to Magic Shape");
jLabelObject.setIcon(icon);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(800,600);
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(jLabelObject); //adds label
frame.pack();
}
}
According to your code, Java searches for the file logo.png in the working directory. The working directory is the value returned by System.getProperty("user.dir"). If Java does not find the file, it essentially creates a null icon and that's why you aren't seeing the icon.
Just make sure you put the file in the correct directory.
Refer to the following.
Accessing Resources
Retrieving Resources
How to Use Icons
Make sure the png file is placed at current working directory:
System.out.println(System.getProperty("user.dir"));
I'm writing a simple Java code that shows a text inside a frame and substitutes the standard ImageIcon with a custom one (.png file), stored in the same directory of this Java file.
However, whenever I run the code no text appears inside the frame and I also noticed that no Image is shown as Icon.
I'm running Java on VsCode and the code is as follows:
public class Example{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(420,420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("icon.png");
frame.setIconImage(image.getImage());
Jlabel label = new Label();
label.setText("Hello there!");
frame.setVisible(true);
}
}
Needless to say that I'm importing javax.swing.ImageIcon, javax.swing.JFrame and javax.swing.JLabel.
Whenever I run the above code this is the frame that appears:
May this be a problem due to Virtualization not being set on on my computer?
I'm fairly new to working with graphics in Java, and have been trying to make a simple console to display text based games in a window. I have a test class, where I'm working on the console, but when I add a JTextArea to my console window, it either takes up the entire window or doesn't display at all.
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.Event;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
JTextArea jta = new JTextArea(100,100);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
frame.add(jta);
}
}
I know that some of my imports aren't used in this file, but they will used in the final game. I am also aware that the JTextArea is set to size 100,100, which I am unsure whether it is too large or small. I could really use some help on this, though.
Your problem is the installed by default BorderLayout in your frame. The easiest way to solve your problem is to set another layout manager.
public static void main(String[] args) {
SwingUtilities.invokeLater(GUI::startUp);
}
private static void startUp() {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
frame.setLayout(new FlowLayout()); // FlowLayout is required
JTextArea jta = new JTextArea(40,40);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
// JScrollPane to get the scroll bars when required
frame.add(new JScrollPane(jta));
// setVisible should be last operation to get a correct painting
frame.setVisible(true);
}
Please take a look for layout managers in Swing
The native Windows LookAndFeel in Java 6 seems to incorrectly size some fonts.
Test program:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Test {
public static void main(String[] arg) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
final JMenuBar mb = new JMenuBar();
final JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
mb.add(file);
final JToolBar toolbar = new JToolBar();
final JButton button = new JButton("Button");
toolbar.add(button);
final JLabel label = new JLabel("Basic Colors");
final JPanel panel = new JPanel(new BorderLayout());
panel.add(toolbar, BorderLayout.PAGE_START);
panel.add(label, BorderLayout.CENTER);
final JFrame frame = new JFrame();
frame.setJMenuBar(mb);
frame.add(panel);
frame.setTitle("Test");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Output, compared with a native Windows app on Vista:
While the text in the test app menu bar is sized correctly, the rest of the text is smaller than in the native app next to it. Zoomed in, you can see that the text in the test app JLabel is 1px shorter than in the native app:
Is this a bug in the Windows LaF, or are we misusing it? If it's a bug, is there a known workaround?
Java 6 uses its own font renderer, including the implementation of subpixel antialiasing / hinting whatsit. While the output is meant to be similar to Windows' rendering, either the top of the B being at a pixel boundary, or it being rounded, or both, throws the Java renderer off. The Windows font renderer decides to place the top of the letter above the boundary, while the Java one places it below. The 'l' looks like it's at the same height in both samples, so it doesn't seem like the renderer gets the height of every letter wrong. Maybe try comparing with some letters where the top is a straight line, like a T or an E?