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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
private JButton button;
private JTextArea textArea;
private JTextField status;
private JCheckBox editable;
private JComboBox colorBox;
private boolean isTextEditable = true;
//private JComboBo
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);
this.setContentPane(j);
// call the parent class constructor - sets the frame title
// this.tempImage = this.getToolkit().getImage("portable.jpg");
//JPanel p2=new JPanel();
//p2.add(this.tempImage);
//ImageIcon icon = new ImageIcon(tempImage);
//Image img = Toolkit.getDefaultToolkit().getImage(java.net.URLClassLoader.getSystemResource("portable.JPG"));
// setIconImage( img );
// controls panel
//this.theImage= new read(portable.JPG);
JPanel controls = new JPanel(new FlowLayout());
this.button = new JButton("Clear Text");
this.button.addActionListener(this);
controls.add(this.button);
editable = new JCheckBox("Read-Only");
editable.addActionListener(this);
controls.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);
// status TextField
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);
// default layout is border layout for frame/jframe
//this.getContentPane().add("Center",tempImage);
this.getContentPane().add("North",controls);
//this.getContentPane().add("South",this.status);
this.getContentPane().add("Center",p);
// this.getContentPane().add("South",p2);
this.pack(); //set the size of the frame according
//to the component's preferred sizes
this.show(); //make the frame visible
this.repaint();
} |
Partager