简体   繁体   中英

End line and continue in java jframe

I have created this script. My problem is that I would like to end a line and continue on the next. I am using flowlayout in this particular section of code and have tried other layout such as grid, but to no prevail. Please help, examples are greatly appreciated and thank you in advance. Here is my code:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";

public void addComponentToPane(Container pane) {

    JPanel card1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    card1.add(new JLabel("Username:"));
    card1.add(new JTextField("Username", 10));
    card1.add(new JButton("Login")); //end line here

    card1.add(new JLabel("Password:"));
    card1.add(new JTextField("Password", 10));
    card1.add(new JButton("Register")); //end line here

    card1.add(new JCheckBox());
    card1.add(new JLabel("Remember credentials"));

    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);

    pane.add(new JPanel() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 200);
        }
    }, BorderLayout.CENTER);
    pane.add(cards, BorderLayout.PAGE_END);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Login");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    CardLayoutDemo demo = new CardLayoutDemo();
    demo.addComponentToPane(frame.getContentPane());

    frame.pack();
    ImageIcon img = new ImageIcon("C:\\Users\\********\\Pictures\\settings-file-icon-2.png");
    frame.setIconImage(img.getImage());
    frame.setSize(850, 650);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

public static void main(String[] args) {

    try {

        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }

    UIManager.put("swing.boldMetal", Boolean.FALSE);

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

This is what the jframe currently looks like: 当前画面

And this is what i would like it to look like: 屏幕编辑

As I previously posted in your previous same question which you deleted minutes ago:

I'm going to guess that you're not happy with the layout of components at the bottom of your GUI. If so, then you will want to change use of layout managers. Consider nesting JPanels, each using its own layout manager and by doing this create complex GUI's with simple layout managers. Or if you're adventurous, try to use the GridBagLayout to make a grid for your user input components. Looking again through your code, perhaps GridBagLayout will be your best bet. If you do that, your JLabels can be positioned at [0, 0] and [0, 1] and the JTextFields at [1, 0] and [1, 1] (numbers corresponding to the gridx and gridy positions respectively).

You can read up on layout manager use here:

If you only want to design your gui and then program it, usage of a GUI builder is highly recommended (plenty of them, NetBeans for example). If you want to code your GUI by hand, I would try using a couple of GridLayouts for example. Use one to divide your frame into four (GridLayout 2x2), then fit a JPanel into the bottom-right part of your layout and use another GridLayout 2x2 or 2x3 on it and push your boxes and labels to appropriate "spaces" created within the inner JPanel. By using panels with GridLayouts within other such panels you gain quite a lot of control over the outcome, and it is still relatively easy to code it by hand.

You could, of course, use GridBagLayout, which probably gives you the greatest possibilities, but it is also quite difficult to design and code by hand (however it is ofcourse possible to code it by hand).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM