【发布时间】:2014-09-02 10:33:41
【问题描述】:
我的工作有问题。 我想要发生的是我想让我的图像有一个特定的或永久的位置。 但是每次我调整窗口大小时,图像也会改变它的位置。当我使用 .setResizable(false) 时,我看不到文本和图像。
import java.awt.*;
import javax.swing.*;
public class ProgDraft extends JFrame {
private ImageIcon image1;
private JLabel label1;
ProgDraft() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
ImageIcon pics = new ImageIcon(getClass().getResource("l.png"));
JLabel logo = new JLabel(pics);
panel.setBounds(100,100,100,100);
panel.add(logo);
JLabel title = new JLabel("Testing Title");
panel.add(title);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel);
getContentPane().add(logo, BorderLayout.CENTER);
getContentPane().add(title, BorderLayout.NORTH);
}
}
这里是主要的
import javax.swing.*;
import java.awt.*;
public class ProgDraftMain {
public static void main(String args[]) {
ProgDraft gui = new ProgDraft();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setResizable(false);
gui.setVisible(true);
//gui.pack();
gui.setSize(500 , 300);
}
}
谢谢!
【问题讨论】:
-
这会出问题,因为它是一样的:getContentPane().add(panel); getContentPane().add(logo, BorderLayout.CENTER); 两个组件都用 BorderLayout.CENTER 添加(CENTER 是默认值)
标签: java swing user-interface jpanel jlabel