Border and Flow Layout doesn't show two windows? - java

I created two method with parameters one as BorderLayout and other as FlowLayout and each method has its own frame.
But only one window popups with mix layout.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class BLayOut extends JFrame
{
private JFrame fr,fr2;
private JLabel label,label2,label3;
public void win(BorderLayout bl)
{
fr =new JFrame("BorderLayout");
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(bl);
label= new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 2");
add(label,BorderLayout.NORTH);
add(label2,BorderLayout.SOUTH);
add(label3,BorderLayout.CENTER);
}
public void win(FlowLayout fl)
{
fr2 =new JFrame("FlowLayout");
setSize(500,200);
setVisible(true);
setLocation(300, 0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(fl);
label= new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 3");
add(label);
add(label2);
add(label3);
}
}
class BLayOutMain
{
public static void main (String args [])
{
BLayOut bl = new BLayOut();
bl.win(new BorderLayout());
bl.win(new FlowLayout());
}
}

You're mixing up your references...
First, you create a class that extends from JFrame...
public class BLayOut extends JFrame {
Then you declare two instance variables of JFrame...
private JFrame fr, fr2;
Then in your methods, you create an instance of JFrame and assign it to one of these variables and promptly ignore them...
fr = new JFrame("BorderLayout");
// Which frame are you modifying now...??
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(bl);
label = new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 2");
add(label, BorderLayout.NORTH);
add(label2, BorderLayout.SOUTH);
add(label3, BorderLayout.CENTER);
Basically, what this is doing is setting the properties of the instance of BLayOut and not fr or fr2.
Start by dropping the extends JFrame from BLayOut, it's confusing the issue, this will generate a list of compiler errors where the methods cannot be found. These can be fixed by using either fr or fr2, depending on the method...
fr = new JFrame("BorderLayout");
// Which frame are you modifying now...??
fr.setSize(300, 200);
fr.setVisible(true);
fr.setDefaultCloseOperation(EXIT_ON_CLOSE);
fr.setLayout(bl);
fr.label = new JLabel("Label 1");
fr.label2 = new JLabel("Label 2");
fr.label3 = new JLabel("Label 2");
fr.add(label, BorderLayout.NORTH);
fr.add(label2, BorderLayout.SOUTH);
fr.add(label3, BorderLayout.CENTER);
You really should only call setVisible when you are ready to display the initialized UI
fr = new JFrame("BorderLayout");
//...
fr.setVisible(true);
This way, your UI will show up without having the need to revalidate the frame in some way...

Related

Placing of JPanels on the JFrames is not correct

I wrote a program to compose a GUI using swing/awt framework for my assignment. So far, I am able to get the pieces working together, but when I put them all into a JFrame, they are not coming out as expected.
I have recently started working on Java GUI framework, and not sure what is missing in my code. How can I get this working properly?
I am also attaching the screen shots (see at the bottom) of the output I am getting.
public class MainFrame extends JFrame {
public MainFrame() {
addComponentsToPane(this.getContentPane());
}
private void addComponentsToPane(Container pane) {
// Set layout
GridBagConstraints gbc = new GridBagConstraints();
this.setTitle("Test tool");
this.setSize(600, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 1));
// Add video JComponent
mMainPanel = new MainPanel();
pane.add(mMainPanel, 0);
// Add conference screen panel
mFeedPanel = new FeedPanel();
pane.add(mFeedPanel, 1);
// Add a button panel
mButtonPanel = new ButtonPanel();
pane.add(mButtonPanel, 2);
this.setResizable(true);
this.setVisible(true);
this.pack();
}
}
// In actual output, there is 1 screen in this panel.
// mScreen1 is derived from JComponent object.
public class MainPanel() extends JPanel {
public MainPanel() {
addMainPanelComponents();
}
private void addMainPanelComponents() {
this.setSize(352, 240);
setBackground(Color.yellow);
setLayout(new GridLayout(1, 2));
add(mScreen);
setVisible(true);
}
}
// In actual output, there are 3 screens in this panel. I have shown code for 1 screen only
// mScreen1 is derived from JComponent object.
public class FeedPanel extends JPanel {
public FeedPanel() {
addFeedPanelComponents();
}
private void addFeedPanelComponents() {
String img1 = "images/screen1.png";
setSize(352, 150);
setBackground(Color.yellow);
setLayout(new GridLayout(1, 3));
Image image1 = ImageIO.read(new File(img1));
mScreen1.setImage(image1);
add(mScreen1);
setVisible(true);
}
}
public class ButtonPanel extends JPanel {
public ButtonPanel() {
addButtonPanelComponents();
}
private void addButtonPanelComponents() {
this.setSize(352, 150);
this.setBackground(Color.yellow);
this.setLayout(new GridLayout(1,
5));
// Add Button to panel
mStartButton = new JButton("Start");
this.add(mStartButton);
mStartButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
StartButtonActionListener(ae);
}
});
mStopButton = new JButton("Stop");
this.add(mStopButton);
mStopButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
StopButtonActionListener(ae);
}
});
setVisible(true);
}
}
This comes by default on running the code.
This comes after manually resizing the frame.
The combination of BorderLayout , GirdLayout and BoxLayout can do this for you(Actually it's not the only choice).
Here is the code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GridLayoutTest {
public void createUI(){
JFrame frame = new JFrame();
JPanel topPanel = new TopPanel();
JPanel buttomPanel = new ButtomPanel();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(topPanel,BorderLayout.CENTER);
mainPanel.add(buttomPanel,BorderLayout.SOUTH);
frame.add(mainPanel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
GridLayoutTest test = new GridLayoutTest();
test.createUI();
}
#SuppressWarnings("serial")
class TopPanel extends JPanel{
public TopPanel(){
setLayout(new GridLayout(2, 3));
ImageIcon icon = new ImageIcon("capture.png");
JLabel label1 = new JLabel(icon);
label1.setVisible(false);
JLabel label2 = new JLabel(icon);
JLabel label3 = new JLabel(icon);
label3.setVisible(false);
JLabel label4 = new JLabel(icon);
JLabel label5 = new JLabel(icon);
JLabel label6 = new JLabel(icon);
add(label1);
add(label2);
add(label3);
add(label4);
add(label5);
add(label6);
}
}
#SuppressWarnings("serial")
class ButtomPanel extends JPanel{
public ButtomPanel(){
JButton startButton = new JButton("start");
JButton stopButton = new JButton("stop");
JButton recordButton = new JButton("record");
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(Box.createHorizontalGlue());
add(startButton);
add(Box.createHorizontalStrut(10));
add(stopButton);
add(Box.createHorizontalStrut(10));
add(recordButton);
add(Box.createHorizontalGlue());
}
}
}
BoxLayout is so good too provide white space and help you to center the component.
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(Box.createHorizontalGlue());
add(startButton);
add(Box.createHorizontalStrut(10));
add(stopButton);
add(Box.createHorizontalStrut(10));
add(recordButton);
add(Box.createHorizontalGlue());
Add Glue before the first component and after the last component will help you too center the component and add strut can help you to provide white space you want. you can refer to https://stackoverflow.com/a/22525005/3378204 for more details.
Here is the effect:
The BoxLayout won't affect your component's size. Hope it can help you.
Try this :
public class Main{
private JFrame f;
private JLabel l1, l2, l3,l4;
private JPanel p1, p2, p3;
private JButton b1, b2, b3;
public Main(){
this.f = new JFrame();
this.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.f.setLayout(new GridLayout(3,1));
this.p1 = new JPanel();
this.p1.setLayout(null)
this.p1.setSize(yoursize);
this.l1 = new JLabel();
this.l1.setBounds(x,y,xspan,yspan);
this.p1.add(l1);
this.p2 = new JPanel();
this.p2.setLayout(new GridLayout(1,3));
this.l2 = new JLabel();
this.l3 = new JLabel();
this.l4 = new JLabel();
this.p2.add(l2);
this.p2.add(l3);
this.p2.add(l4);
this.p3 = new JPanel();
this.p3.setLayout(new GridLayout(1,3));
this.b1 = new JButton();
this.b2 = new JButton();
this.b3 = new JButton();
this.p3.add(b1);
this.p3.add(b2);
this.p3.add(b3);
this.f.add(p1);
this.f.add(p2);
this.f.add(p3);
this.f.pack();
this.f.setResizeable(false)
}}
Add your video components instead of labels and you can change the color of the components as you wish.
Also if you want more control over the size and position of the components, use null layout and place them individually using setBounds() function as once shown in the program above. It is surely time consuming but makes the layout perfect.

Graphic elements won't apear in JFrame (Java)

I'm in the middle of working on a program. I created a JFrame with a bunch of panels, buttons, labels and textfields that are supposed to be inside it. For some reason the JFrame apears, but with nothing inside it. Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
public class GUI extends JFrame {
JButton rect,oval,tri,free,addPoint;
JLabel xLabel,yLabel;
JTextField xTextField,yTextField;
JPanel leftPanel,rightPanel,optionsPanel,pointsPanel;
public GUI(){
initUI();
}
private void initUI(){
setLayout(new GridLayout(1,2,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Graphics Generator");
setSize(500,500);
setVisible(true);
add(leftPanel);
add(rightPanel);
leftPanel.setLayout(new GridLayout(2,1,5,5));
leftPanel.add(optionsPanel);
optionsPanel.setLayout(new GridLayout(1,4,2,2));
rect = new JButton("Rectangle");
oval = new JButton("Oval");
tri = new JButton("Triangle");
free = new JButton("Free Shape");
optionsPanel.add(rect);
optionsPanel.add(oval);
optionsPanel.add(tri);
optionsPanel.add(free);
leftPanel.add(pointsPanel);
pointsPanel.setLayout(new GridLayout(1,5,2,2));
pointsPanel.add(xLabel);
pointsPanel.add(xTextField);
pointsPanel.add(yLabel);
pointsPanel.add(yTextField);
pointsPanel.add(addPoint);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
your JComponents aren't initialized,
you added JComponents to already visible JFrame,
you have to move code line setVisible(true); to the end of constructor,
Swing GUI should be crated on Initial Thread
You should invoke setVisible(true) after you're done adding your components. They won't render otherwise until there's a call to revalidate();
You also need to initialise your components before trying to use them.
Example:
leftPanel = new JPanel();
rightPanel = new JPanel();
add(leftPanel);
add(rightPanel);
Repeat for the other components.
None of your panels, textfields, or labels have been initialized. I'm getting NullPointerException
The code below will get your program running. But you really need to look into some LayoutManagers
private void initUI(){
leftPanel = new JPanel();
rightPanel = new JPanel();
optionsPanel = new JPanel();
pointsPanel = new JPanel();
yLabel = new JLabel();
xLabel = new JLabel();
xTextField = new JTextField();
yTextField = new JTextField();
add(leftPanel);
add(rightPanel);
leftPanel.setLayout(new GridLayout(2,1,5,5));
leftPanel.add(optionsPanel);
optionsPanel.setLayout(new GridLayout(1,4,2,2));
rect = new JButton("Rectangle");
oval = new JButton("Oval");
tri = new JButton("Triangle");
free = new JButton("Free Shape");
addPoint = new JButton("Add Point");
optionsPanel.add(rect);
optionsPanel.add(oval);
optionsPanel.add(tri);
optionsPanel.add(free);
leftPanel.add(pointsPanel);
pointsPanel.setLayout(new GridLayout(1,5,2,2));
pointsPanel.add(xLabel);
pointsPanel.add(xTextField);
pointsPanel.add(yLabel);
pointsPanel.add(yTextField);
pointsPanel.add(addPoint);
setLayout(new GridLayout(1,2,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Graphics Generator");
setSize(500,500);
setVisible(true);
}

How to set component size inside container with BoxLayout

I'm facing a problem with using BoxLayout.
In my example, I try to decrease the height of the text field and change the width of the buttons (as shown in green marker in the picture at the bottom). I know about the techniques setPreferredSize() and setMaximumSize(), but it did not work as it should. The line add(Box.createHorizontalGlue()) also did not help.
Thanks for any ideas.
public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
add(textField);
button = new JButton("Button 4");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
//add(Box.createHorizontalGlue());
}
}
First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.
For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...
Documentation says:
BoxLayout pays attention
to a component's requested minimum, preferred, and maximum sizes.
While you are fine-tuning the layout, you might need to adjust these
sizes. ... For example, a button's maximum size is generally the
same as its preferred size. If you want the button to be drawn wider
when additional space is available, then you need to change its
maximum size.
Then set components maximum size: c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height)); (c means button, label and textField in your example)
Edit 1:
Here is working source code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
label.setMaximumSize(new Dimension(Integer.MAX_VALUE, label.getMinimumSize().height));
add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, textField.getMinimumSize().height));
add(textField);
button = new JButton("Button 4");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);
// add(Box.createVerticalGlue());
}
}
Edit 2:
If you want laid out Button 4 at the bottom of right column add this line add(Box.createVerticalGlue()); between add(textField); and button = new JButton("Button 4");.
As a quick remedy, you can use nested layouts, in the sense, that on the right side, create a JPanel with BorderLayout, put a JPanel(say compPanel) at the CENTER and a JPanel(say buttonPanel) at PAGE_END location. Now use a new JPanel(say panel) with GridLayout and put all the components on it, and place this compPanel inside centerPanel. Place JButton(button4) inside buttonPanel as is.
BoxLayout on the contrary, respects the preferred size of a given JComponent, which is usually calculated based on the content the JComponent holds or given explicity, hence components do not tend to align well with respect to other given components.
Here is the working example :
import java.awt.*;
import javax.swing.*;
public class Testy extends JPanel {
private JPanel panel;
private JPanel buttonPanel;
public Testy() {
setLayout(new BorderLayout(5, 5));
JPanel compPanel = new JPanel();
panel = new JPanel(new GridLayout(6, 1, 5, 5));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
panel.add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
panel.add(textField);
compPanel.add(panel);
buttonPanel = new JPanel();
button = new JButton("Button 4");
buttonPanel.add(button);
add(compPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel centerPanel = new JPanel();
frame.getContentPane().setLayout(new BorderLayout(5, 5));
centerPanel.setBackground(Color.DARK_GRAY);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(this, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Testy().constructGUI();
}
});
}
}
OUTPUT :
This should get close, based on your draw, just need to work on that component
below the JLabel (using setPreferredSize()):
JPanel main = new JPanel(new GridLayout(1, 2));
JPanel left = new JPanel();
//left.setPreferredSize(some size);
JPanel right = new JPanel(new GridLayout(6, 1));
//right.setPreferredSize(some size);
right.add(new JButton("Button 1"));
//...
right.add(new JButton("Button 4"));
main.add(left);
main.add(right);

JButtons and JLabels are not appearing

What is wrong with my code? My buttons and the labels are not appearing.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HelloPanelLabel extends JFrame {
public static void main(String[] args) {
new HelloPanelLabel(); // creates an instance of frame class
}
public HelloPanelLabel() {
this.setSize(200, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hello World!");
this.setVisible(true);
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d= tk.getScreenSize();
int x=(d.height/2);
int y=(d.width/2);
this.setLocation(x, y);
//JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
//panel1.add(label1);
JButton button1 = new JButton("Click me!");
//panel1.add(button1);
this.setVisible(true);
}
}
The reason for not showing of JButton and JLabel is that you have not added the JPanel containing these two components to the JFrame.You just need a little modification in your code. Here is that:
panel1.add(label1);
JButton button1 = new JButton("Click me!");
panel1.add(button1);
getContentPane().add(panel1);//Add to ContentPane of JFrame
this.setVisible(true);
And remove the the previous this.setVisible(true) line in your programe.
You need to set a layout and add your components to the frame.
setLayout(new FlowLayout());
//JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
add(label1);
//panel1.add(label1);
JButton button1 = new JButton("Click me!");
add(button1);
//panel1.add(button1);
this.setVisible(true);
Like the comments stated, you have to call pack(). However, if you want to define more complex layouts, you will have to create a more complex.
If you want to use a JPanel for your components
public class HelloPanelLabel extends JFrame {
public static void main(String[] args) {
new HelloPanelLabel().setVisible(true);
}
public HelloPanelLabel() {
//The same as setTitle.
super("Hello World!");
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
panel1.add(label1);
JButton button1 = new JButton("Click me!");
panel1.add(button1);
add(panel1);
//Size the frame to fit the components
pack();
//Center the frame.
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
alternatively you can add them directly to the contentPane of the JFrame.
setLayout(new FlowLayout());
JLabel label1 = new JLabel("hello, world");
add(label1);
JButton button1 = new JButton("Click me!");
add(button1);
Toolkit theKit = getToolkit();
Dimension wndSize = theKit.getScreenSize();
setSize(wndSize.width / 8, wndSize.height / 12);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Put text boxes in separate lines

MainClass(){
JFrame main = new JFrame("Login Form ");
main.setBounds(350,150,500,500);
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel pane = new JPanel();
main.add(pane);
main.add(new JLabel("Username: "));
pane.add(name);
//main.add(pane);
pane.add(new JLabel("Password: "));
pane.add(pass);
submit = new JButton("Submit");
pane.add(submit);
submit.addActionListener(new Handler());
}
I want to separate the text boxes in separate lines after the label username and name text box. I need to control the cursor to a new line.
i want to separate the text boxes in separate lines
import java.awt.*;
import javax.swing.*;
class MainClass {
JTextField name;
// This should be a JPasswordField!
JTextField pass;
JButton submit;
MainClass(){
JFrame main = new JFrame("Login Form ");
// Don't use this nonsense!
//main.setBounds(350,150,500,500);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel pane = new JPanel(new GridLayout(0,1));
main.add(pane);
pane.add(new JLabel("Username: "));
pane.add(name);
pane.add(new JLabel("Password: "));
pane.add(pass);
submit = new JButton("Submit");
pane.add(submit);
//submit.addActionListener(new Handler());
main.pack();
main.setVisible(true);
}
public static void main(String[] args) {
MainClass mc = new MainClass();
}
}
If I was building a login screen, it might be laid out more along these lines (with the labels right justified and the button in it's own panel - left as an exercise for the reader).
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class MainClass {
JTextField name;
JPasswordField pass;
JButton submit;
MainClass(){
JFrame main = new JFrame("Login Form ");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JPasswordField(10);
JPanel gui = new JPanel(new BorderLayout(3,3));
gui.setBorder(new EmptyBorder(5,5,5,5));
main.setContentPane(gui);
JPanel labels = new JPanel(new GridLayout(0,1));
JPanel controls = new JPanel(new GridLayout(0,1));
gui.add(labels, BorderLayout.WEST);
gui.add(controls, BorderLayout.CENTER);
labels.add(new JLabel("Username: "));
controls.add(name);
labels.add(new JLabel("Password: "));
controls.add(pass);
submit = new JButton("Submit");
gui.add(submit, BorderLayout.SOUTH);
main.pack();
main.setVisible(true);
}
public static void main(String[] args) {
MainClass mc = new MainClass();
}
}
Use BoxLayout; create a JPanel, use SetLayout to set it to BoxLayout, and set the BoxLayout to PAGE_AXIS. Then things you add go one after another vertically down the 'page'. There are options for alignment, see the API for BoxLayout or the Oracle/Sun/Java tutorial on layout managers.

Categories

Resources