java - 我需要一个基本的简单Java布局方法

标签 java swing user-interface layout

我在互联网上查过关于 FlowLayoutGroup 等的信息,所有示例都没有帮助。我只需要一种基本方法来为我的 Java 应用程序进行良好的布局。我会告诉你我的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test1 {

    //Step1 - Declaring variables
    private static JFrame myFrame;
    private static JPanel myPanel;
    private static JLabel titleLabel=null;
    private static JLabel logIn=null;
    private static JLabel username=null;
    private static JLabel password=null;
    private static JTextField usernameField=null;
    private static JPasswordField passwordField=null;
    private static Color myColor=new Color(0, 102, 204);
    private static Font myFont11=new Font("Tahoma", 1, 11);
    private static Font myFont12bold=new Font("Tahoma", Font.BOLD, 12);
    private static Font myFont11bold=new Font("Tahoma", Font.BOLD, 11);

    //Step2 - Creating Components
    public void createComponents() {


        //Title Label
        titleLabel=new JLabel("My Program");
        titleLabel.setForeground(Color.white);
        titleLabel.setFont(myFont12bold);
        //titleLabel.setVisible(false); //hide it or show it
        //--------------------------------------------------------


        logIn=new JLabel("Log in");
        logIn.setFont(myFont11bold);
        logIn.setForeground(Color.white);
        username=new JLabel("Username");
        username.setLabelFor(usernameField);
        username.setFont(myFont11);
        username.setForeground(Color.white);
        password=new JLabel("Password");
        password.setLabelFor(passwordField);
        password.setFont(myFont11);
        password.setForeground(Color.white);
        usernameField=new JTextField(10);
        usernameField.setBorder(new LineBorder(null, 0, false));
        passwordField=new JPasswordField(10);
        passwordField.setBorder(new LineBorder(null, 0, false));

        //Panel
        myPanel=new JPanel();
        myPanel.setBackground(myColor);
        myPanel.add(titleLabel);       
        myPanel.add(logIn);
        myPanel.add(mySeparator2);
        myPanel.add(username);
        myPanel.add(usernameField);
        myPanel.add(password);
        myPanel.add(passwordField);
        //----------------------------------------------------------

    //Step3 - Main Function
    public static void main(String[] arg) {

        //Frame
        myFrame=new JFrame();

        myFrame.setPreferredSize(new Dimension(400,300));//width:400px, height:300px
        myFrame.setLocationRelativeTo(null);//to show at center of screen
        myFrame.setTitle("My Program");
        Test1 prog=new Test1();
        prog.createComponents();
        myFrame.add(myPanel);
        myFrame.pack();//this alone will not give the frame a size
        myFrame.setVisible(true);
        //----------------------------------------------------------------------

    }

}

这是一个基本的 gui,它有一些标签和一些文本字段,使用 .pack() 方法它们将显示在同一行上,我只需要一个简单的小方法来制作一个好的布局

最佳答案

代码中的 static 变量太多并不是理想的解决方案,原因有很多。尝试使用不同的方法,直到并且除非您不考虑为您的 Project 创建一个 Factory Class。看看这个修改后的版本,这是否足够好:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test1 {

    //Step1 - Declaring variables
    private JFrame myFrame;
    // Added by me
    private JPanel contentPane;
    private JPanel myPanel;
    private JLabel username=null;
    private JLabel password=null;
    private JTextField usernameField=null;
    private JPasswordField passwordField=null;
    private Color myColor=new Color(200, 102, 204);
    private Font myFont11=new Font("Tahoma", 1, 11);
    private Font myFont12bold=new Font("Tahoma", Font.BOLD, 12);
    private Font myFont11bold=new Font("Tahoma", Font.BOLD, 11);

    //Step2 - Creating Components
    public void createComponents() {

        contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(new GridBagLayout());
        contentPane.setBorder(BorderFactory.createTitledBorder("My Program"));

        username=new JLabel("Username");
        username.setLabelFor(usernameField);
        username.setFont(myFont11);
        username.setForeground(Color.white);
        password=new JLabel("Password");
        password.setLabelFor(passwordField);
        password.setFont(myFont11);
        password.setForeground(Color.white);
        usernameField=new JTextField(10);
        usernameField.setBorder(new LineBorder(null, 0, false));
        passwordField=new JPasswordField(10);
        passwordField.setBorder(new LineBorder(null, 0, false));

        //Panel
        myPanel=new JPanel();
        myPanel.setOpaque(true);
        myPanel.setBorder(BorderFactory.createTitledBorder("Login"));
        myPanel.setBackground(myColor);
        myPanel.setLayout(new GridLayout(2, 2, 2, 2));
        myPanel.add(username);
        myPanel.add(usernameField);
        myPanel.add(password);
        myPanel.add(passwordField);
        //----------------------------------------------------------
        contentPane.add(myPanel);

        myFrame=new JFrame();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //myFrame.setPreferredSize(new Dimension(400,300));//width:400px, height:300px
        myFrame.setLocationRelativeTo(null);//to show at center of screen
        myFrame.setTitle("My Program");
        //myFrame.add(myPanel);
        myFrame.setContentPane(contentPane);
        myFrame.pack();//this alone will not give the frame a size
        myFrame.setVisible(true);
    }   

    //Step3 - Main Function
    public static void main(String[] arg) {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Test1().createComponents();
            }
        });
    }

}

这是输出:

LOGIN WINDOW

关于java - 我需要一个基本的简单Java布局方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10701109/

相关文章:

c# - 调用 UI-Element 时线程运行缓慢

java - 帮助设置 Math.tan 的正确参数

java - drawString 不在窗口上绘制文本

java - RMI 多客户端

java - Swing Call 是什么意思?

java - 如何将 JTextArea 中的换行文本转换为多个行分隔的字符串

c - 如何在主窗口菜单中放置关闭按钮?

python - Kivy - 通过 id 删除小部件

java - 在集成测试之间以编程方式重置 Hibernate 数据库

java - 从 Windows 上下文菜单运行 Java 应用程序