1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private BufferedImage image;
public SwingImageButton()
{
super("Swing Button Example");
/*Load an image */
String filename="portable.JPG";
image = new BufferedImage(290, 470, BufferedImage.TYPE_INT_RGB);
// create an image by reading in the PNG, GIF, or JPEG from a filename
try { image = ImageIO.read(new File(filename)); }
catch(IOException e) {
e.printStackTrace();
throw new RuntimeException("Could not open file: " + filename);
}
if (image == null)
throw new RuntimeException("Invalid image file: " + filename);
ImageIcon icon = new ImageIcon(image);
JLabel j = new JLabel(icon);
JPanel controls = new JPanel(new FlowLayout());
this.button = new JButton("Clear Text");
this.button.addActionListener(this);
j.add(this.button);
editable = new JCheckBox("Read-Only");
editable.addActionListener(this);
j.add(this.editable);
controls.add(new JLabel(" Text Color:"));
String[] items = {"Black", "Blue", "Green", "Red"};
colorBox = new JComboBox(items);
colorBox.addActionListener(this);
controls.add(colorBox);
this.status = new JTextField();
this.updateStatus("Application Started.");
// main area, with scrolling and wrapping
this.textArea = new JTextArea(20,40);
JScrollPane p = new JScrollPane(this.textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.textArea.setWrapStyleWord(true);
j.add(p);
this.getContentPane().add("North",controls);
this.getContentPane().add("South",j);
this.pack(); //set the size of the frame according
//to the component's preferred sizes
this.show(); |
Partager