当前位置: 技术问答>java相关
JAVA高手请进?
来源: 互联网 发布时间:2015-11-09
本文导语: 请问JAVA中如何更换窗口上的图标? | 你说的是窗口左上角的窗体图标吧 这样写即可: setTitle(title); setSize(800,600); ImageIcon imageIcon = new ImageIcon("images\host16.gif"); setIconImage(imageIcon.getImage()); ...
请问JAVA中如何更换窗口上的图标?
|
你说的是窗口左上角的窗体图标吧
这样写即可:
setTitle(title);
setSize(800,600);
ImageIcon imageIcon = new ImageIcon("images\host16.gif");
setIconImage(imageIcon.getImage());
这样写即可:
setTitle(title);
setSize(800,600);
ImageIcon imageIcon = new ImageIcon("images\host16.gif");
setIconImage(imageIcon.getImage());
|
用这个(不过不好意思,我不是高手),void setIconImage(Image image)
这是示例:(最后两行是你要的)
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
interface MyInitScheme {
void initVarient();
void initFace();
}
class MyMenu extends JMenu implements MyInitScheme {
public MyMenu(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyMenuItem extends JMenuItem implements MyInitScheme {
public MyMenuItem(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyButton extends JButton implements MyInitScheme {
public MyButton(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyFrame extends JFrame implements MyInitScheme {
private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new MyMenu("File");
private JMenuItem mnuFileExit = new MyMenuItem("Exit");
private JMenu mnuHelp = new MyMenu("Help");
private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");
private JButton btnDefault = new MyButton("Default");
private JButton btnOther = new MyButton("Another");
private JTextArea txt=new JTextArea(30,20);
private JScrollPane scrollPane=new JScrollPane(txt);
private KeyAdapter key=new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
btnDefault.requestFocus();
}
}
};
public void initVarient(){};
public void initFace() {
this.setSize(new Dimension(320, 240));
this.setResizable(false);
this.getContentPane().setLayout(null);
this.getContentPane().setBackground(SystemColor.activeCaption);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mnuBar.setBackground(SystemColor.desktop);
mnuBar.setBorder(BorderFactory.createRaisedBevelBorder());
mnuFile.setMnemonic('F');
mnuFileExit.setMnemonic('x');
mnuFileExit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} );
mnuHelp.setMnemonic('H');
mnuHelpContact.setMnemonic('M');
mnuHelpContact.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Process p=Runtime.getRuntime().exec("C:/Program Files/"
+"Internet Explorer/IExplore.EXE"
+" mailto:zhangjiawang0809@sohu.com");
} catch (Exception ex) {
System.out.println(ex);
}
}
} );
btnDefault.setBounds(new Rectangle(220, 50, 75, 30));
btnDefault.setMnemonic('D');
btnOther.setBounds(new Rectangle(220, 100, 75, 30));
btnOther.setMnemonic('A');
btnOther.addKeyListener(key);
addKeyListener(key);
mnuFile.add(mnuFileExit);
mnuHelp.add(mnuHelpContact);
mnuBar.add(mnuFile);
mnuBar.add(mnuHelp);
// txt.setEditable(false);
txt.setCaretColor(getBackground());
scrollPane.setBounds(new Rectangle(10, 10, 200, 170));
this.setJMenuBar(mnuBar);
this.getRootPane().setDefaultButton(btnDefault);
this.getContentPane().add(btnDefault, null);
this.getContentPane().add(btnOther, null);
getContentPane().add(scrollPane);
}
public MyFrame(String title) {
super(title);
initVarient();
initFace();
show();
btnOther.requestFocus();
}
}
public class MyButtonApp {
public static void main(String[] args) {
JFrame frame=new MyFrame("Test MyButton");
// frame.setExtendedState(Frame.ICONIFIED);
java.awt.Toolkit kit=java.awt.Toolkit.getDefaultToolkit();
frame.setIconImage(kit.getImage("javalogo52x88.gif"));
}
}
这是示例:(最后两行是你要的)
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
interface MyInitScheme {
void initVarient();
void initFace();
}
class MyMenu extends JMenu implements MyInitScheme {
public MyMenu(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyMenuItem extends JMenuItem implements MyInitScheme {
public MyMenuItem(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyButton extends JButton implements MyInitScheme {
public MyButton(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyFrame extends JFrame implements MyInitScheme {
private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new MyMenu("File");
private JMenuItem mnuFileExit = new MyMenuItem("Exit");
private JMenu mnuHelp = new MyMenu("Help");
private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");
private JButton btnDefault = new MyButton("Default");
private JButton btnOther = new MyButton("Another");
private JTextArea txt=new JTextArea(30,20);
private JScrollPane scrollPane=new JScrollPane(txt);
private KeyAdapter key=new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
btnDefault.requestFocus();
}
}
};
public void initVarient(){};
public void initFace() {
this.setSize(new Dimension(320, 240));
this.setResizable(false);
this.getContentPane().setLayout(null);
this.getContentPane().setBackground(SystemColor.activeCaption);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mnuBar.setBackground(SystemColor.desktop);
mnuBar.setBorder(BorderFactory.createRaisedBevelBorder());
mnuFile.setMnemonic('F');
mnuFileExit.setMnemonic('x');
mnuFileExit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} );
mnuHelp.setMnemonic('H');
mnuHelpContact.setMnemonic('M');
mnuHelpContact.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Process p=Runtime.getRuntime().exec("C:/Program Files/"
+"Internet Explorer/IExplore.EXE"
+" mailto:zhangjiawang0809@sohu.com");
} catch (Exception ex) {
System.out.println(ex);
}
}
} );
btnDefault.setBounds(new Rectangle(220, 50, 75, 30));
btnDefault.setMnemonic('D');
btnOther.setBounds(new Rectangle(220, 100, 75, 30));
btnOther.setMnemonic('A');
btnOther.addKeyListener(key);
addKeyListener(key);
mnuFile.add(mnuFileExit);
mnuHelp.add(mnuHelpContact);
mnuBar.add(mnuFile);
mnuBar.add(mnuHelp);
// txt.setEditable(false);
txt.setCaretColor(getBackground());
scrollPane.setBounds(new Rectangle(10, 10, 200, 170));
this.setJMenuBar(mnuBar);
this.getRootPane().setDefaultButton(btnDefault);
this.getContentPane().add(btnDefault, null);
this.getContentPane().add(btnOther, null);
getContentPane().add(scrollPane);
}
public MyFrame(String title) {
super(title);
initVarient();
initFace();
show();
btnOther.requestFocus();
}
}
public class MyButtonApp {
public static void main(String[] args) {
JFrame frame=new MyFrame("Test MyButton");
// frame.setExtendedState(Frame.ICONIFIED);
java.awt.Toolkit kit=java.awt.Toolkit.getDefaultToolkit();
frame.setIconImage(kit.getImage("javalogo52x88.gif"));
}
}