so i am using intellij and am trying to make a jbutton with text. it works fine without the text but when i put the text on it takes up the whole jframe and i do not know why. if you could help me i would greatly appreciate it. here is my code. Edit thank you Manchi for your answer it worked perfectly and i am no longer looking for answers but i do not know how to close the question.
package com.company;
import javax.swing.*;
import java.awt.*;
class Fantasyrpglifesim implements JButton {
Fantasyrpglifesim() {
}
public static void main(String[] args) {
MouseInputAdapter();
//Frame//
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.setSize(1500000, 1500000);
frame.getContentPane();
frame.setVisible(true);
//Buttons//
frame.add(BUTTON).setBounds(570,500,150,150);
BUTTON.setText("Age up");
}
private static void MouseInputAdapter() {
}
}
You just need to change the layout of the JFrame. Add the next line to your code:
frame.setLayout(new FlowLayout());
Related
I'm trying to make chess in java, but I'm stuck on the panel's size and location. When I try to change any of them, it does nothing and I don't know why. Could someone explain it to me?
package Chess;
import javax.swing.*;
import java.awt.*;
public class Window extends Component{
public void addChessWindow() {
JFrame chessWindow = new JFrame("Chess");
chessWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chessWindow.setResizable(false);
chessWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
chessWindow.setUndecorated(true);
chessWindow.setBackground(Color.decode("#4D6713"));
chessWindow.setVisible(true);
JPanel field = new JPanel();
field.setBackground(Color.BLACK);
field.setBounds(30,15,2,1);
chessWindow.add(field);
}
}
Note: everything starts in the main method.
I think you could do something like this:
chessWindow.add(field, BorderLayout.CENTER);
I'm trying to followed java tutorials and now I am going over JFrame.
This is a information inquiry more than help question.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login {
public static void main(String[] args){
//Creating object of LoginFrame class and setting some of its properties
LoginFrame frame = new LoginFrame();
frame.setTitle("LoginForm");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This code will cause the frame to be resized to a very small size at the top left corner regardless of the bounds I set.
A simple fix for this is to place frame.setResizable() before setting its bounds.
Does anyone know why this happens or am I doing something wrong?
I'm also on Ubuntu 20.04, maybe this matters but I haven't found an answer.
Tutorial shows above code.
The following is the code for LoginFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Creating LoginFrame class
public class LoginFrame extends JFrame implements ActionListener {
//Creating constructor of LoginFrame() class
LoginFrame(){
}
//Overriding actionPerformed() method
#Override
public void actionPerformed(ActionEvent e){
}
}
Like I was saying I was only following a tutorial. This was only the beginning of the tutorial but I had the same issue when starting another very simple frame tutorial.
The following works fine for me :
import javax.swing.*;
class Scratch extends JFrame {
public Scratch() {
super();
}
public static void main(String[] args){
//Creating object of LoginFrame class and setting some of its properties
Scratch frame = new Scratch();
frame.setTitle("LoginForm");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Result : I see a big rectangular window - in the shape of a smart phone screen I'd say.
setResizable(false) means you cannot resize the frame. I suspect the problem you're trying to identify lies somewhere in the LoginFrame class... no code for this was included though so hard to comment furhter.
This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 7 years ago.
I am trying to read a file with a scanner and create a JButton for each new line in the file. After I create a button, I add it to the frame. However once I run the program, only the most recent button appears. I'm not sure why creating the buttons in a loop causes this to happen. If anyone has an explanation for why this happens, that would be very much appreciated, thanks!
import java.awt.FlowLayout;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class PointOfSale extends JFrame {
ArrayList<JButton> menuButtons = new ArrayList<>();
public PointOfSale(File menu) throws IOException{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
Scanner sc = new Scanner(menu);
while (sc.hasNextLine()){
String name = sc.nextLine();
JButton menuButton = new JButton(name);
frame.add(menuButton);
menuButtons.add(menuButton);
}
sc.close();
frame.pack();
frame.setVisible(true);
}
}
Your code does not respect layout managers as the JFrame uses a BorderLayout which will only display one button. Read the layout manager tutorials, use different ones, here perhaps a GridLayout, and you'll likely have your problem solved.
//public class PointOfSale extends JFrame {
public class PointOfSale {
There is no need for your class to extend JFrame since you create the JFrame in the class.
Then you need to use:
frame.setLayout(new FlowLayout());
since this is the frame you add the buttons to.
today i try to do a example of a "Window" on Java. I try to Concat the Title but my "GetTitle()" don't work! Anyone can help me with this?
And why "public class MiVentana extends JFrame {" and "MiVentana Frame = new MiVentana("Titulo");" says warning?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MiVentana extends JFrame {
public MiVentana (String Titulo){
this.setTitle(Titulo);
this.setSize(300,400);
this.setLocation(160,80);
this.setLayout(new FlowLayout());
this.ConfigurarVentana();
this.setVisible(true);
}
public void ConfigurarVentana(){
JPanel panel = new JPanel();
JButton boton = new JButton ("OK");
boton.addActionListener(new EscuchadorBoton());
panel.add(boton);
this.add(panel);
}
class EscuchadorBoton implements ActionListener{
public void actionPerformed(ActionEvent a){
this.setTitle(this.getTitle().concat(this.getTitle()));
}
}
public static void main(String[] args) {
MiVentana Frame = new MiVentana("Titulo");
//frameTest.setVisible(true);
}
}
EDIT: I'm working on Ubuntu 14.04 IDE Eclipse 3.8
Using this inside the ActionListener refers to the EscuchadorBoton listener, not the instance of MiVentana - your JFrame.
Using MiVentana.this should refer to the window, not the listener and you'll be able to get and set the title with that.
This post describes what is happening a bit better - basically you want this from the enclosing class, not the enclosed class.
Basically instead of doing this:
this.setTitle(this.getTitle().concat(this.getTitle()));
You need to do this:
MiVentana.this.setTitle(MiVentana.this.getTitle().concat(MiVentana.this.getTitle()));
I would like to create a simple GUI in Java. I know the basics of creating JLabel, etc. However, I cannot find why my JLabel is not displayed on the screen. Here is my code:
package test;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class A1Panel extends JPanel implements ActionListener {
JLabel firstInt;
public void init() {
makeComponents();
makeLayout();
}
private void makeComponents() {
firstInt = new JLabel("First argument");
firstInt.setFont(new Font("Helvetica", Font.BOLD, 16));
firstInt.setBackground(Color.lightGray);
firstInt.setVisible(true);
firstInt.setHorizontalAlignment(SwingConstants.CENTER);
}
private void makeLayout() {
add(firstInt);
}
public void actionPerformed(ActionEvent e) {
}
}
I then add my JPanel to my JFrame using a different class called GUI:
import test.A1Panel;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Testing GUI");
frame.setLayout( new GridLayout(1,3));
JPanel panel = new A1Panel();
panel.setBorder( BorderFactory.createRaisedBevelBorder() );
frame.add( panel);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}
When I hit compile, what I get is a simple frame with three empty panels. I do not understand why my JLabel is not in the first panel since I have added it to my frame. Am I missing something?
The frame is not empty, the panel is. Nowhere in your code do I see a call to the methods init() or makeComponents(). In fact, I would turn your init() method into a constructor, like so:
public A1Panel() {
makeComponents();
makeLayout();
}
Another alternative to this would be to call panel.init() after declaring JPanel panel = new A1Panel()
After you instance A1Panel, you haven't called A1Panel.init()
I would suggest removing init() and adding all the code to the constructor of A1Panel. If, however, you wanted to keep the init() function, you would want to call it after JPanel panel = new A1Panel()
The code to add the label was not actually called in the main, was it? So look carefully, when is init actually called?
Look at the
private void makeLayout()
method.
If I replace public void init() by A1Panel(), it does the job. Thank you for your help.