I am trying to set the width of the JTextField but non of the methods to change it's size are working I have tried to change the amount of columns to change the width of the JTextField but it's not working?
What is causing this?
And how will I be able to change the width?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class main {
public static void main(String[] args) {
//Create and set up the window.
final JFrame frame = new JFrame("Layout 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 1500, 1000);
frame.setBackground(new Color(255,255,255));
frame.setLayout(null);
Container pane = frame.getContentPane();
pane.setLayout(null);
JLayeredPane jlp = new JLayeredPane();
frame.setContentPane(jlp);
JPanel container = new JPanel();
//container.setBackground(new Color(241,241,241));
container.setBackground(Color.RED);
container.setBounds(0,0,frame.getWidth(), 75);
container.setLayout(null);
JPanel containerA = new JPanel();
containerA.setBackground(Color.blue);
containerA.setBounds(90 ,10,50,50);
JPanel containerB = new JPanel();
containerB.setBackground(Color.RED);
containerB.setBounds(95 ,20,50,50);
//This is the top textfield
JTextField textField = new JTextField(1000);
textField.setLayout(null);
textField.setPreferredSize(new Dimension(1000,1000));
textField.setBounds(0,0,2300,50);
textField.setColumns(1000);
textField.setSize(1000,10);
textField.invalidate();
textField.setMinimumSize(new Dimension(1000,100));
container.add(textField);
jlp.add(container, Integer.valueOf(2));
jlp.add(containerA, Integer.valueOf(1));
jlp.add(containerB, Integer.valueOf(3));
container.setVisible(true);
frame.invalidate();
frame.setVisible(true);
}
}
You need to change just this line, and will get width of text field,
textField.setSize(1000,25);
Related
I want to create a simple Java Swing app with few buttons and text fields. However after I launch my program all I see is an empty window. All elements appear only when I manualy change the size of the window. How can I change that and make all elements appear immediately?
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
class JavaLesson extends JFrame{
public static void main(String[] args){
new JavaLesson();
}
public JavaLesson(){
this.setSize(400,400);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width / 2) - (this.getWidth() / 2);
int yPos = (dim.height / 2) - (this.getHeight() / 2);
this.setLocation(xPos,yPos);
this.setTitle("My app");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("This is label");
JButton button1 = new JButton("Send");
JTextField textField1 = new JTextField("Type here",7);
panel.add(label1);
panel.add(button1);
panel.add(textField1);
this.add(panel);
textField1.requestFocus();
}
}
you have to set your this.setVisible(true) on the line after you added your panel to your frame, doing so, it will display all the components you've added in the frame.
Try this and see if it works out.
public JavaLesson(){
this.setSize(400,400);
..
..
JPanel panel = new JPanel();
JLabel label1 = new JLabel("This is label");
JButton button1 = new JButton("Send");
JTextField textField1 = new JTextField("Type here",7);
panel.add(label1);
panel.add(button1);
panel.add(textField1);
this.add(panel);
this.setVisible(true);
}
Hope this helps.
Cheers :)
I have problem with displaying components on my JFrame. I'm closing my current window and opening new one and want to display jLabel on it but nothing is happening. Code is below :
Frame[] nF = DBChooser.getFrames();
nF[0].setVisible(false);
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel ("Processing...");
textLabel.setLayout(null);
pan.setLayout(null);
windoow.setLayout(null);
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
I appreciate any help
Why you need so many setLayout(null); ? I remove them and it worked
public class DBChooser extends Frame {
public static void main(String args[]) {
Frame[] nF = DBChooser.getFrames();
// nF[0].setVisible(false);
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel("Processing...");
// textLabel.setLayout(null);
// pan.setLayout(null);
// windoow.setLayout(null);
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
}
}
It is because you set a null layout to window and panel without specifying any width, lenght or position, either use some LayoutManager or set these properties (eg. bounds). A null LayoutManager means that you need to set everything yourself, because there is nothing (no LayoutManager) that would place your elements automatically. This example uses a BorderLayout, which creates a nice effect:
the code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
JFrame windoow = new JFrame("Processing");
JPanel pan = new JPanel();
windoow.setPreferredSize(new Dimension(400, 150));
pan.setPreferredSize(new Dimension(400, 150));
JLabel textLabel = new JLabel("Processing...");
textLabel.setLayout(null);
pan.setLayout(new BorderLayout());
windoow.setLayout(new BorderLayout());
pan.add(textLabel);
pan.revalidate();
pan.repaint();
windoow.getContentPane().add(pan);
windoow.setLocationRelativeTo(null);
windoow.pack();
windoow.setVisible(true);
}
}
I want to be able to position my two JLabels but when I change the values in the position lines it does nothing. Also when I run it only the second label is displayed.
My code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class cubeTimerClass {
public static void main(String[] args) {
window(); //Runs the window method
}
public static void window() {
//Create a window
JFrame window = new JFrame(); //Create the window object
window.setSize(900, 600); //Set the size of the window
window.setTitle("Cube Timer"); //Set the title of the window
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to quit when user closes the window
window.setVisible(true); //Make the window visible
//Create a label
JLabel label1 = new JLabel(""); //Create the label1 object
label1.setText("Message 1"); //Set the text for label1
label1.setAlignmentX(0);
label1.setAlignmentY(0);
window.add(label1); //Place the label on the window
//Create a label
JLabel label2 = new JLabel(""); //Create the label2 object
label2.setText("Message 2"); //Set the text for label2
label2.setAlignmentX(0);
label2.setAlignmentY(50);
window.add(label2); //Place the label on the window
}
}
Check with this -
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(null);
JLabel label = new JLabel("Java");
panel.add(label);
Dimension size = label.getPreferredSize();
label.setBounds(90, 100, size.width, size.height);
frame.setSize(300, 200);
frame.setVisible(true);
I am creating a JFrame that contains a JPanel with a grid of buttons. Everything works fine but I then want to add a JLabel above the panel of buttons but the label never appears. It does appear if I don't use BoxLayout, though. Any help is appreciated.
The first code part below is my JFrame class:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
Panel panel = new Panel();
Dimension frameDim = new Dimension(1000, 1000);
Dimension labelDim = new Dimension(100, 20);
Box box = new Box(BoxLayout.Y_AXIS);
JLabel label = new JLabel("Tic Tac Toe");
JPanel pane = new JPanel();
public Frame() {
pane.add(label);
pane.setPreferredSize(labelDim);
pane.setMinimumSize(labelDim);
add(pane);
box.add(Box.createVerticalGlue());
box.add(panel);
box.add(Box.createVerticalGlue());
add(box);
setTitle("Tic Tac Toe");
setSize(frameDim);
setMinimumSize(frameDim);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
The code below is my JPanel class with buttons:
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panel extends JPanel {
private static final long serialVersionUID = 2L;
private int i;
JButton[] button = new JButton[9];
GridLayout layout = new GridLayout(3, 3);
Dimension dim = new Dimension(500, 500);
public Panel() {
for (i = 0; i<9; i++) {
button[i] = new JButton();
add(button[i]);
}
setPreferredSize(dim);
setMinimumSize(dim);
setMaximumSize(dim);
setLayout(layout);
}
}
The last code part below is the Main class:
public class RunGame {
public static void main(String[] args) {
new Frame();
}
}
add(box);
This is adding the label direct to the JFrame, the content pane of which is laid out using a BorderLayout. The default when adding to a component to a border layout without any constraint is the CENTER, which can only display a single component. To fix it use:
pane.add(box);
you can use the BorderLayout when adding your label into Pane into the Frame and remove that box thing as below
public Frame()
{
//create label and add it to the frame
JLabel label = new JLabel("Tic Tac Toe");
label.setHorizontalAlignment( JLabel.CENTER );
add(label, BorderLayout.NORTH);
//create buttonsPanel and add it to the frame
JPanel buttons = new JPanel();
buttons.setLayout( new GridLayout(3, 3));
for (int i = 0; i < 9; i++)
{
buttons.add(new JButton(""+i));
}
add(buttons, BorderLayout.CENTER);
//setup the title, other properties for the frame etc..
setTitle("Tic Tac Toe");
setSize(1000, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
hope this helps better :)
This is the information for what I think is a correct basic GUI, but only the frame is showing up. I don't know why this is. I have everything initialized and set to visible and added, but it acts like nothing is added. Thanks for the help!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class basicButtonPress
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton();
JLabel label = new JLabel();
frame = new JFrame("Test Pop - Up");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.add(button);
panel.add(label);
button = new JButton("Test");
label = new JLabel("This is test label");
}
}
You're calling new JFrame() twice; same with JPanel, JButton and JLabel. Remove the duplicates and you'll be closer to fixing the problem.