How do you to create a JFrame with simple text? - java

I just want to make a JFrame that will say "Hello world", nothing big, no interaction needed. How do I do this?
I can create the JFrame, however I do not know how to put a JPanel with simple text in it.
Here is what I got so far
JFrame frame = new JFrame("Relief Valve");
frame.setResizable(false);
frame.setLocation(500,300);
JPanel p1 = new JPanel();
frame.setVisible(true);

Instead of creating the JPanel, try:
JLabel label = new JLabel("this is my text");
frame.add(label);
frame.pack();

JFrame window = new JFrame("Hello World App");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.add(new JLabel("Hello World"), BorderLayout.CENTER);
window.pack();
window.setVisible(true);
window.setLocationRelativeTo(null);
I'm currently on a mobile device but I'll be happy to document that when I get on a computer, feel free to ask any questions though.

You need to create a JLabel (witch is from Swing library) the code for that is:
JLabel label = new JLabel("Hello world");
if u want to set it to a specific loaction you need to create a render method :
public void render(Graphics g){
g.drawString(label,x,y);
}
the x and y is the position of the string with your label.

In order to create a JFrame with a simple text, you have to create a label and attach it to your frame.
Let's assume you have a JFrame created:
JFrame myFrame = new JFrame("My Frame");
Let's create the text label:
JLabel myLabel = new JLabel("Text");
To change a text of already created label:
myLabel.setText("New Text");
And eventually to clear the label:
myLabel.setText("");
Let's connect all the dots:
myFrame.add(myLabel, BorderLayout.CENTER);
myFrame.pack();
To learn more about JFrames, check: https://javatutorial.net/swing-jframe-basics-create-jframe

Related

Program with JFrame won't start

I was trying to write a program, where you put some numbers in a JTextField an then it does something with it. I tried figuring out how to even make an input possible. But the way I'm trying it doesn't work despite Eclipse showing no errors. And yes, I know there is no way to stop this program but this is just a test.
import javax.swing.*;
public class NotenEingabe extends JFrame {
public static void main(String[]args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("text");
JTextField field = new JTextField("text");
panel.add(label);
panel.add(field);
frame.setTitle("Grade input");
frame.pack();
frame.add(panel);
frame.setVisible(true);
}
}
I hope it is a real problem and not simply my tiredness.
frame.setVisible(true);
frame.add(panel);
Components should be added to the frame BEFORE the frame is made visible.
The layout manager is not invoked so the components have a size of (0, 0) which means there is nothing to paint.
frame.setTitle("Grade input");
frame.add(panel);
//frame.setSize(700, 700);
frame.pack();
frame.setVisible(true);
First of all when you extend your class from jframe you dont need to instantiate new jFrame just make a new instance of your class then access jframe with that or just delete extend Jframe and use functional approach.
The you didn't set de layout of pane
Test this one then you can choose from veriety of layouts
panel.setLayout(newFlowLayout());

JFrame content displayed with a delay

I have an applet with a main view and multiple additional pop-up JFrames.
One JFrame (Report() ) takes more than 20 seconds to load because of an SQL query.
I would like to display a pop-up JFrame which would warn the user, that the requested window is loading, but the content of the pop-up JFrame is displayed only after the asked JFrame was loaded.
This is the code that I have:
JFrame frame = new JFrame("Loading");
JLabel label = new JLabel("My label");
label.setFont(new java.awt.Font("Arial", 1, 16));
label.setText("<html> Please wait while </br> report loads </br>");
JPanel top = new JPanel();
top.add(label);
top.setBackground(Color.WHITE);
frame.add(top);
frame.setBackground(Color.WHITE);
frame.pack();
frame.setSize(200, 200);
frame.setVisible(true);
new Report();
In Report I do not have a main() method, I initialize it from the constructor directly with:
// setting up the cross-platform look and feel
preInitComponents();
// setting up the UI
initComponents();
// visualizing the jframe
pack();
setVisible(true);
Any suggestions what am I doing wrong?

Change dimensions of JButton

I have problem with this. The button is taking up the entire JFrame. I've tried changing the dimensions of the JFrame and the JButton but with no changed. It's completely hiding a JTable underneath. Could someone please tell me what I'm doing wrong.
JFrame FRAME = new JFrame();
JButton BUTTON = new JButton("OK");
FRAME.add(new JScrollPane(TableName));
BUTTON.setPreferredSize(new Dimension(20,30));
FRAME.add(BUTTON);
FRAME.setSize(700, 600);
FRAME.setVisible(true);
FRAME.setLocationRelativeTo(null);
The default layout manager for a a JFrame is a BorderLayout. You are attempting to add two component the CENTER which is not allowed. Only the last one added will be displayed.
You need to specify constraints when adding components to a BorderLayout. Your code should be something like:
frame.add(new JScrollPane(TableName), BorderLayout.CENTER);
button.setPreferredSize(new Dimension(20,30));
frame.add(button, BorderLayout.PAGE_START);
Also, variables names should NOT be upper cased. Follow Java convention.
Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples. The tutorial code will also show you how to better structure your program so you follow Swing coding conventions.
Don't add the button straight to the frame, create a JPanel first, add the button to the panel then add the panel to the frame.
JFrame frame = new JFrame();
// frame.setBounds(x axis, y axis, weight, height)
frame.setBounds(10,10,304,214);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Button");
button.setBounds(98, 75, 126, 39);
frame.getContentPane().add(button);
frame.setVisible(true);

JButton added to JPanel doesn't show

I can't seem to add a JButton to a JPanel.
I have a PropWindow (JFrame) that has a PropView (JPanel) in it. the PropView-JPanel seems to be added correctly because I can draw shapes on it with paint().
But when I use this to try adding a button it just won't show up att all :/
JButton testButton;
public PropView(int width, int height) {
super(true);
setLayout(null);
setSize(width, height);
//TestButton
testButton = new JButton("Test");
testButton.setLocation(10,10);
testButton.setSize(100, 50);
testButton.setVisible(true);
add(testButton);
setFocusable(true);
setVisible(true);
}
The JFrame and the JPanel are both 250x600 px.
I can't tell from the code snippet you posted but just in case: make sure you call pack () on the frame after you have added the panel or any other components.
Also, it's usually discouraged to extend a JPanel or JFrame, unless you have a good reason to do it, just a heads up.
Here you have a short tutorial about displaying frames:
And some sample code in it that might help:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
Make sure you added PropPanel to PropWindow using myPropWindow.getContentPane().add(myPropPanel), not just myPropWindow.add(myPropPanel).

A Small Guessing Game GUI opens in two windows

Alright so here's the problem:
I created this small guessing game and wanted to make it gui based...
but it appears in two different windows -
first window is the menu(button and label)
second window is activated by the button and has the game in it.
Is there a way for me to have both windows in one- AS IN the first window gets overwritten by the second window?
public class Skeleton extends JFrame implements ActionListener
{
JPanel glass = new JPanel();
JButton btn = new JButton("Start");
TextField tf = new TextField();
JLabel label = new JLabel("Enter Guess Here: ");
JLabel answerLabel = new JLabel("Answer:...");
Board bob =new Board();
public Skeleton()
{
setIconImage(new ImageIcon("icon.png").getImage());
getContentPane().setBackground(Color.darkGray);
getContentPane().setForeground(Color.black);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
add(label);
label.setForeground(Color.black);
add(tf);
tf.setText("");
add(btn);
btn.setBackground(Color.green);
btn.addActionListener(this);
add(answerLabel);
answerLabel.setForeground(Color.black);
setTitle("Guessing Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
public void actionPerformed(ActionEvent e)
{//GAME FRAME
JFrame frame = new JFrame();
int userGuess= Integer.parseInt(tf.getText());
frame.add(bob);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,285);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("icon.png").getImage());
...........
This is obviously not the full code, just the part that uses the GUI.
NOTE: I do have some other within this actionevent which occur after the button is clicked.
Also this is my first game ever :D (if i can call it one)
You create new JFrame into "actionPerformed" method, so it's normal to have two frames (the Skeleton instance and the new one).
Just use "this" instead of a new JFrame instance to keep only one JFrame instance.
Each JFrame will create a new window. If you want to display everything into the same window, create only one JFrame and change its content.
You can change the whole content of a JFrame by setting its contentPane (frame.setContentPane(...)) or by adding/removing Panel to it.
I suggest to take a look at JPanel since you will use it a lot.

Categories

Resources