Topic: 请教:在frame中显示一幅图片和一个按钮怎么做? |
Print this page |
1.请教:在frame中显示一幅图片和一个按钮怎么做? | Copy to clipboard |
Posted by: topo Posted on: 2003-03-14 16:22 我想在frame中显示一幅图片和一个按钮该使用什么控件显示图片? |
2.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: topo] | Copy to clipboard |
Posted by: cn Posted on: 2003-03-14 17:56 你甚至可以把图片放到按钮上 |
3.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: cn] | Copy to clipboard |
Posted by: topo Posted on: 2003-03-14 23:11 我不打算放到按钮上,怎么办?按钮上好像不能显示这麽大的一张jpg图 |
4.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: topo] | Copy to clipboard |
Posted by: cn Posted on: 2003-03-15 01:28 Post is deleted |
5.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: cn] | Copy to clipboard |
Posted by: topo Posted on: 2003-03-15 10:46 我想在一个frame中显示数据库中的image图片,使用什么组件?我使用按钮,在jbuilder工程中导入,在按钮组件的icon属性里发现了图片文件,可是选不上去。是不是不支持jpg?---好像是不该把图片放到含有空格的目录中。这个问题解决了。我还是想知道你们做数据库应用时使用什么组件显示图片? |
6.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: topo] | Copy to clipboard |
Posted by: software_young Posted on: 2003-03-18 00:15 这里是一个例子,是从我的一个项目中摘出来的: 首先是构造一个扩展JTextField 的类 package weathermechanism; import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class clsDisplayImage extends JTextField { Image myimage; public clsDisplayImage() { } public clsDisplayImage(String PictureFileName) { super(PictureFileName); Toolkit toolkit = Toolkit.getDefaultToolkit(); myimage = toolkit.getImage(PictureFileName); } public clsDisplayImage(int columns) { super(columns); } public clsDisplayImage(String text, int columns) { super(text, columns); } public clsDisplayImage(Document doc, String text, int columns) { super(doc, text, columns); } public void paintComponent(Graphics g) { g.drawImage(myimage, 0, 0, this); } } 其次是在你的用户界面类中使用这个类 package weathermechanism; import java.io.*; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import com.borland.jbcl.layout.*; public class frmWindchill extends JDialog { JPanel panel1 = new JPanel(); XYLayout xYLayout1 = new XYLayout(); JTextArea txtDefinition = new JTextArea(); JTextArea txtDetail = new JTextArea(); JButton btnReturn = new JButton(); clsDisplayImage displayImage; JLabel lblCopyright = new JLabel(); String PictureFileName = "Windchill.jpg"; private Connection connection; private Statement statement; TitledBorder titledBorder1; TitledBorder titledBorder2; TitledBorder titledBorder3; TitledBorder titledBorder4; public frmWindchill(Frame frame, String title, boolean modal) { super(frame, title, modal); try { jbInit(); pack(); } catch (Exception ex) { ex.printStackTrace(); } } public frmWindchill() { this(null, "", false); } private void getData() { Statement statement; clsDatabaseConnection cdc = new clsDatabaseConnection(); cdc.buildConnection(); connection = cdc.getConnection(); InputStream inputStream; OutputStream outputStream; byte[] buffer; try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet resultSet = statement.executeQuery( "select Picture, Description1, Description2 from CSCI3130Project.dbo.Weather where DataID=4"); resultSet.first(); try { inputStream = resultSet.getBinaryStream("Picture"); buffer = new byte[inputStream.available()]; inputStream.read(buffer); outputStream = new FileOutputStream(PictureFileName); outputStream.write(buffer); inputStream.close(); outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); return; } catch (SQLException se) { se.printStackTrace(); return; } txtDefinition.setText(resultSet.getString("Description1")); txtDetail.setText(resultSet.getString("Description2")); statement.close(); } catch (ClassCastException e) { e.printStackTrace(); return; } catch (SQLException se) { se.printStackTrace(); return; } cdc.closeConnection(); return; } private void jbInit() throws Exception { titledBorder1 = new TitledBorder(""); titledBorder2 = new TitledBorder(""); titledBorder3 = new TitledBorder(""); titledBorder4 = new TitledBorder(""); panel1.setLayout(xYLayout1); this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); this.setModal(true); this.setResizable(false); this.setTitle("Windchill"); txtDefinition.setForeground(Color.black); txtDefinition.setBorder(titledBorder2); txtDefinition.setMaximumSize(new Dimension(2147483647, 2147483647)); txtDefinition.setPreferredSize(new Dimension(106, 18)); txtDefinition.setEditable(false); txtDefinition.setText(""); txtDefinition.setLineWrap(true); txtDefinition.setRows(0); txtDefinition.setWrapStyleWord(true); btnReturn.setText("Return"); btnReturn.addActionListener(new frmWindchill_btnReturn_actionAdapter(this)); lblCopyright.setText( "Copyright \u00A9 2003 XtremeSoft. All rights reserved."); txtDetail.setBorder(titledBorder3); txtDetail.setEditable(false); txtDetail.setLineWrap(true); txtDetail.setWrapStyleWord(true); getContentPane().add(panel1, BorderLayout.CENTER); getData(); displayImage = new clsDisplayImage(PictureFileName); panel1.add(displayImage, new XYConstraints(175, 5, 430, 558)); panel1.add(btnReturn, new XYConstraints(272, 581, -1, 27)); panel1.add(lblCopyright, new XYConstraints(181, 613, 249, -1)); panel1.add(txtDefinition, new XYConstraints(5, 44, 164, 225)); panel1.add(txtDetail, new XYConstraints(5, 303, 163, 222)); } void btnReturn_actionPerformed(ActionEvent e) { File f = new File(PictureFileName); f.delete(); this.dispose(); } } class frmWindchill_btnReturn_actionAdapter implements java.awt.event.ActionListener { frmWindchill adaptee; frmWindchill_btnReturn_actionAdapter(frmWindchill adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btnReturn_actionPerformed; } } |
7.Re:请教:在frame中显示一幅图片和一个按钮怎么做? [Re: software_young] | Copy to clipboard |
Posted by: topo Posted on: 2003-03-22 10:33 谢谢!感觉在java中处理带图片的数据库,不是很简单。不像是其他语言好 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |