Code :
import javax.swing.*;
import java.awt.*;
public class firstGUI extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
Image image = new ImageIcon("dist.jpg").getImage();
g.drawImage(image,0,0, this);
}
}
Compiles perfectly, but when I run it, it just shows a form. No picture(or any other operation in paintComponent) shows up. Is there something I'm missing?
Your paintComponent method is an instance method of your firstGUI class (a JPanel). The problem is that you are not creating an instance of firstGUI and adding it to the frame.
The following replacement main method instantiates firstGUI and adds it to the contentPane of the frame:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().add(new firstGUI());
frame.setVisible(true);
}
Related
I am new to java, I wanted to make a program that draws a box on screen, everything is correct except for the paintComponent(); which is not working
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel
{
#Override //this section creates the box
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(150,150,20,20);
}
public static void createWindow() //this section creates the frame
{
JFrame frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set closing bebavior
frame.setSize(400, 400); //set the size of jframe
frame.setLocationRelativeTo(null); //center the jframe
frame.setVisible(true); //show the frame
}
//main method
public static void main(String[] args)
{
createWindow();//launch your creaWindow method
paintComponent();
}
}
You have to review the basics of Java. Your mistake in this is that you have to create an object of this class Frame and add it to the JFrame which you have created. Below is the correct code #Hh000.
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel{
#Override //this section creates the box
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(150,150,20,20);
}
//main method
public static void main(String[] args){
JFrame frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set closing bebavior
Frame frameObject = new Frame(); //Making a class object
frame.add(frameObject); //Adding the object into the JFrame
frame.setSize(400, 400); //set the size of jframe
frame.setLocationRelativeTo(null); //center the jframe
frame.setVisible(true);
}
}
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'm trying to put my text adventure in a GUI,and make it so there are no buttons just text and user input. This is the code for my GUI
import java.awt.*;
import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Component textLabel = null;
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createwindow(); }
}
I have my text adventure in a separate class and was wondering if there was any easy way to display that in this GUI. Thanks for your help.
import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Component textLabel = null;
JTextArea jta = new JTextArea();
frame.add(jta);
// frame.pack();
}
public static void main(String[] args) {
createwindow(); }
}
In my understanding, you just want a TextArea.Is right?
I'm trying to display a message in a JPanel.
I've used the drawString() function of the Graphics class.
Here's my code :
public class Frame {
JFrame frame;
JPanel panel;
Graphics graph;
Frame() {
frame = new JFrame();
panel = new JPanel();
frame.setTitle("My wonderful window");
frame.setSize(800, 600);
frame.ContentPane(panel);
frame.setVisible(true);
}
void displayMessage(String message) {
graph = new Graphics();
graph.drawString(message, 10, 20);
}
}
I've this error :
error: Graphics is abstract; cannot be instantiated
Override the JPanel's paintComponent(Graphics g) method. IN the method you have access to a valid Graphics instance. The method called on each paint.
But may be it's better to add a JLabel to the panel. The label initially has no text and when you have a message just call setText(messageText) of the label.
You should create subclasses for your JFrame and JPanel, and override the methods you want. You could try something like:
package test;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame {
public static final String message = "HELLO WORLD!!!";
public class Panel extends JPanel {
public void paintComponent(Graphics graph) {
graph.drawString(message, 10, 20);
}
}
public Frame() {
Panel panel = new Panel();
this.setTitle("My wonderful window");
this.setSize(800, 600);
this.setContentPane(panel);
this.setVisible(true);
}
public static void main(String[] args) {
new Frame();
}
}
Also, there are a lot of great books/tutorials about this. You should read one.
Edit:
You should also read about all the JComponents (JButtons, JLabels...). They're rather useful.
I started a new project, all the code is right (I think) and no window is appearing. There are no compilation errors, whenever I run the program nothing happens.
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame{
public static String title = "Tower Defense";
public static Dimension size = new Dimension(700, 550);
public static void main(String args[]){
Frame frame = new Frame();
}
public Frame() {
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
setVisible(true);
}
}
You never call init() method. How can your frame be visible?
Just change your main method to:
public static void main(String args[]){
Frame frame = new Frame();
frame.init();
}
You never make a call to init() in your frame constructor:
public Frame() {
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
**init();**
}
init() method will never be called in your program.
Set the visibility in your Frame() Constructor itself.
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame{
public static String title = "Tower Defense";
public static Dimension size = new Dimension(700, 550);
public static void main(String args[]){
Frame frame = new Frame();
}
public Frame(){
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}