java布局管理怎么有图片_java-用于背景图像和文本的布局管理器
您可以通過多種方式實現(xiàn)這一目標(biāo).
最簡單的方法就是只使用現(xiàn)有的…
如果您不需要在運(yùn)行時縮放背景(即您可以使用不可調(diào)整大小的窗口逃脫),只需使用JLabel作為主要容器,就可以大大簡化您的生活.
public class LabelBackground {
public static void main(String[] args) {
new LabelBackground();
}
public LabelBackground() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoginPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPane extends JLabel {
public LoginPane() {
try {
setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/background.jpg"))));
} catch (IOException ex) {
ex.printStackTrace();
}
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel nameLabel = new JLabel("Name: ");
nameLabel.setForeground(Color.WHITE);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setForeground(Color.WHITE);
add(nameLabel, gbc);
gbc.gridy++;
add(passwordLabel, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx++;
gbc.gridy = 0;
add(new JTextField(20), gbc);
gbc.gridy++;
add(new JTextField(20), gbc);
gbc.gridy++;
gbc.insets = new Insets(10, 2, 2, 2);
gbc.anchor = GridBagConstraints.EAST;
add(new JButton("Submit"), gbc);
}
}
}
更新了左對齊示例
在構(gòu)造函數(shù)的末尾,添加…
JPanel filler = new JPanel();
filler.setOpaque(false);
gbc.gridx++;
gbc.weightx = 1;
add(filler, gbc);
總結(jié)
以上是生活随笔為你收集整理的java布局管理怎么有图片_java-用于背景图像和文本的布局管理器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 代培军事生在学校体检两次有异议还可以提出
- 下一篇: 俄罗斯联邦工商会中东是干嘛的