Java - Want window that is blank - java

I'm still very new to Java.
In C# we have the ability to just go and set the form border style like this:
this.FormBorderStyle = FormBorderStyle.None;
How can I accomplish this.

You can use the JFrame method setUndecorated(true). Here is an example:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("TitleLessJFrame");
frame.getContentPane().add(new JLabel(" HEY!!!"));
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
}

Related

Unable to add button to frame GUI

I am trying to add a simple JButton to a JPanel in my program. The problem is when I run the problem, I do not see any button at all.
This is my code:
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuiStopwatch {
public GuiStopwatch() {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
May I know what did I do wrong and how do I fix this?
You are not adding the panel to the frame at any point.
EDIT
Here is the code you would need if you wanted it in a separate method:
import javax.swing.*;
import java.awt.*;
public class GuiStopwatch {
private static void stopwatch(JFrame frame) {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
frame.add(panel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
stopwatch(frame);
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
You could swap things, create everything you need for your frame on the constructor, what makes the code more organized and also you can use it in other Classes, putting on main method will limit what you can do and makes the code not organized
See here an example:
public GuiStopwatch() {
setTitle("Stopwatch");
setSize(600, 600);
// Create JButton and JPanel
JButton button = new JButton("START");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GuiStopwatch guistopwatch = new GuiStopwatch();
}

Why is my frame empty when I run? [duplicate]

This question already has answers here:
JFrame not presenting any Components
(4 answers)
Closed 7 years ago.
When i run this code:
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public void main(String args[]){
new Menu();
}
Menu(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setSize(500, 300);
frame.setVisible(true);
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
}
public void actionPerformed(ActionEvent e){
}
}
The window opens but it does not show anything... Where is my label? I am very lost because I have done various GUI before and either I am being stupid or i don't know! Sorry if its a stupid question, I'm just so stuck I had to post this.
Your code works for me, but I think it doesn't for you because you add a component after you set the frame visible. Call frame.setVisible(true) (and setSize) after
frame.add(north, BorderLayout.NORTH);
north.add(logo);
So your code should look like this (also formatted it properly):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public static void main(String args[]) {
new Menu();
}
public Menu() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
frame.setSize(500, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
}

Java setBounds() method (JFrame)

Hi Im using the setBounds method to open a window whith a certain size. But the size that I pass in the argument is the size of the window including the bars of the frame. How can I set the dimensions only for the content?
Set the size of the content, then call pack() on the JFrame.
Edit: Because Guillaume Polet will not stop griefing me, here is a complete working example. Notice how you don't need to (mis)use inheritance at all, and it gets the job done in much fewer lines:
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main {
public static void main(String... args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
}
}
There are 2 ways to fix this:
Take into account the border of the frame when setting the bounds of the frame
Override getPreferredSize() of the content pane of the JFrame and then call pack() on the frame.
Here is a demo of the two techniques:
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
protected void initUI() {
JFrame frame = new JFrame("Insets technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Insets i = frame.getInsets();
System.out.println(i);
Rectangle bounds = new Rectangle(50, 100, 400, 500);
bounds.width += i.right + i.left;
bounds.height += i.bottom + i.top;
frame.setBounds(bounds);
}
protected void initUI2() {
JFrame frame = new JFrame("Preferred size technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 500);
}
});
frame.pack();
frame.setLocation(50, 100);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Test test = new Test();
test.initUI();
test.initUI2();
}
});
}
}

How to create a JFrame with a title

How do you create a JFrame with a title using Java Eclipse? I've tried using
package frame;
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setText("abc");
}
Perhaps using the setTitle method
frame.setTitle("abc");
or alternatively
JFrame frame = new JFrame("abc");
This is the way to create a JFrame with title:
JFrame frame = new JFrame("Title");
This is the way to create a JFrame and set its title:
JFrame frame = new JFrame();
frame.setTitle("Title");
And this is the first result of Google for create jframe with title
You could use .setTitle(); to set the title.
import javax.swing.*;
import javax.awt.*;
public class MyFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("abc");
frame.setSize(100,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

JLabel as Background Image

I can't seem to figure this out.
Please help I need this to work out to continue my project.
Awww I have to add this for allowing me to post
import javax.swing.*;
import java.awt.*;
#SuppressWarnings("serial")
public class MainFrame extends JFrame {
public static void Draw(){
DrawFrame();
}
public static void DrawFrame(){
int h = 600;
int w = 340;
JFrame frame = new JFrame();
JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png"));
frame.setResizable(false);
frame.setSize(h, w);
frame.setTitle("MarioCraft");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(background1);
background1.setVisible(true);
background1.setIcon(new ImageIcon("/res/mariocraft_main.png"));
background1.setText("Background failed to load");
}
}
A JLabel always displays the image at its actual size so you should not be manually setting the size of the frame.
Instead the code should be something like:
JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png"));
JFrame frame = new JFrame();
frame.add(background1);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
You need to add the JLabel instance to the JFrame before you realize it (i.e. make it visible). Also, remove these three calls:
background1.setVisible(true);
background1.setIcon(new ImageIcon("/res/mariocraft_main.png"));
background1.setText("Background failed to load");
They are completely unnecessary. Also, another approach to setting a background image to a component is to override it's paintComponent method and draw the image directly to it's Graphics object.
Do you want to set JLabel as background image for the JFrame. Then,
frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg"));
See a sample code snippet taken for here
frame.setLayout(new BorderLayout());
frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg")));
frame.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
frame.add(l1);
frame.add(b1);
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Mainframe extends JFrame
{
public JLabel image ;
public Container c;
public Mainframe()
{
c=this.getContentPane();
image=new JLabel(new ImageIcon("bg.jpg"));
image.setSize(500, 550);
c.setLayout(new FlowLayout());
c.add(image);
add(image);
this.setSize(500, 550);
this.show();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new Mainframe();
}
}

Categories