将按钮和文本放在新行上并将它们移动到 Java 中的屏幕底部 [英] Place buttons and text on new line and move them to the bottom of the screen in java

查看:34
本文介绍了将按钮和文本放在新行上并将它们移动到 Java 中的屏幕底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始了 Java 编程,并且正在尝试创建一个登录屏幕.但是,我无法弄清楚如何创建一个新行来放置我的按钮和文本.另外,我想将这些移动到 JPanel 的右下角.我为我糟糕的措辞道歉,希望你能从我的代码中明白我的意思.请提前帮助并感谢您.

I Have recently started Java programming and am trying to create a login screen. However, I cannot figure out how to create a new line to put my buttons and text on. Also, I would like to move these to the bottom right corner of the JPanel. I apologise for my poor wording and hopefully you can see what I mean from my code. Please help and thank you in advance.

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();
    card1.add(new JLabel("Username:"));
    card1.add(new JTextField("Username", 10));
    card1.add(new JButton("Login")); //end line here
    //New line
    card1.add(new JLabel("Password:"));
    card1.add(new JTextField("Password", 10));
    card1.add(new JButton("Register")); //end line here
    //New line
    card1.add(new JCheckBox());
    card1.add(new JLabel("Remember credentials"));


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

    pane.add(cards, BorderLayout.BOTTOM_RIGHT);// Add cards to bottom right hand corner.
}

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();
    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();
        }
    });
}
}

推荐答案

如图所示这里,您可以将您的<屏幕右下方的 href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html" rel="nofollow noreferrer">顶级容器.用您自己的组件替换 getPreferredSize() 中提到的占位符.另外,考虑一个 JToolBar,它可以在许多实现上浮动.

As shown here, you can position your top-level container in the lower, right portion of the screen. Substitute your own components for the placeholder mentioned in getPreferredSize(). Also, consider a JToolBar, which can float on many implementations.

附录:我想将按钮移动到 JPanel 的右下角,不移动 JPanel 到屏幕底部.

Addendum: I want to move the buttons to the bottom right corner of the JPanel, not move the JPanel to the bottom of the screen.

为您的 card1 指定 FlowLayout.RIGHT 会产生显示的结果.在内容窗格的 BorderLayout.CENTER 中替换具有 CardLayout 的面板.

Specifying FlowLayout.RIGHT for your card1 produces the result shown. Substitute your panel having CardLayout in the content pane's BorderLayout.CENTER.

public void addComponentToPane(Container pane) {

    JPanel card1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    …
    pane.add(new JPanel() {
        @Override // placeholder for actual content
        public Dimension getPreferredSize() {
            return new Dimension(800, 200);
        }
    }, BorderLayout.CENTER);
    pane.add(cards, BorderLayout.PAGE_END);
}

附录:这是您示例的更新.

Addendum: Here's an update to your example.

public void addComponentToPane(Container pane) {
    …
    pane.add(cards, BorderLayout.PAGE_END);
}
…
private static void createAndShowGUI() {
    JFrame frame = new JFrame("Login");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Move frame to lower right corner of screen
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int) rect.getMaxX() - frame.getWidth();
    int y = (int) rect.getMaxY() - frame.getHeight();
    frame.setLocation(x, y);

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

    frame.pack();
    frame.setVisible(true);
}

这篇关于将按钮和文本放在新行上并将它们移动到 Java 中的屏幕底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆