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);
}
}
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'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 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);
}
}
I was able to hide the the maximize icon by setting setResizable(false), how can achieve the same for the minimize icon?
Here is the way, how to remove min/max buttons from JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JFrameTest extends JDialog {
public JFrameTest(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
JFrameTest frame = new JFrameTest(new JFrame(), "Title");
JPanel panel = new JPanel();
panel.setSize(200, 200);
JLabel lbl = new JLabel("JFrame without max/min");
panel.add(lbl);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
But it's really nasty to users and could be walk arounded by key shortcuts etc.
Check the example Specifying Window Decorations
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);
}