Java中通过那个方法可以设置Jframe的显示图标。要不然设计的窗口都是JDK默认图标。

Java中通过那个方法可以设置Jframe的显示图标。要不然设计的窗口都是JDK默认图标。,第1张

import java.awt.EventQueue

import java.awt.Image

import java.io.File

import javax.imageio.ImageIO

import javax.swing.JFrame

public class IconFrame extends JFrame {

public IconFrame init(){

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

this.setSize(300,200)

try {

Image image = ImageIO.read(new File("INPUT YOUR IMAGE FILE NAME HERE!"))

this.setIconImage(image)

} catch (Exception e) {

e.printStackTrace()

}

return this

}

public static void main(String[] args){

EventQueue.invokeLater(new Runnable(){

public void run(){

new IconFrame().init().setVisible(true)

}

})

}

}

JTextPane 是可以做的,

import java.io.File

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class 文本窗格里的组件图标 extends JFrame {

private JFileChooser chooser = new JFileChooser()

private JTextPane textPane = new JTextPane()

public 文本窗格里的组件和图标() {

Container contentPane = getContentPane()

JMenuBar menuBar = new JMenuBar()

JMenu insertMenu = new JMenu("Insert")

JMenuItem imageItem = new JMenuItem("image"),

chooserItem = new JMenuItem("color chooser")

insertMenu.add(imageItem)

insertMenu.add(chooserItem)

menuBar.add(insertMenu)

setJMenuBar(menuBar)

textPane.setFont(new Font("Serif", Font.ITALIC, 24))

contentPane.add(textPane, BorderLayout.CENTER)

chooserItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JColorChooser chooser = new JColorChooser()

chooser.setMaximumSize(

chooser.getPreferredSize())

textPane.insertComponent(chooser)

}

})

imageItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int option =

chooser.showDialog(文本窗格里的组件和图标.this,"Pick An Image")

if(option == JFileChooser.APPROVE_OPTION) {

File file = chooser.getSelectedFile()

if(file != null) {

textPane.insertIcon(new ImageIcon(

file.getPath()))

}

}

}

})

}

public static void main(String args[]) {

GJApp3.launch(new 文本窗格里的组件和图标(),

"Using JTextPane",300,300,450,300)

}

}

class GJApp3 extends WindowAdapter {

static private JPanel statusArea = new JPanel()

static private JLabel status = new JLabel(" ")

public static void launch(final JFrame f, String title,

final int x, final int y,

final int w, int h) {

f.setTitle(title)

f.setBounds(x,y,w,h)

f.setVisible(true)

statusArea.setBorder(BorderFactory.createEtchedBorder())

statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0))

statusArea.add(status)

status.setHorizontalAlignment(JLabel.LEFT)

f.setDefaultCloseOperation(

WindowConstants.DISPOSE_ON_CLOSE)

f.addWindowListener(new WindowAdapter() {

public void windowClosed(WindowEvent e) {

System.exit(0)

}

})

}

static public JPanel getStatusArea() {

return statusArea

}

static public void updateStatus(String s) {

status.setText(s)

}

}

在java swing中需要为容器添加图片,或者背景图片。

提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel

1.JPanel(源代码)

package oo

import java.awt.Graphics

import java.awt.Image

import java.io.File

import javax.swing.ImageIcon

import javax.swing.JFrame

import javax.swing.JPanel

public class Drawing {

JFrame jframe = new JFrame()

public static JPanel GImage = null

public Drawing() {

initFrame()

}

// 初始化窗口

public void initFrame() {

// 利用JPanel添加背景图片

GImage = new JPanel() {

protected void paintComponent(Graphics g) {

ImageIcon icon = new ImageIcon("image\\benbenla.jpg")

Image img = icon.getImage()

g.drawImage(img, 0, 0, icon.getIconWidth(),

icon.getIconHeight(), icon.getImageObserver())

jframe.setSize(icon.getIconWidth(), icon.getIconHeight())

}

}

jframe.setTitle("测试背景图片")

jframe.add(GImage)

jframe.pack()

jframe.setVisible(true)

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

public static void main(String[] args) {

new Drawing()

}

}

2.JLabel源代码

package swing.draw

import java.awt.Image

import javax.swing.ImageIcon

import javax.swing.JFrame

import javax.swing.JLabel

public class Drawing2 {

JLabel jlpic = new JLabel()

JFrame jframe = new JFrame()

public Drawing2() {

init1Frame()

}

public void init1Frame() {

ImageIcon icon = new ImageIcon("image\\benbenla.jpg")

icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),

icon.getIconHeight(), Image.SCALE_DEFAULT))

System.out.println(icon.getIconHeight() + "" + icon.getIconWidth())

jlpic.setBounds(0, 0, 1366, 768)

jlpic.setHorizontalAlignment(0)

jlpic.setIcon(icon)

jframe.setSize(1366, 768)

jframe.add(jlpic)

jframe.pack()

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

jframe.setVisible(true)

}

public static void main(String args[]) {

new Drawing2()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11274119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存