Java 如何在点击按钮时设置文本区域的背景图像?
我想设置一个文本区域的图像作为背景图像,点击一个按钮。这怎么可能Java 如何在点击按钮时设置文本区域的背景图像?,java,swing,applet,Java,Swing,Applet,我想设置一个文本区域的图像作为背景图像,点击一个按钮。这怎么可能 import java.awt.BorderLayout; import java.awt.Container; import java.awt.Graphics; import java.awt.Image; import javax.swing.GrayFilter; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JSc
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class BackgroundSample {
public static void main(String args[]) {
JFrame frame = new JFrame("Background Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ImageIcon imageIcon = new ImageIcon("draft.gif");
JTextArea textArea = new JTextArea() {
Image image = imageIcon.getImage();
Image grayImage = GrayFilter.createDisabledImage(image);
{
setOpaque(false);
}
public void paint(Graphics g) {
g.drawImage(grayImage, 0, 0, this);
super.paint(g);
}
};
JScrollPane scrollPane = new JScrollPane(textArea);
Container content = frame.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);
}
}
这就是我所说的。如何做同样的事情,但是使用actionlistener(单击按钮)您需要扩展JTextPane类并创建一个
setImage(Image)
方法。此方法将保存图像的引用,然后调用repaint()
此外,您应该重写paintComponent()方法,而不是paint()方法。忽略任何与此相反的教程,因为它已过期10年。您需要扩展JTextPane类并创建一个
setImage(Image)
方法。此方法将保存图像的引用,然后调用repaint()
此外,您应该重写paintComponent()方法,而不是paint()方法。忽略任何有其他说明的教程,因为它已过期10年。尝试以下方法:
class MyTextArea extends JTextArea {
private Image backgroundImage;
public MyTextArea() {
super();
setOpaque(false);
}
public void setBackgroundImage(Image image) {
this.backgroundImage = image;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, this);
}
super.paintComponent(g);
}
}
然后在你的行动中:
@Override
public void actionPerformed(ActionEvent arg0) {
Image image = ImageIO.read(..);
if (image != null)
textArea.setBackgroundImage(image);
}
以下是一个例子:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class BackgroundDemo {
private static void createAndShowUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("BackgroundDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonsPanel = new JPanel();
final MyTextArea textArea = new MyTextArea();
textArea.setText("Some text");
JButton loadButton = new JButton("Set background");
buttonsPanel.add(loadButton);
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
Image image = ImageIO.read(fc.getSelectedFile());
if (image != null)
textArea.setBackgroundImage(image);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
JPanel content = new JPanel(new BorderLayout());
content.add(buttonsPanel, BorderLayout.SOUTH);
content.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.add(content);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class MyTextArea extends JTextArea {
private Image backgroundImage;
public MyTextArea() {
super();
setOpaque(false);
}
public void setBackgroundImage(Image image) {
this.backgroundImage = image;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, this);
}
super.paintComponent(g);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
试试这个:
class MyTextArea extends JTextArea {
private Image backgroundImage;
public MyTextArea() {
super();
setOpaque(false);
}
public void setBackgroundImage(Image image) {
this.backgroundImage = image;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, this);
}
super.paintComponent(g);
}
}
然后在你的行动中:
@Override
public void actionPerformed(ActionEvent arg0) {
Image image = ImageIO.read(..);
if (image != null)
textArea.setBackgroundImage(image);
}
以下是一个例子:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class BackgroundDemo {
private static void createAndShowUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("BackgroundDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonsPanel = new JPanel();
final MyTextArea textArea = new MyTextArea();
textArea.setText("Some text");
JButton loadButton = new JButton("Set background");
buttonsPanel.add(loadButton);
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
Image image = ImageIO.read(fc.getSelectedFile());
if (image != null)
textArea.setBackgroundImage(image);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
JPanel content = new JPanel(new BorderLayout());
content.add(buttonsPanel, BorderLayout.SOUTH);
content.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.add(content);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class MyTextArea extends JTextArea {
private Image backgroundImage;
public MyTextArea() {
super();
setOpaque(false);
}
public void setBackgroundImage(Image image) {
this.backgroundImage = image;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, this);
}
super.paintComponent(g);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
到目前为止,您尝试了什么?你有没有研究过事件监听器,特别是动作监听器?在调用textarea的构造函数时,我能够在文本区域显示背景图像,但我不确定如何使用动作监听器。对于信息,我正在处理一个小程序。从问题中删除HTML标记。请检查代码格式是否符合预期。该示例中的格式化代码缺少两个'}}'。到目前为止您尝试了什么?你有没有研究过事件监听器,特别是动作监听器?在调用textarea的构造函数时,我能够在文本区域显示背景图像,但我不确定如何使用动作监听器。对于信息,我正在处理一个小程序。从问题中删除HTML标记。请检查代码格式是否符合预期。该示例中的格式化代码缺少两个“}}”。-1,1)类名不应为TextArea,因为已经有一个同名的AWT组件,这可能会导致混淆。2) 您不会仅仅为了读取图像而创建图像图标。相反,您可能会使用ImageIO。3) 没有必要把论坛搞得乱七八糟,因为这个答案是2小时前给出的。填鸭式代码没有帮助,可能会引入坏习惯,如第1点和第2点所述。-1,1)类名不应该是TextArea,因为已经有一个同名的AWT组件,这可能会导致混淆。2) 您不会仅仅为了读取图像而创建图像图标。相反,您可能会使用ImageIO。3) 没有必要把论坛搞得乱七八糟,因为这个答案是2小时前给出的。正如第1点和第2点所指出的那样,填鸭式喂食代码没有帮助,可能会引入坏习惯。但它似乎不起作用。。你能告诉我有什么问题吗。这是我的密码,我试过了。但它似乎不起作用。。你能告诉我有什么问题吗。这是我的密码