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) {
}
}
Related
This question already has answers here:
Setting background color for a JFrame
(14 answers)
Closed 3 years ago.
I have tried to change the background color of a JFrame and it just never works. Here is my code:
package com.company;
import javax.swing.*;
import java.awt.*;
public class Main extends JPanel
{
private static JFrame frame = new JFrame();
private static int rand = 3;
public static void main(String[] args)
{
frame.getContentPane().add(new Main());
frame.setSize(1960, 1070);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setResizable(false);
}
}
You'll need to go through getContentPane to change the frame's background color:
frame.getContentPane().setBackground(Color.BLACK);
Change the color of the JPanel you are adding, not the JFrame. You won't see the black JFrame if you add a white JPanel on top of it!
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ChangeColor extends JPanel {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public static void createGUI() {
JFrame frame = new JFrame();
ChangeColor c = new ChangeColor();
c.setBackground(Color.BLACK);
frame.add(c);
frame.setSize(1960, 1070);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}
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();
}
I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel sidePanel;
public void setup()
{
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocation(100, 50);
mainPanel = new JPanel();
Dimension d = new Dimension(800, 600);
mainPanel.setMaximumSize(d);
mainPanel.setMinimumSize(d);
mainPanel.setPreferredSize(d);
frame.add(mainPanel);
frame.pack();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
}
public void setupPanels()
{
gamePanel = new JPanel();
Dimension d = new Dimension(600, 600);
gamePanel.setMaximumSize(d);
gamePanel.setMinimumSize(d);
gamePanel.setPreferredSize(d);
gamePanel.setBackground(Color.RED);
mainPanel.add(gamePanel);
sidePanel = new JPanel();
d = new Dimension(600, 600);
sidePanel.setMaximumSize(d);
sidePanel.setMinimumSize(d);
sidePanel.setPreferredSize(d);
sidePanel.setBackground(Color.BLUE);
mainPanel.add(sidePanel);
}
public void setupGame()
{
GameArea game = new GameArea();
gamePanel.add(game);
game.start();
}
public static void main(String[] args)
{
Main main = new Main();
main.setup();
main.setupPanels();
main.setupGame();
}
This question already has answers here:
Opening a new JFrame from a Button
(4 answers)
Closed 6 years ago.
I know this has been asked thousands of times, but I have never found an answer that works for me. I'm using Java IDE for Java Developers (Eclipse Kepler).
I need to have a JButton which by clicking it, it will close the JFrame that the button is on, and opens a new one that exists in a different class. I have this:
JButton button = new JButton("Click Me!");
add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
}
});
}
I have no idea what to put after the actionPerformed. And frame.dispose(); does not work for me.
I'm asking, how do I close the JFrame with a JButton, and by clicking the same button it also opens a new class's JFrame?
Here's an example that may help:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame() {
setLayout(new BorderLayout());
getContentPane().setPreferredSize(new Dimension(400, 250));
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame2.setLocation(300, 150);
frame2.add(new JLabel("This is frame2."));
frame2.setVisible(true);
frame2.setSize(200, 200);
}
} );
add(btn,BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.add(new JLabel("This is frame1."), BorderLayout.NORTH);
frame.setVisible(true);
}
});
}
}
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();
}
}