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();
}
}
Related
I'm trying to set a Icon on the top left corner for my Window. but don't seem to work.. what do you think seems to be the problem?
I also tried setting an Icon and Text on an instance of JLabel but only the text came out.
The link is a screenshot for the output of the JFrame I made.
public class Window extends JFrame{
private final int WIDTH = 600;
public Window(){
// Setting the Window
setTitle("Module Organizer");
ImageIcon img = new ImageIcon("checkmark-square.png");
setIconImage(img.getImage());
setSize(WIDTH,(WIDTH*3)/4);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// adding components
add(new NavPane(), BorderLayout.WEST);
setVisible(true);
}
}
Here is the code
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Window{
JFrame f=new JFrame("Test");
public Window(){
JPanel j=new JPanel();
f.setSize(1000,500);
ImageIcon img = new ImageIcon("Image.jpg");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel l=new JLabel(img);
j.add(l);
f.add(l);
f.setVisible(true);
}
public static void main(String[] args) {
new Window();
}
}
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();
}
I'm a complete noobie with swing. I'm trying to set a few JPanels and TextAreas to show up but after spending 2 days reading the APIs and trying to add panels to frames and textareas to panels and nothing is showing up.. I'm utterly confused. If anyone could explain how is the best way to do this I would be very grateful
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout()); // J FRAME
JPanel panel = new JPanel(); // first panel on the left
panel.setLayout(new BoxLayout(panel, 1));
// frame.getContentPane().setBackground(Color.red);
frame.add(panel);
JLabel surname = new JLabel();
JLabel initial = new JLabel();
JLabel ext = new JLabel();
surname.setOpaque(true);
initial.setOpaque(true);
ext.setOpaque(true);
frame.add(surname);
panel.add(initial);
panel.add(ext);
JTextArea table = new JTextArea();
table.setEditable(false);
panel.add(table);
table.setVisible(true);
You're adding stuff to the JFrame after it's already visible. If you do that, you need to revalidate your JFrame so it knows to redo its layout.
You could also just wait to show your JFrame until after you've added everything.
Edit: Here is an example program that shows what I'm talking about. Try running this, then take out the call to revalidate() to see the difference.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton show = new JButton("Show");
show.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent showE) {
frame.add(new JLabel("Test"), BorderLayout.SOUTH);
frame.revalidate(); //tell the JFrame to redo its layout!
}
});
frame.add(show);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
You are adding an empty elements like:
JLabel surname = new JLabel();
Your elements is already added but have nothing to be display.
Try :
JLabel surname = new JLabel("UserName");
JLabel initial = new JLabel("Iinitial");
JLabel ext = new JLabel("Ext");
JTextArea table = new JTextArea(10, 5);
I'm making a small game and at the beginning i want to have JCheckBox for choosing the language(after that they are few more of them for setting the game) and above that a jlabel with picture with name of the game OR draw an image there, the problem is that i dont know any other way how to center the panel with checkboxes then to use GridBagLayout and when i use this, i cannot draw anything to the frame, id like to also remove those grey lines around the checkboxes if its possible, appreciate any help, thanks.
This is my second question here and i cant add images yet so here is a link to the picture :
here is code for the frame
private GamePlan plan;
private JFrame frame;
private String language;
private JPanel panel;
private JCheckBox englishBox;
private JCheckBox germanBox;
public Settings(GamePlan plan){
this.plan = plan;
frame = new JFrame();
frame.setSize(600, 500);
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.setResizable(false);
frame.setVisible(true);
panel = new JPanel(new GridLayout(2, 1));
englishBox = new JCheckBox("English", false);
germanBox = new JCheckBox("German", false);
englishBox.addActionListener(new EnglishLanguage());
germanBox.addActionListener(new GermanLanguage());
panel.add(englishBox);
panel.add(germanBox);
englishBox.setOpaque(false);
germanBox.setOpaque(false);
panel.setOpaque(false);
frame.add(panel);
frame.getContentPane().setBackground(new Color(216,252,202));
}
" the problem is that i dont know any other way how to center the panel with checkboxes then to use GridBagLayout and when i use this, i cannot draw anything to the frame"
I can't really tell what you're doing wrong without a complete example. I don't even see where you're trying to add the image. But don't try and draw on the frame. Draw on a JPanel instead.
Here is an example you may be able to gain some insight from.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class ImageByDrawing {
public ImageByDrawing() {
ImagePanel imagePanel = new ImagePanel();
imagePanel.setBorder(new TitledBorder("Drawn Image onto JPanel"));
JCheckBox germanBox = new JCheckBox("German");
germanBox.setOpaque(false);
JCheckBox englishBox = new JCheckBox("English");
englishBox.setOpaque(false);
JPanel boxPanel = new JPanel();
boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
boxPanel.setOpaque(false);
boxPanel.add(germanBox);
boxPanel.add(englishBox);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(imagePanel, BorderLayout.CENTER);
centerPanel.add(boxPanel, BorderLayout.SOUTH);
centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
centerPanel.setOpaque(false);
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.add(centerPanel);
mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
mainPanel.setBackground(new Color(216,252,202));
JFrame frame = new JFrame();
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class ImagePanel extends JPanel {
BufferedImage img;
int dWidth;
int dHeight;
public ImagePanel() {
try {
img = ImageIO.read(getClass().getResource("/resources/stackblack.jpg"));
dWidth = img.getWidth();
dHeight = img.getHeight();
} catch (IOException ex) {
Logger.getLogger(ImageByDrawing.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
}
#Override
public Dimension getPreferredSize() {
return (img == null) ? new Dimension(300, 300) : new Dimension(dWidth, dHeight);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ImageByDrawing();
}
});
}
}
Also I don't know why you prefer to draw the image. The same can be easily done with a JLabel and ImageIcon
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class ImageByDrawing {
public ImageByDrawing() {
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackblack.jpg"));
JLabel label = new JLabel(icon);
label.setBorder(new TitledBorder("JLabel with ImageIcon"));
JCheckBox germanBox = new JCheckBox("German");
germanBox.setOpaque(false);
JCheckBox englishBox = new JCheckBox("English");
englishBox.setOpaque(false);
JPanel boxPanel = new JPanel();
boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
boxPanel.setOpaque(false);
boxPanel.add(germanBox);
boxPanel.add(englishBox);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(label, BorderLayout.CENTER);
centerPanel.add(boxPanel, BorderLayout.SOUTH);
centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
centerPanel.setOpaque(false);
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.add(centerPanel);
mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
mainPanel.setBackground(new Color(216, 252, 202));
JFrame frame = new JFrame();
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ImageByDrawing();
}
});
}
}
The last part of your question, as #Jere pointed out you can use setFocusPainted for the check box germanBox.setFocusPainted(false);