在上面这张图片里面,在我自己的定义中,JFrame是网格布局
我是这样定义的:jf.setLayout(new GridLayout(3,1)) ;// jf 是JFrame的对象
然后来问题了,
问题1、:怎么才能使得【用户账号】和【用户密码】界面两者的间隔变小问题2、我想在【用户账号】的上面加3行子,怎么加?我直接用Label貌似不给力,望指教!
解决方案 »
- JAVA中如何将用户输入的字符串转换成程序行里的代码
- java写的软件,怎么能不用编译直接运行
- 请教Boolean.getBoolean(String name)这个方法
- 为什么不能重绘呢?
- 在APPLET中用JFreeChart,那个包巨大,几乎1M,我用的功能很少,有没有什么工具可以找出我不需要用到的class??
- 在main 方法中声明内部类的对象
- 请问:如何清空JRadioButton ??
- 大家练练手,征集下面这个小程序的算法!!!(很有趣的阿!!)
- 如何对HTML文件进行全文检索
- 方法(method)和函数(function)有什么区别?
- 刚刚写了一段代码,希望大家指点下。
- JPanel的问题,求教!
1:将窗体大小变小
2:使用绝对定位,即setBound()
Q2:上面再加三行的话,jf.setLayout(new GridLayout(6,1))
或者直接绝对定位加上去
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* @date 20/12/2012
*/
public class LoginPanel extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { @Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
JFrame test = new JFrame("欢迎登入在线销售系统");
test.setContentPane(new LoginPanel());
test.pack();
test.setResizable(false);
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
test.setVisible(true);
}
});
}
private static final String LABEL_USERNAME = "用户账号:";
private static final String LABEL_PASSWORD = "用户密码:";
private static final String LABEL_OK = "确定";
private static final String LABEL_Cancel = "取消";
private static final int FIELD_COLS = 20;
private JTextField fieldUsername;
private JPasswordField fieldPassword;
private Action actionOK;
private Action actionCancel;
LoginPanel() {
super(new BorderLayout(5, 5));
assert SwingUtilities.isEventDispatchThread();
fieldUsername = new JTextField(FIELD_COLS);
fieldPassword = new JPasswordField(FIELD_COLS);
actionOK = new AbstractAction(LABEL_OK) { @Override
public void actionPerformed(ActionEvent e) {
//@TODO to be implemented.
}
};
actionCancel = new AbstractAction(LABEL_Cancel) { @Override
public void actionPerformed(ActionEvent e) {
//@TODO to be implemented.
}
};
add(layoutText(Arrays.asList(
"第一行文字",
"第二行文字",
"第三行文字"
)), BorderLayout.NORTH);
add(layoutFields(), BorderLayout.CENTER);
add(layoutControl(), BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}
private JComponent layoutControl() {
JComponent result = new JPanel();
result.add(new JButton(actionOK));
result.add(new JButton(actionCancel));
return result;
}
private JComponent layoutFields() {
JComponent result = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
result.add(new JLabel(getBoldHTML(LABEL_USERNAME)), gbc);
gbc.gridy++;
result.add(new JLabel(getBoldHTML(LABEL_PASSWORD)), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
result.add(fieldUsername, gbc);
gbc.gridy++;
result.add(fieldPassword, gbc);
return result;
}
private JComponent layoutText(List<String> lines) {
assert lines != null;
JComponent result = new JPanel(new GridLayout(lines.size(), 1));
for(String line : lines)
result.add(new JLabel(line));
result.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 10));
return result;
}
private String getBoldHTML(String s) {
return "<html><b>" + s + "</b></html>";
}
}
谢谢 ...
虽然我只学了2礼拜Java,但我会尝试着看
【哎:谁叫学校任务需要呢 ... 】
this.setResizable(false);//设置窗体大小不可改变