jf.setIconImage(Toolkit.getDefaultToolkit().createImage("D:\\yourpic.gif"));
创新互联致力于互联网品牌建设与网络营销,包括成都网站建设、网站制作、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。创新互联为不同类型的客户提供良好的互联网应用定制及解决方案,创新互联核心团队十多年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。
替换成自己的图标
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
先上效果图:
登录界面
源码如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600;
private static final int LOGIN_HEIGHT = 400;
private static final long serialVersionUID = -2381351968820980500L;
public AbsoluteLoginFrame(){
//设置窗口标题
setTitle("登录界面");
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel();
//设置背景颜色
loginPanel.setBackground(new Color(204, 204, 204));//#CCC
loginPanel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBounds(114, 70, 360, 224);
centerPanel.setLayout(null);
JLabel jLabel = new JLabel("用户名:");
jLabel.setOpaque(true);
jLabel.setBackground(Color.YELLOW);
jLabel.setBounds(60, 60, 54, 20);
JLabel label = new JLabel("密 码:");
label.setOpaque(true);
label.setBackground(Color.CYAN);
label.setBounds(60, 90, 54, 20);
JTextField textField = new JTextField(15);
textField.setBounds(130, 60, 166, 21);
JPasswordField passwordField = new JPasswordField(15);
passwordField.setBounds(130, 90, 166, 21);
JButton jButton = new JButton("登录");
jButton.setBounds(148, 120, 62, 28);
centerPanel.add(jLabel);
centerPanel.add(label);
centerPanel.add(textField);
centerPanel.add(jButton);
centerPanel.add(passwordField);
loginPanel.add(centerPanel);
getContentPane().add(loginPanel);//将初始面板添加到窗口中
setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new AbsoluteLoginFrame();
}
}
Screen.java
public class Screen {
private int width;
private int height;
public Screen(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
this.width = screenSize.width;
this.height = screenSize.height;
}
public static Point getCenterPosition(int width, int height){
Screen screen = new Screen();
int x = (screen.getWidth() - width) / 2;
int y = (screen.getHeight() - height) / 2;
return new Point(x, y);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
下面的代码演示了两种方法传递x值到 B 窗口中,一种是通过 B 的构造方法,一种是通过 B 中的 x 的 setter 传递。
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class A extends JFrame {
private int x = 10;
public A() {
this.setTitle("A");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JButton button = new JButton("Open B");
button.addActionListener(e - {
// 通构造方法传递
B b = new B(this.x);
// 通过 setter 方法传递
b.setX(x);
b.setVisible(true);
});
this.add(button);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
class B extends JFrame {
private int x;
public B(int x) {
this.setTitle("B");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLayout(new FlowLayout());
this.x = x;
JButton button = new JButton("显示x的值");
button.addActionListener(e - {
JOptionPane.showMessageDialog(this, x);
});
this.add(button);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
public class App {
public static void main(String[] args) {
new A().setVisible(true);
}
}