Hello,
I've no experience with layouts, and I want to learn it, especially how to place components in the right places. I'll give an example, and I was wondering if you could help me?
Main class:
import javax.swing.*;
import java.awt.event.*;
public class Main {
public static void main(String[]args) {
final Frame frame = new Frame();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
}
}
Frame class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Frame extends JFrame {
private JTabbedPane tabs;
private Administrator tab = new Administrator();
public Frame() {
super("TabbedPane");
tabs = new JTabbedPane();
getContentPane().add(tabs);
tabs.addTab("Admin", tab);
setSize(300,300);
setVisible(true);
}
}
Administrator class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Administrator extends JPanel {
private JTextField usernameField;
private JButton logInButton, cancelButton;
private JPasswordField passwordField;
private static final String text = "<html> <center><b><font size=+5>ADMIN</font></b> <br><b><font size=+5>CONSOLE</font></b></center> </html>";
private JLabel textt;
public Administrator() {
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
logInButton = new JButton("Log in");
cancelButton = new JButton("Cancel");
textt = new JLabel();
textt.setText(text);
add(textt);
add(new JLabel("Username: "));
add(usernameField);
add(new JLabel("Password: "));
add(passwordField);
add(logInButton);
add(cancelButton);
logInButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Log in successful");
}
});
cancelButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
As you see, my window now looks like this:
http://i.imgur.com/8fGAQ.png
I want my layout to look something like this, more structured:
http://i.imgur.com/X0Jaa.png
I clearly dont know how to do that. Could anyone help me, show me some code in my example for example, or anything? I'd really appreciate that :D