Reputation: 11416
in the below code i am trying to create tabs.
1-according to the code below, the jlabel the contains the word "Demo", at run time, it does not show up! why that is happening and how to force that jlabel to appear?
2-as you see in the code, the function createTab1() should create jTextField and JPasswordField. at run time they are appearing but they are alligined horizontally, what i want is, to display the JTextField and its jlabel beside each other horizontally and the second jlabel and the JpassworfField beside each other but under the jTextField and its jLabel, as shown below:
Labe jtextField
label jPasswordField
but the code below result the GUI shown in
Code:
private void setUpGUI() {
// TODO Auto-generated method stub
jFrame_Main = new JFrame("Main Window");
jPanel_ContainerPanel = new JPanel(new BorderLayout());
jPanel_ContainerPanel.setBorder(BorderFactory.createLoweredBevelBorder());
jLabel_ContainerLabel = new JLabel("Demo");
jLabel_ContainerLabel.setHorizontalAlignment(SwingConstants.CENTER);
jPanel_ContainerPanel.add(jLabel_ContainerLabel, BorderLayout.NORTH);
createTab1();
//createTab2();
jTabbedPane = new JTabbedPane();
jTabbedPane.add("tab1", jPanel1);
jFrame_Main.getContentPane().add(jPanel_ContainerPanel);
jFrame_Main.getContentPane().add(jTabbedPane);
jFrame_Main.pack();
jFrame_Main.setVisible(true);
}
private void createTab1() {
// TODO Auto-generated method stub
jPanel1 = new JPanel();
jPanel1.setBounds(10, 15, 150, 20);
jLabel1 = new JLabel("userName");
jPanel1.add(jLabel1);
JTextField field = new JTextField();
field.setBounds( 10, 35, 150, 50 );
jPanel1.add( field );
JLabel label2 = new JLabel( "Password:" );
label2.setBounds( 10, 60, 150, 20 );
jPanel1.add( label2 );
JPasswordField fieldPass = new JPasswordField();
fieldPass.setBounds( 10, 80, 150, 20 );
jPanel1.add( fieldPass );
}
public static void main(String[] args) {
GUITabs guiTabs = new GUITabs();
}
}
Upvotes: -1
Views: 1177
Reputation: 285405
Problems:
setBounds(...)
to add a component to a container (a JPanel) that by default uses a FlowLayout, a layout that does not respect the size/position/bounds properties of its components.setBounds(...)
on a JPanel that is explicitly defined to use null
layouts, I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.setColumn(int column)
. This will give the field some width.For example:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;
import javax.swing.*;
public class UserNamePassword {
private static final int COLUMN_COUNT = 10;
private static final int I_GAP = 3;
private JTextField userNameField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JPanel mainPanel = new JPanel(new GridBagLayout());
public UserNamePassword() {
userNameField.setColumns(COLUMN_COUNT);
passwordField.setColumns(COLUMN_COUNT);
GridBagConstraints gbc = getGbc(0, 0, GridBagConstraints.BOTH);
mainPanel.add(new JLabel("User Name:"), gbc);
gbc = getGbc(1, 0, GridBagConstraints.HORIZONTAL);
mainPanel.add(userNameField, gbc);
gbc = getGbc(0, 1, GridBagConstraints.BOTH);
mainPanel.add(new JLabel("Password:"), gbc);
gbc = getGbc(1, 1, GridBagConstraints.HORIZONTAL);
mainPanel.add(passwordField, gbc);
}
public static GridBagConstraints getGbc(int x, int y, int fill) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
gbc.fill = fill;
return gbc;
}
public String getUserName() {
return userNameField.getText();
}
public char[] getPassword() {
return passwordField.getPassword();
}
public JComponent getMainPanel() {
return mainPanel;
}
private static void createAndShowGui() {
UserNamePassword project2 = new UserNamePassword();
int input = JOptionPane.showConfirmDialog(null, project2.getMainPanel(),
"Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (input == JOptionPane.OK_OPTION) {
if (project2.getUserName().equals("username") &&
Arrays.equals(project2.getPassword(), "password".toCharArray())) {
// you're good
} else {
JOptionPane.showMessageDialog(project2.getMainPanel(),
"error message", "Error Title", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 1