Java 通过选中复选框,复选框文本和jLabel图标应显示在窗格中
我有一个带有一些文本的Java 通过选中复选框,复选框文本和jLabel图标应显示在窗格中,java,swing,jeditorpane,Java,Swing,Jeditorpane,我有一个带有一些文本的JCheckBox,还有一个带有ImageIcon的JLabel 要求是通过检查JCheckBox,JCheckBox文本和JLabel图标应显示在JEditorPane 我曾经 if (jCheckBox3.isSelected()) { s1 = jCheckBox3.getText() + jLabel1.getIcon() ; } 及 输出为 “复选框文本”文件:/G:/myProject/build/classes/myProject/img.
JCheckBox
,还有一个带有ImageIcon的JLabel
要求是通过检查JCheckBox
,JCheckBox
文本和JLabel
图标应显示在JEditorPane
我曾经
if (jCheckBox3.isSelected()) {
s1 = jCheckBox3.getText() + jLabel1.getIcon() ;
}
及
输出为
“复选框文本”文件:/G:/myProject/build/classes/myProject/img.png
我得到的不是图像,而是图像路径
这是一个示例,您可以这样做:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CheckBoxCheckingDisplayImagePanel extends JPanel implements ActionListener {
private JCheckBox checkBox;
private JLabel label;
private JEditorPane editorPane;
private JPanel editorPanel;
public CheckBoxCheckingDisplayImagePanel() {
try {
JPanel panel = new JPanel(new GridLayout(0, 2));
this.setLayout(new BorderLayout(10, 10));
this.add(panel,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(ImageIO.read(new URL("http://i.stack.imgur.com/lxthA.jpg")));
label = new JLabel(icon, SwingConstants.HORIZONTAL);
checkBox = new JCheckBox("Check me!");
checkBox.addActionListener(this);
panel.add(label);
panel.add(checkBox);
editorPane = new JEditorPane();
this.add(editorPane,BorderLayout.CENTER);
} catch (MalformedURLException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(checkBox)) {
editorPane.setContentType("text/html");
editorPane.setText("<img src=\"http://i.stack.imgur.com/lxthA.jpg\">");
}
}
}
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入javax.swing.JCheckBox;
导入javax.swing.JEditorPane;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingConstants;
公共类CheckBoxCheckingDisplayImagePanel扩展JPanel实现ActionListener{
私有JCheckBox复选框;
私人标签;
私人杰迪托潘;
私人JPanel editorPanel;
public CheckBoxCheckingDisplayImagePanel(){
试一试{
JPanel面板=新JPanel(新网格布局(0,2));
这个.setLayout(新的BorderLayout(10,10));
此.add(面板,BorderLayout.NORTH);
ImageIcon icon=新的ImageIcon(ImageIO.read)(新URL(“http://i.stack.imgur.com/lxthA.jpg")));
标签=新的JLabel(图标,SwingConstants.HORIZONTAL);
checkBox=newjcheckbox(“检查我!”);
复选框.addActionListener(此);
面板。添加(标签);
面板。添加(复选框);
editorPane=新的JEditorPane();
添加(editorPane,BorderLayout.CENTER);
}捕获(格式错误){
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE,null,ex);
}
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(例如getSource().equals(复选框)){
setContentType(“text/html”);
editorPane.setText(“”);
}
}
}
Patrick请阅读如何创建一个。如果您发布一个供我们测试的帮助,您将有更好的机会获得良好的帮助。您可以编辑您的问题并在那里添加屏幕截图。非常感谢您的回复,这非常有帮助,但问题尚未解决。我正在开发netbeans IDE,我无法使用相同的方法在netbeans中获取图像。是否有其他方法我可以分享屏幕截图的方式是的,我试过了,用示例代码我失去了一些文本我可以分享屏幕截图吗
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CheckBoxCheckingDisplayImagePanel extends JPanel implements ActionListener {
private JCheckBox checkBox;
private JLabel label;
private JEditorPane editorPane;
private JPanel editorPanel;
public CheckBoxCheckingDisplayImagePanel() {
try {
JPanel panel = new JPanel(new GridLayout(0, 2));
this.setLayout(new BorderLayout(10, 10));
this.add(panel,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(ImageIO.read(new URL("http://i.stack.imgur.com/lxthA.jpg")));
label = new JLabel(icon, SwingConstants.HORIZONTAL);
checkBox = new JCheckBox("Check me!");
checkBox.addActionListener(this);
panel.add(label);
panel.add(checkBox);
editorPane = new JEditorPane();
this.add(editorPane,BorderLayout.CENTER);
} catch (MalformedURLException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(checkBox)) {
editorPane.setContentType("text/html");
editorPane.setText("<img src=\"http://i.stack.imgur.com/lxthA.jpg\">");
}
}
}