我想在图像的前面/顶部添加 JTextArea。每当我尝试将其添加到框架中,将其定位为 BorderLayout.CENTER/TOP 等时,文本就会添加到图像上方。当我尝试frame.add(textArea)时,整个图像不会显示。
我应该在 ComponentImage 类中添加 JTextArea 吗?
这是我的代码:
GUI 类:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GUILab {
private Image image;
public JTextArea textArea;
public JPanel bottomPanel;
public JTextField jtf;
public JButton updateButton;
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int frameWidth = screenWidth / 2;
int frameHeight = screenHeight / 2;
frame.setSize(frameWidth, frameHeight);
frame.setTitle("");
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//Call image
ComponentImage image = new ComponentImage();
frame.add(image, BorderLayout.CENTER);
//TextField: adding to bottomPanel, then add bottomPanel to frame
JTextArea textArea = new JTextArea(8, 20);
JPanel bottomPanel = new JPanel();
JTextField jtf = new JTextField("Test", 25);
JButton updateButton = new JButton("Update");
bottomPanel.add(jtf);
bottomPanel.add(updateButton);
updateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
textArea.append(jtf.getText()+"\n");
System.out.println(jtf.getText());
}
});
frame.add(bottomPanel, BorderLayout.SOUTH);
//frame.add(textArea);
frame.pack();
frame.setVisible(true);
}
}
ComponentImage 类:
package test;
import java.awt.Graphics;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;
public class ComponentImage extends JComponent {
private Image image;
private int compWidth=400;
private int compHeight=300;
public GUILab guilab;
public void paintComponent(Graphics g) {
image=new ImageIcon("bg1.jpg").getImage();
g.drawImage(image, 0,0,null);
g.drawString("Test", 100, 120);
}
public Dimension getPreferredSize() {
return new Dimension(compWidth, compHeight);
}
}
编辑:澄清:当在文本字段中输入文本并单击“更新”按钮时,该文本应显示在图像顶部
最佳答案
g.drawImage(image, 0,0,null);
您正在按实际大小绘制图像。如果这是您的真正要求,那么只需使用带有 ImageIcon 的 JLabel 即可。 JLabel 默认情况下以其首选尺寸显示图像,并将计算标签的首选尺寸作为图像的尺寸。
如果您的要求是随着帧大小的变化缩放图像,那么是的,您将需要进行自定义绘画,但您将使用:
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
对图像进行动态缩放。
When I have tried frame.add(textArea), the whole image won't display.
当使用 BorderLayout 并且未指定约束时,则假定为 BorderLayout.CENTER
。您无法将两个组件添加到框架的中心。
因此您需要将文本区域添加到图像面板(您的自定义组件或 JLabel:
JPanel background = new ComponentImage();
background.setLayout(...);
background.add( textArea );
请注意,如果您在 JPanel 上进行自定义绘画,则默认布局将设置为 FlowLayout。如果您在 JComponent 上进行自定义绘制,它没有默认的布局管理器,因此您需要设置布局管理器。
关于java - 在图像前面添加 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132658/