我使用以下代码来设计我的JFrame。但是,如果我尝试更改JFrame大小,则JTextfield的大小不会调整大小。我使用了Border Layout
并根据布局的一些建议将其对齐到中心来处理调整大小。
public class Parser extends JFrame{
static JFrame frame;
static JLabel lblFile;
static JPanel pHdr;
static JPanel pBody;
static JPanel pFtr;
static JPanel pMainPanel;
static JPanel pOuterMainPanel;
static JLabel lblImage;
public Parser()
{
String startUpPath = System.getProperty("user.dir");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Log Parser");
frame.setPreferredSize(new Dimension(700,500));
frame.setLocation((int)screenSize.getWidth()/3,(int)screenSize.getHeight()/4);
frame.setLayout(new BorderLayout());
//initialize the JPanels and add to frames
pOuterMainPanel = new JPanel(new BorderLayout());
pHdr = new JPanel(new BorderLayout(700,100));
pHdr.setPreferredSize(new Dimension(700,100));
pHdr.setOpaque(true);
pHdr.setBackground(Color.WHITE);
pOuterMainPanel.add(pHdr,BorderLayout.NORTH);
pMainPanel = new JPanel(new BorderLayout(700,400));
pBody = new JPanel(new GridBagLayout());
pMainPanel.add(pBody,BorderLayout.CENTER);
pBody.setPreferredSize(new Dimension(700,400));
pBody.setOpaque(true);
pBody.setBackground(Color.WHITE);
pOuterMainPanel.add(pMainPanel,BorderLayout.CENTER);
pFtr = new JPanel(new BorderLayout(700,50));
pFtr.setOpaque(true);
pFtr.setBackground(Color.WHITE);
pFtr.setPreferredSize(new Dimension(700,50));
pOuterMainPanel.add(pFtr,BorderLayout.SOUTH);
//Customize Hdr Panel
lblImage = new JLabel();
ImageIcon imgicon = new ImageIcon(startUpPath + "\\Resources\\IM.png");
lblImage.setIcon(imgicon);
pHdr.add(lblImage,BorderLayout.CENTER);
//Customize Body Panel
GridBagConstraints gc = new GridBagConstraints();
gc.gridx=0;
gc.gridy=0;
gc.anchor = GridBagConstraints.CENTER;
JLabel lblPath = new JLabel("Path ");
lblPath.setForeground(Color.darkGray);
lblPath.setFont(new Font("Times New Roman", Font.BOLD, 15));
pBody.add(lblPath,gc);
gc.gridx++;
final JTextField txtFilePath = new JTextField(30);
txtFilePath.setPreferredSize(new Dimension(30,30));
pBody.add(txtFilePath,gc);
gc.gridx++;
JLabel lblsp1 = new JLabel(" ");
pBody.add(lblsp1,gc);
gc.gridx++;
final JButton btnBrowse = new JButton("Browse");
btnBrowse.setBackground(new Color(224,224,224));
btnBrowse.setPreferredSize(new Dimension(80,30));
pBody.add(btnBrowse,gc);
gc.gridx++;
//Finalize the Frame
frame.add(pOuterMainPanel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
btnBrowse.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
btnBrowse.setBackground(new Color(30,91,195));
btnBrowse.setForeground(Color.white);
}
public void mouseExited(MouseEvent evt)
{
btnBrowse.setBackground(new Color(224,224,224));
btnBrowse.setForeground(Color.black);
}
});
FocusListener fl = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
txtFilePath.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
txtFilePath.setBorder(BorderFactory.createLineBorder(new Color(34,5,134),2));
}
};
txtFilePath.addFocusListener(fl);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Parser();
}
}
答案 0 :(得分:1)
您应该明确了解GridBagLayout如何运作的更多信息
为了解决您的问题,我想向您介绍weight
个变量。它们是GridBagConstraints
的一部分,因为您声明了自己的实例,所以它们的初始值为0
。
因此,您需要设置gc.fill = GridBagConstraints.HORIZONTAL;
以鼓励组件使用可用的水平空间
布局通过使用weight
值来确定每个组件对preferredSize
的“可用”空间量。
因此,如果您希望JTextField
获取所有可用空间声明gc.weightx = 1.0f;
表示在添加之前水平方向上有100%的可用空间。并在添加下一个元素之前将其设置回0.0f
。