White bar at top of Jpanel undecorated screen - java

Image of white bar
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DisplayImage {
public static void main(String avg[]) throws IOException
{
DisplayImage abc=new DisplayImage();
}
public DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("SP.jpg"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setIconImage (new ImageIcon("SP.jpg").getImage());
frame.setResizable(false);
frame.setTitle("Demo");
frame.setLayout(new FlowLayout());
frame.setSize(1920, 1080);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I thought this would produce a full screen of the Jpanel image and no white bar. I used the undecorated Jpanel function to try to make this happen but the white line appeared.

Related

how to change the background color in a window application in java

I want to create an application where pressing a button changes the background color, but I don't know why the color won't change. I can't change it at all. Swapping the button color and its font works, but I can't change the background color.
package okno;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Kolory extends JFrame implements ActionListener {
static JButton FirstButton;
Color [] colors= {Color.GREEN,Color.RED,Color.BLACK};
JLayeredPane screen;
public Kolory() {
super("Zmiana kolorów");
screen = new JLayeredPane();
getContentPane().add(screen).setBackground(Color.RED);;
screen.setPreferredSize(new Dimension(500,500));
FirstButton = new JButton("Pierwszy przycisk");
screen.add(FirstButton);
FirstButton.setBounds(30, 30, 150, 20);
FirstButton.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new Kolory();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
int counter = r.nextInt(3);
screen.setBackground(colors[counter]);
}
}

Is it possible to add a image in JFrame but the class is extended with JPanel?

Here is my code, i can run it in class extended with JFrame. But now i need to add this code to a class extended with JPanel. Is it possible to add this in JPanel class? If cannot how can i add an image in JPanel class?
JLabel img;
String url = "image/Screenshot(295).png";
void Car() {
frame=new JFrame("Malaysia Checker");
frame.getContentPane().setBackground(Color.white);
img = new JLabel();
ImageIcon icon = new ImageIcon(url);
img.setIcon(icon);
img.setBounds(200, 200, 200, 200);
add(img);
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Simple answer is, yes, and you should. A JPanel is just a type of container, a JFrame is a specialised type of container, so many of the concepts are transferable.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBackground(Color.WHITE);
try {
// Warning, this is a blocking call and my slow down the launch/presentation of the view
BufferedImage background = ImageIO.read(new URL("https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/234px-Java_programming_language_logo.svg.png"));
JLabel label = new JLabel(new ImageIcon(background));
add(label);
} catch (IOException ex) {
ex.printStackTrace();
add(new JLabel("Could not load background image"));
}
}
}
}
Remember, in order to display any component, you need some kind of Window class, here I've just used a JFrame as the top level container
This kind of question would be better answer by reading through the Creating a GUI With JFC/Swing tutorials

cannot setVerticalTextPosition for JLabel?

I have an icon with text for JLabel, I am trying to get the text to vertically positioned at the bottom, but this can't work, this is my whole class:
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("my frame");
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("my label");
ImageIcon mouse = new ImageIcon("mouse.jpg");
label.setIcon(mouse);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
frame.add(label);
frame.setVisible(true);
frame.setSize(500, 500);
}
}
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("my frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("my label");
ImageIcon mouse = new ImageIcon("mouse.jpeg");
label.setIcon(mouse);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
frame.add(label);
frame.setVisible(true);
frame.setSize(500, 500);
}
}
Reference:
LabelTextPosition

image displaying in java

i have tried this code,the code is running but not showing any image..
i have saved this file as image.java
and also tell me if there is a problem in the adress..
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Image {
public static void main(String avg[]) throws IOException
{
image abc=new image();
}
public void DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("D:\\snakeandladder.jpg"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I am not good in programming,but don't you have to call the function by using object like abc.DisplayImage(); in main function.I am not sure about this..

how to add images to a panel at any location (x,y)

i want to add images or labels with image in it to a panel at any location which will be decided by the user clicking on the panel(add image where user clicks in the panel).
how to do this.
thanks
try (and tweak) this sscce:
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JLabelOnClick extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JLabelOnClick();
final JPanel panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// if you want an image instead, use the JLabel(Icon image)
// constructor
JLabel label = new JLabel("test");
label.setBounds(e.getX(), e.getY(), label.getPreferredSize().width, label
.getPreferredSize().height);
panel.add(label);
panel.validate();
frame.repaint();
}
});
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
}
set null as LayoutManager for that panel and then manually set position for each image.

Categories