jf.setIconImage(Toolkit.getDefaultToolkit().createImage("D:\\yourpic.gif"));
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都網(wǎng)站建設(shè)、網(wǎng)站制作、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗,為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
替換成自己的圖標(biāo)
圖片看起來很模糊,隱約看到需要一個登錄窗口,那就分享一下以前練習(xí)的登錄窗口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(){
? //設(shè)置窗口標(biāo)題
? setTitle("登錄界面");
? //設(shè)置一個初始面板,填充整個窗口
? JPanel loginPanel = new JPanel();
? //設(shè)置背景顏色
? 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);//設(shè)置窗口大小
? setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//設(shè)置窗口位置
? setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗口默認(rèn)關(guān)閉方式
? 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 的構(gòu)造方法,一種是通過 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?-?{
//?通構(gòu)造方法傳遞
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);
}
}