在下面的代码中我试图创建标签。
1 - 根据下面的代码,jlabel包含单词“Demo”,在运行时,它不显示!为什么会这样,以及如何强迫jlabel出现?
2 - 正如您在代码中看到的,函数createTab1()应该创建jTextField和JPasswordField。在运行时它们出现但是它们是水平地连接,我想要的是,将JTextField及其jlabel水平地显示在彼此旁边,将第二个jlabel和JpassworfField放在彼此旁边但是在jTextField和它的jLabel下,如下所示:
Labe jtextField
label jPasswordField
但下面的代码会导致
中显示的GUI代码:
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();
}
}
答案 0 :(得分:1)
问题:
setBounds(...)
将组件添加到容器(JPanel),该容器默认使用FlowLayout,这是一种不尊重其组件的大小/位置/边界属性的布局。setBounds(...)
布局的JPanel上使用null
,我建议您不要这样做,因为这会导致非常不灵活的GUI&# 39;虽然它们在一个平台上看起来不错但在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。相反,您将需要学习和学习布局管理器,然后嵌套JPanels,每个JPanels都使用自己的布局管理器来创建令人满意的复杂GUI,这些GUI在所有操作系统上都很好看。setColumn(int column)
为您的JTextField和JPasswordField提供一个列值。这将为该字段提供一些宽度。例如:
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();
}
});
}
}