Swing 中JComboBox和图片联动
2011-05-23 16:14
127 查看
package swing.boomlink.jcombobox.t;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SelectReportStyel extends JPanel implements ActionListener {
JLabel picture; //定义图片标签
public SelectReportStyel() {
super(new BorderLayout());
//创建combobox选择项,5中报表格式,从0开始
String petStrings[] = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(4);
petList.addActionListener(this);
// Set up the picture.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
updateLabel(petStrings[petList.getSelectedIndex()]);
picture.setBorder(BorderFactory.createEmptyBorder(40, 30, 30, 30));
/* 创建一个占用空间但没有绘制的空边框,指定了顶线、底线、左边框线和右边框线的宽度。 */
// The preferred size is hard-coded to be the width of the
// widest image and the height of the tallest image + the border.
picture.setPreferredSize(new Dimension(207, 152 + 40)); // 设置此组件的首选大小
// Lay out the demo.
add(petList, BorderLayout.PAGE_START);
add(picture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(90, 90, 90, 90)); //整个窗体大小
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String petName = (String) cb.getSelectedItem();
updateLabel(petName);
}
public void updateLabel(String name) {
ImageIcon icon = createImageIcon("images/" + name + ".gif");
picture.setIcon(icon);
picture.setToolTipText("这是图片的提示文字" + "a drawing of a"
+ name.toLowerCase());
if (icon != null) {
picture.setText(null);
} else {
picture.setText("图片未找到");
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = SelectReportStyel.class.getResource(path);
if (imgURL != null) {
System.out.println(imgURL);
return new ImageIcon(imgURL);
} else {
System.err.println("没有找到图片" + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
public static void createAndShowGUI() {
// create and setup the window
JFrame frame = new JFrame("ComboBox的测试例子");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create and set up content pane
JComponent newContentPane = new SelectReportStyel();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
// display the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SelectReportStyel extends JPanel implements ActionListener {
JLabel picture; //定义图片标签
public SelectReportStyel() {
super(new BorderLayout());
//创建combobox选择项,5中报表格式,从0开始
String petStrings[] = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(4);
petList.addActionListener(this);
// Set up the picture.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
updateLabel(petStrings[petList.getSelectedIndex()]);
picture.setBorder(BorderFactory.createEmptyBorder(40, 30, 30, 30));
/* 创建一个占用空间但没有绘制的空边框,指定了顶线、底线、左边框线和右边框线的宽度。 */
// The preferred size is hard-coded to be the width of the
// widest image and the height of the tallest image + the border.
picture.setPreferredSize(new Dimension(207, 152 + 40)); // 设置此组件的首选大小
// Lay out the demo.
add(petList, BorderLayout.PAGE_START);
add(picture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(90, 90, 90, 90)); //整个窗体大小
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String petName = (String) cb.getSelectedItem();
updateLabel(petName);
}
public void updateLabel(String name) {
ImageIcon icon = createImageIcon("images/" + name + ".gif");
picture.setIcon(icon);
picture.setToolTipText("这是图片的提示文字" + "a drawing of a"
+ name.toLowerCase());
if (icon != null) {
picture.setText(null);
} else {
picture.setText("图片未找到");
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = SelectReportStyel.class.getResource(path);
if (imgURL != null) {
System.out.println(imgURL);
return new ImageIcon(imgURL);
} else {
System.err.println("没有找到图片" + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
public static void createAndShowGUI() {
// create and setup the window
JFrame frame = new JFrame("ComboBox的测试例子");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create and set up content pane
JComponent newContentPane = new SelectReportStyel();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
// display the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
相关文章推荐
- SWING项目中窗体之间的数据联动的实现
- swing设置背景图片的方法
- Swing图片ImageIcon对象到SWT图片Image对象的转换
- Swing控件(JPanel,Container等)设置背景图片
- swing设置面板背景图片
- swing中JList控件使用:每一项以 图片+文字(文字在图片的下面) 的形式显示并且在水平方向排列
- Java Swing JRadioButton 给radioButton加图片不隐藏单选按钮
- Swing 将显示好的JPanel保存为一张图片
- TCP/IP发送图片,swing.JPanel显示并更新图片(二、图片发送和发送/接受的同步)
- Swing 局部JPanel背景图片,图片随窗体变化
- Swing之图片水平翻转、垂直翻转、180度旋转
- swing 添加背景图片
- Axure教程:Banner图片联动伸缩效果
- swing设置图片为背景颜色
- Java中swing使用ImageIcon类添加图片
- swing文件选取器 例子 (4种读写文件方式-和图片的读写方法)
- Java开发- Swing AWT-设置背景图片
- JS 自动识别上传图片的宽高并且可以联动修改(联动效果,不变形)
- java swing应用(3):画当前月份的日历并保存为图片
- Java Swing 中插入图片背景