import java.awt.BorderLayout;
10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有岷縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登錄");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("確定");
private JButton cancel = new JButton("取消");
public Login(){
frame.setSize(300,200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
//頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系統(tǒng)管理員登錄"));
c.add(titlePanel,"North");
//中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("用戶名:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("密 碼:");
l2.setBounds(50, 60, 50, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
username.setBounds(110,20,120,20);
password.setBounds(110,60,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel,"Center");
//底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
new Login();
}
}
因?yàn)槟阍谥骶€程中調(diào)用了Threed.sleep(1000) 那么主線程就一直在這里暫停.沒機(jī)會(huì)顯示數(shù)字.
然后當(dāng)主線程恢復(fù)的時(shí)候, 如果你設(shè)置shuzi.setVisible(true);那么就直接消失了
修改參考,
Thread?t?=?new?Thread(new?Runnable()?{
public?void?run()?{
try?{
Thread.sleep(1000);
shuzi.setVisible(false);//設(shè)置不可見
}?catch?(Exception?e1)?{
return;
}
}
});
t.start();//啟動(dòng)
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
class?aa?implements?ActionListener?{
JButton?jb;
public?aa(JButton?jb)?{//構(gòu)造器,把JButton傳遞進(jìn)來,
this.jb?=?jb;
}
public?void?actionPerformed(ActionEvent?e)?{
jb.setVisible(false);//設(shè)置jbutton不可見
}
}
public?class?J1?extends?JFrame{
JButton?j1?=?new?JButton("Game1");
JButton?j2?=?new?JButton("Game2");
J1(){
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container?c?=?getContentPane();
c.setLayout(new?FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new?aa(j1));//把j1這個(gè)按鈕傳遞進(jìn)過去
j2.addActionListener(new?aa(j2));//把j2這個(gè)按鈕傳遞進(jìn)過去
setSize(400,400);
setVisible(true);
}
public?static?void?main(String[]?args)?{
new?J1();
}
}
效果圖