How to set size and location of JTextField? - java

I want to set location and size of my JTextField, how can I do it ? My code doesn't work.
public static void main(String[] args) {
JTextField txt1;
JFrame frame = new JFrame("Hangman");
frame.setSize(1100, 600);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
frame.getContentPane().setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt1 = new JTextField(50);
txt1.setSize(200, 199);
txt1.setLocation(400, 200);
frame.add(txt1);
frame.setVisible(true);
}
txt1.setSize and txt1.setLocaiton doesn't work.
Output

Absolute positioning doesn't work with a layout manager.
If you really need it, you will have to set null as a layout manager, i.e :
frame.getContentPane().setLayout(null);

Related

Java JFrame inner size

How can I make the inner size 500x500 pixel?
Or should I hardcode the 28px macOS top-bar for windows?
My simple code:
import javax.swing.JFrame;
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Don't set the JFrame size. Use a JPanel and add that to the JFrame and set the size of the JPanel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen.
frame.setVisible(true);
If you are extending JPanel it is best to set the size by overridding the following:
#Override
public Dimenison getPreferredSize() {
return new Dimension(500,500);
}
It is also considered best practice to do most layouts and especially painting inside JPanel(s) and not the JFrame.

Cannot set the location for java widget

The problem is that I can't position the button where I want to. So I wrote this code to set again the location:
button.setLocation(new Point(100, 60));
import javax.swing.*;
import java.awt.*;
public class gui {
public static void main(String [] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.ORANGE);
frame.add(panel);
JButton button = new JButton("Button");
button.setPreferredSize(new Dimension(200,25));
button.setLocation(new Point(100, 60));
panel.add(button);
frame.setSize(new Dimension(500,400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI App");
frame.setResizable(false);
frame.setVisible(true);
}
}
You have to use the method setBounds()! This method requires 4 parameters: The x & y coordinate and the width and height your button should have. Also you have to set the Layout to null... So try this:
button.setBounds(100, 60, 100, 50) //just an example
frame.setLayout(null)
add below to your component:
//JPanel layout
panel.setLayout(null);
// postioning
button.setLocation(100,60);
As panel.setLayout(null) set your content panel to use absolute layout. Which you'd always have to set your component's bounds explicitly by using setBounds method.
Better to choose here the most suitable layout than using absolute layout.
ex:panel.setLayout(new FlowLayout());

Change color of JButton without changing its shape

I want to change the color of JButton by:
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
In order for change to occur, I have to add:
button.setOpaque(true);
button.setBorderPainted(false);
However, this remove the curves around the edges and thus changes the shape of the button. Is there a way to just simply change to color and keep the other properties? Another example is the changing of color (getting darker) when you press a button without having changed its color.
Here is some code that illustrates the difference between the two buttons:
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button1 = new JButton();
JButton button2 = new JButton();
button1.setPreferredSize(new Dimension(100,100));
button2.setPreferredSize(new Dimension(100,100));
button2.setBackground(Color.decode("#00a5ff"));
button2.setBorderPainted(false);
button2.setOpaque(true);
JPanel pane1 = new JPanel(new FlowLayout());
JPanel pane2 = new JPanel(new FlowLayout());
pane1.add(button1);
pane2.add(button2);
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Thanks
I literally just tested the following:
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
It seemed to work, but I am working on Ubuntu Studio 16.04. If you notice that it does not work, then let me know. Could you please show the result of your white button or your failed button (if it still doesn't work)?
Perhaps you have something else going on in your code. I altered a small example I have and the color changes with only setBackground(Color) using regular Button and using JButton. See the following...
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
Button closeButton = new Button("Close");
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
frame.dispose();
}
});
closeButton.setBackground(Color.decode("#00a5ff"));
panel.add(closeButton);
frame.add(panel);
frame.setSize(200, 100);
frame.setLocation(200, 50);
frame.setVisible(true);
}

pack() method of JFrame doesn't work (java,ubuntu)

i'm new in java .
i just learn JPanel and JFrame.
i got this note from java software solutions:
" The pack method of the frame sets its size appropriately based on
its contents—in this case the frame is sized to accommodate the size
of the panel it contains."
so i wrote this code :
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setSize(1000, 500);
panel.setBackground(darkBlue);
}
but it the result is a really tiny window that i should maximize it with mouse to see the content
but when i set frame size every thing work great!
and i use Ubuntu.
so what's the reason of this problem?
From the order of your code:
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.
Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).
When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.
To see the components within the JFrame:
public static void main (String [] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.add(label1); //Add label to panel
frame.add(panel); //Add panel (with label) to frame
frame.pack(); //Let the frame adjust its size based on the added components
frame.setVisible(true);
}
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
Color darkBlue = new Color(8,40,94);
panel.setPreferredSize(new Dimension(1000, 500));
panel.setBackground(darkBlue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
You should use pack() after setting the sizes.
Furthermore panel.setPreferredSize() works better than setSize() for you :)
call jframe.pack() before jframe.setVisible() method !
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setBackground(darkBlue);
frame.pack() ;
frame.setVisible(true); }
You will also need to check the default layout of JFrame , which is flow layout !

Center align a jtable into a jscrollpane

I am using NetBeans. I turned off the auto-resize of JTable columns. Now it is aligned to the left side of scroll pane. How can I make it centered?
Found the solution. Have to add an extra panel.
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 400);
JButton button = new JButton("Click me");
button.setPreferredSize(new Dimension(200, 40));
JPanel panel = new JPanel();
panel.add(button);
JScrollPane scrollableArea = new JScrollPane(panel);
frame.add(scrollableArea);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Really a great and unexpected solution. Sometime java really acts weird. Here is the orginal post http://www.velocityreviews.com/forums/t600361-jscrollpane.html

Categories

Resources