所以我对我的 JTextField 有疑问。我要做的是将 JTextField 框放在图片下方(某个城镇的地图)。是的,我使用了 .setBounds 并且它已经在图像下方,但我想要发生的是,如果我使用 .pack();,它必须仍然可见。不幸的是,事实并非如此。
我尝试使用 .setBorder(BorderFactory.createEmptyBorder(5,50,0,50)); 我看到该框位于图片下方,但不再可用于放置文本。
最后,我想要图片下方的 JTextField 并且在我打包时仍然必须可见。
请帮忙。谢谢你。我仍然处于发现关于 GUI 的新事物的阶段。对不起菜鸟问题。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
class ProgDraftMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
ProgDraft gui = new ProgDraft();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setResizable(false);
gui.pack();
//gui.setSize(1000 , 1000);
gui.setVisible(true);
}
});
}
}
class ProgDraft extends JFrame {
private ImageIcon image1;
private JLabel label1;
private JTextField textField1;
ProgDraft() {
/***Panel**/
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEADING));
/***Title***/
JLabel title = new JLabel("Perimeter Check", JLabel.CENTER);
Font font = new Font("Gigi", Font.BOLD, 50);
title.setFont(font);
/***Image***/
ImageIcon pics = new ImageIcon(getClass().getResource("antipolo.png"));
JLabel logo = new JLabel(pics);
logo.setBorder(BorderFactory.createEmptyBorder(10, 70, 0, 50));
logo.setToolTipText("Ito raw kunware yung barangay.");
panel.add(logo);
/***Info ANtipolo***/
String text = "Ito kunware ang ANtipolo" + "<br>" +
"Marami ditong landslide areas" + "<br>" + "<br>" +
"Take care and stay safe!" + "<br>" +
"I love my dogs" + "<br>" + "<br>" +"<br>" +
"Please help!";
JLabel dog = new JLabel("<html><div style=\"text-align: center;\">" + text + "</html>");
dog.setBorder(BorderFactory.createEmptyBorder(5,50,0,50));
panel.add(dog);
/***JTextFieldski**/
JTextField textField = new JTextField(6);
textField.setBorder(BorderFactory.createEmptyBorder(5,50,0,50));
textField.setBounds(210,470,100,25);
panel.add(textField);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(textField, BorderLayout.SOUTH);
getContentPane().add(dog, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
getContentPane().add(title, BorderLayout.NORTH);
}
}