因?yàn)槟阍谥骶€程中調(diào)用了Threed.sleep(1000) 那么主線程就一直在這里暫停.沒機(jī)會(huì)顯示數(shù)字.
創(chuàng)新互聯(lián)公司是專業(yè)的赤峰網(wǎng)站建設(shè)公司,赤峰接單;提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行赤峰網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
然后當(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();
}
}
效果圖
你好,在構(gòu)造方法FrameOut()中調(diào)用setLayout()方法加入一種控件布局形式,例如加入setLayout(new FlowLayout());即可以流布局的形式顯示控件。
完整代碼如下:
import java.awt.*;
import java.awt.event.*;
public class ApplicationInOut{
public static void main(String args[ ]){
new FrameInOut();
}
}
class FrameInOut extends Frame implements ActionListener{
Label prompt;
TextField input,output;
FrameInOut( ){
super("圖形界面的Java Application程序");
prompt=new Label("Java 是面向?qū)ο蟮恼Z言嗎?");
input=new TextField(6);
output=new TextField(20);
add(prompt);
add(input);
add(output);
input.addActionListener(this);
setLayout(new FlowLayout()); //此處即為添加布局形式
setSize(800,600);
setVisible(true); //show( );
}
public void actionPerformed(ActionEvent e){
output.setText(input.getText()+"OK!");
}
}
其實(shí)你也不用嚴(yán)格的按著幾步來做:
1,導(dǎo)入包也就是
import java.awt.*;
import java.awt.event.*;
或者import javax.swing.*;
import javax.swing.event.*;
2,構(gòu)造,估計(jì)就是構(gòu)造方法,就是初始化你的組件,
3,不知道
4,應(yīng)該是給組件加監(jiān)視器
5,實(shí)現(xiàn)接口的方法,也就是當(dāng)你按了按鈕或選擇了菜單項(xiàng)會(huì)發(fā)生的動(dòng)作
import java.awt.BorderLayout;
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();
}
}
/**
* 這是一個(gè)可運(yùn)行的程序,直接把代碼復(fù)制到eclipse中即可. 還有問題留言。
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FrameDemo extends JFrame{
//定義需要的組件.
private JPanel panel;//面板組件》
private JTextField tf_input;//供用戶進(jìn)行輸入的文本框》
private JLabel lb_num;//統(tǒng)計(jì)字符個(gè)數(shù).
private JButton totalButton;//統(tǒng)計(jì)字符個(gè)數(shù)按鈕.
/**
* 無參構(gòu)造方法.
* 作用: 初始化組件.
*/
public FrameDemo() {
//初始化組件.
panel = new JPanel();
tf_input = new JTextField(18);
lb_num = new JLabel();
totalButton = new JButton("統(tǒng)計(jì)");
//將組件添加到panel中.
panel.add(tf_input);
panel.add(lb_num);
panel.add(totalButton);
//設(shè)置窗口屬性》
add(panel);//添加panel
setVisible(true);//設(shè)置可見》
setSize(300,300);//設(shè)置大小。
setLocationRelativeTo(null);//居中.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置關(guān)閉動(dòng)作.
//給按鈕添加點(diǎn)擊監(jiān)聽。
totalButton.addActionListener(new ActionListener() {//匿名內(nèi)部類.
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String input = tf_input.getText();//獲取用戶輸入.
int num = input.length();//計(jì)算長度》
String numStr = num+"";//將int ---- String,因?yàn)镴Label中只能接收String類型的》
lb_num.setText(numStr);//設(shè)置到JLabel中進(jìn)行顯示》
}
});
}
/**
* 啟動(dòng)程序
* @param args
*/
public static void main(String[] args) {
new FrameDemo();
}
}