So I need to develop a login application where you'd have;
Username [JTextField]
Password [JTextField]
My question is; how can I put text adjacent to the JTextField? Would I use a JLabel?
What layout manager should I use? (Baring in mind I've looked at documentation and can't make my mind up).
Thank you in advance.
Maybe you should consider using JavaFx if it is available. The reason why is because there is something called SceneBuilder that lets you drag and drop different items onto the pane that you are creating.
If you wanted to use swing your best bet would be to use a JLabel, and then the JTextField next to the said JLabel. Honestly, I think javafx is much easier to use, and if you are considering using it I would watch these tutorials:
https://www.youtube.com/watch?v=FLkOX4Eez6o
Best of luck! :)
If you're going forward with Java Swing, I suggest using gridbaglayout as your layout. It was very easy for me to learn when I first started swing, and it's pretty much the only layout I use now.
You would use JLabels next to the textfields, also, there is something called a JPasswordField that may come into use for you as well.
Happy coding!
You Could setup you code like this however this only works if your frame is a certain size.
import java.awt.* ;
import javax.swing.*;
import java.awt.event.*;
class Setup extends JFrame
{
TextField username = new TextField(15);
JLabel uLabel= new JLabel("Username");
TextField password = new TextField(15);
JLabel pLabel = new JLabel("Password");
Setup( String title)
{
JFrame frame = new JFrame(title);
frame.setSize( 325, 400 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
frame.setLayout( new FlowLayout() );
frame.add(username);
frame.add(uLabel);
frame.add(password);
frame.add(pLabel);
}
public static void main (String[] args )
{
Setup frame = new Setup("Login");
}
}
Related
I am trying to add a JPanel into my JFrame with a specific size. But whatever size I add to the JPanel, it always fills the whole entire JFrame. And I also tried to reposition the JButton according to my set position but it also doesn't work. Any recommendations or explanations anyone? Tq :P
import javax.swing.*;
import java.awt.event.*;
import java.awt.;
import java.awt.image.;
public class Login {
//Creating a method just for the login page0
static void Login(){
JFrame LoginFrame = new JFrame();
JPanel Panel = new JPanel();
Panel.setBounds(40,80,100,50);
Panel.setBackground(Color.black);
JButton Enter = new JButton();
Enter.setBackground(Color.cyan);
Enter.setBounds(50,100,80,30);
Enter.setText("Enter");
JButton Enter2 = new JButton();
Enter.setBackground(Color.cyan);
Enter.setBounds(50,100,80,30);
Enter.setText("Enter");
LoginFrame.setSize(420,720);
LoginFrame.setBackground(Color.white);
LoginFrame.setTitle("LoginFrame");
LoginFrame.setVisible(true);
LoginFrame.setLayout(null);
}
public static void main(String[]args) {
Login();
}
}
First of all, variable names should NOT start with an upper case character. These are Java conventions you will find in any text book or tutorial. Follow the examples.
I am trying to add a JPanel into my JFrame with a specific size
Why use a random size? Swing is designed so that each component will determine its own size so it will work on any platform with any Look and Feel.
Any recommendations
Don't use a null layout. Don't use setBounds(). Swing was designed to be used with layout managers. Layout managers make coding easier and more maintainable. Read the Swing tutorial on Layout Managers
Also, you actually need to "add" the buttons to the panel and "add" the panel to the frame. The above tutorial demos show how to do that.
If you are simply trying to change the size of the buttons then use:
button.setMargin( new Insets(20, 20, 20, 20) );
The button will be sized properly based on the text AND the extra space specified.
If you want your panel to have extra space then you can use:
panel.setBorder( new EmptyBorder(20, 20, 20, 20) );
Read the Swing tutorial on How to Use Borders for more information and examples.
or explanations
The JPanel uses a FlowLayout by default. So the layout manager is resetting the size/location based on the rules of the layout manager. If you want a different layout, then use a different layout manager.
I am currently working on a calculator. Right now, I am trying to gain experience with coding Java GUI, by making a simple program that makes a window with a text field. The code can compile without errors, but when I execute the program, the window appears, but without the text field. How do I make the text field visible? The code is shown as follows:
import javax.swing.*;
import java.awt.*;
public class Window {
public static void main(String[] args) {
JFrame Window = new JFrame("Window");
Window.setSize(400,550);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel = new JPanel (new FlowLayout());
JTextField TextField = new JTextField("Type something here");
Window.setVisible(true);
}
}
You haven't added any of the components to your JFrame. You can add them like so:
Panel.add(TextField);
Window.add(Panel);
Window.setVisible(true);
Side note: You should stick to the Java naming conventions. Use camel case for variable names.
You need to add them before making the frame visible.
Panel.add(TextField);
Window.add(Panel);
You need to take the components that you have instantiated (e.g. the Panel and TextField) and add them to appropriate containers.
For example:
Panel.add(TextField);
Window.add(Panel);
If you have not done so already, I would highly recommend visiting http://docs.oracle.com/javase/tutorial/uiswing/index.html for more information on how to use the Swing library.
On the bottom of the container where it says Statistic, what kind of box is that. I am using JFrame and would like to duplicate that.
Thanks
The container you're seeing is a JPanel. Followed by adding a border to it. You can add a border with text, by doing.
// panel is a JPanel
panel.setBorder(BorderFactory.createTitledBorder("Title"));
You can read more about it on the Java Docs there is even a lot of examples, showing the various borders, etc.
There is even a "BorderDemo.java" you can get from the Java Docs.
It's a JPanel. It provides means for visually structuring elements into box-like containers. You can find more information on how to effectively use and style them here.
EDIT: Since you seem to be more interested in the style of the border than the name of the component being used in the example above here's a snippet using the border style on a panel.
import javax.swing.*;
public class MyPanelTestDrive{
public static void main(String [] args){
JFrame myFrame = new JFrame("Panel Test Drive");
myFrame.setSize(800,600);
JPanel myPanel = new JPanel();
myPanel.setBorder(BorderFactory.createTitledBorder("Panel Title"));
myFrame.add(myPanel);
myFrame.setVisible(true);
}
}
I have been, for a long time, wondering when to use JFrame and JPanel. I was told by textbook, in-person, and via internet search some reasons, but of course, it took me looking at the Java documentation to figure out that a JPanel has the paintComponent(Graphics); method, which allows you to specify what to do to the object itself, unlike JFrame. Also, JPanels are nestable, allowing for more complex graphics than you could achieve with just one container. I also stumbled upon the fact that a JLabel is also a container. (It even has a freaking layout!) Now my question is: when should I be using a JLabel and when should I be using a JPanel?
//I know that you can put JLabels inside a JPanel, and by accident, that a JPanel is more expensive than a JLabel. I, in the long run, plan on making a cashier game that involves some lightweight container for the "money" that moves to the customer's hand upon clicking. I was thinking about using JLabels for the monetary amounts (dollar bills and coins), and the JPanel to serve as the overhead view of the transaction(s). I was also thinking about invoking repaint() (which, to my understanding, can simulate animation) on the "money labels" themselves. This is a double-question, but would you see this as the least expensive way to go about it?
For the most part, JPanel is a container that allows for better organization. When you need to organize your components of your gui, you should use a JPanel and define a layout for the JPanel. You can also nest JPanels within each other and etc. JLabels don't have the capability of defining layouts for further components to be displayed inside the JLabel.
Here's an example of a app that says "Hello World!!!" without the quotes:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class JFrame {
public static void main (strings args[]) {
JFrame frame = new JFrame();
String title = "Hello App";
frame.setTitle(title);
frame.setSize(300, 200);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panle = new JPanel();
frame.add(panle);
JLabel lable = new JLabel("Hello World!!!");
panle.add(lable);
}
}
The file name is "JFrame.java" without the quotes.
I am teaching myself Java and I am reading "Java All in One Desk Reference For Dummies." I am currently using the code provided in the book to practice Swing. Here is the code I am using that comes from the book: `import javax.swing.*;
public class JavaBook6 extends JFrame
{
public static void main(String[] args)
{
new JavaBook6();
}
public JavaBook6()
{
this.setSize(400, 400);
this.setLocation(500, 0);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Sample");
this.setVisible(true);
JPanel pnlMain = new JPanel();
JCheckBox chkMy = new JCheckBox("Save");
JButton btnMy = new JButton("Search");
JTextField txtMy = new JTextField(20);
pnlMain.add(chkMy);
pnlMain.add(txtMy);
pnlMain.add(btnMy);
this.add(pnlMain);
}
}
I seem to get inconsistent results when I press run. A window always shows up. However, sometimes the only thing displayed in the window is the Frame title and other times the components such as JCheckBox, JTextArea and JButton show up, as I would expect.
My question is why do the components show up sometimes and not others? I have tried using other components and get the same inconsistent results.
As I stated I am a beginner and I therefore have a very basic understanding of how java works, so please forgive me if the answer to my question is obvious.
I'm not too impressed with the text book:
The GUI should be created on the EDT. Read the section from the Swing tutorial on Concurrency for more information. I would also recommend you use the examples from the tutorials since they incorporate the suggestions from the tutorial.
Component must be added to the GUI before the setVisible( true ) method is invoked. (There are ways around this, but for now keep it simple and follow this rule).
You generally need to do...
this.pack();
before it will display everything.
I suspect if you resize the window, everything shows up?
Adding the pack() tells the layout manager to position and size all the components.
Also if you resize the window or force it to refresh in some way it will also display the components.