I need a basic simple java layout method
•
Java
I have checked the Internet about flowlayout, group, etc. all these are useless examples I just need a basic method to make a good layout for my java application I'll tell you my code:
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",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,false));
passwordField=new JPasswordField(10);
passwordField.setBorder(new LineBorder(null,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);
//----------------------------------------------------------------------
}
}
This is a basic GUI with some tags and some text fields The pack () method will show them on the same line, and I just need a simple little method to make a good layout
Solution
For many reasons, too many static variables in the code are not ideal @ R_ 301_ 2422 @ case Try a different approach unless you don't consider making a factory class for your project Look at this modified version. It's not bad:
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,204);
private Font myFont11=new Font("Tahoma",11);
private Font myFont12bold=new Font("Tahoma",12);
private Font myFont11bold=new Font("Tahoma",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,false));
//Panel
myPanel=new JPanel();
myPanel.setOpaque(true);
myPanel.setBorder(BorderFactory.createTitledBorder("Login"));
myPanel.setBackground(myColor);
myPanel.setLayout(new GridLayout(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,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();
}
});
}
}
This is the output:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
