① 用java做一個窗口
java做窗口的話,需要用swing技術,之後創建JFrame 等組件,即可完成窗口創建工作。
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame; //定義一個窗口架構
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內容
JMenuItem mi2; //定義菜單的內容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學生查詢");
mi1 = new JMenuItem("確認");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置退出時關閉窗口
frame.setVisible(true);//設置窗口可見
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現代碼根據實際要求填寫
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現代碼根據實際要求填寫
JOptionPane.showMessageDialog(null, "你點擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執行菜單創建
}}
② IntelliJ IDEA 如何開發java窗體程序
主要步驟如下:
1、File——>New Project
2、Next
3、輸入Name,在Project files location中選擇文件要存放的位置,Next
4、Next
5、選擇要創建的項目或者直接點finish,創建簡單的java項目
6、創建一個新類
7、如下:
8、輸入代碼:
9、Ctrl+Shift+F10,運行該類:
③ 用java創建窗口
我試一下硬碟文件存儲吧,首先在C盤根目錄下創建個login.swing的文件,在裡面寫上 tom##123&&lydia##123 ,這個為了方便測試,自己試下吧,我也是沒學多久,如果有太2的地方,請聯系我...謝謝...;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class LoginTest implements ActionListener{
private JFrame jf ;
private JLabel l1,l2 ;
private JTextField tf1 ;
private JPasswordField tf2;
private JPanel northPanel,centerPanel ;
private JButton b1,b2 ;
private File file = new File("c:/login.swing");
public LoginTest() {
jf = new JFrame("My First WindowTest") ;
northPanel = new JPanel(new GridLayout(2,2,10,10)) ;
l1 = new JLabel("用戶名:") ;
tf1 = new JTextField() ;
l2 = new JLabel("密 碼:") ;
tf2 = new JPasswordField() ;
northPanel.add(l1);
northPanel.add(tf1);
northPanel.add(l2);
northPanel.add(tf2);
centerPanel = new JPanel();
b1 = new JButton("login");
b2 = new JButton("exit");
centerPanel.add(b1);
centerPanel.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
jf.add(northPanel);
jf.add(centerPanel,"South");
jf.setSize(200,130);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation(size.width / 2 - jf.getWidth() / 2, size.height / 2 - jf.getHeight() / 2);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1)) {
String username = tf1.getText() ;
String password = String.valueOf(tf2.getPassword());
BufferedReader br = null ;
try {
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String line = "",str = "" ;
while((line = br.readLine()) != null) {
str += line ;
}
String[] users = str.split("&&");
for(String user : users) {
String[] userInfo = user.split("##");
if(userInfo[0].equals(username) && userInfo[1].equals(password)) {
JOptionPane.showMessageDialog(null, "登錄成功!") ;
return ;
}
}
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!") ;
return ;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
System.exit(0);
}
}
public static void main(String args[]) {
new LoginTest();
}
}
④ JAVA如何進行窗體編程
用JAVA編寫窗體程序一般可以用swing這個庫,你用什麼開發工具都沒有關系。 我們可以實例化JFrame這個類來實現窗體。然後可以往窗體里加入一些容器和組件,還可以對這些組件增加偵聽的方法,這樣,一個JAVA的窗體程序的雛形就有了。這些都很簡單的,你查查API就明白了。 給你一個hello world程序: import javax.swing.JFrame; import javax.swing.JLabel; public class test { public static void main(String args[]) { JFrame j =new JFrame(); j.setSize(100, 100); j.add(new JLabel("Hello world!")); j.setVisible(true); } }
⑤ java中如何創建一個帶滾動條的窗體
例子:
我們設置公交車10秒鍾跑一趟,陸陸續續來的客戶端輸入的數據,
公交車來了,沒人空跑一圈 不執行,相當於不顯示,
公交車來了,站點有5個人就是拉5個人走,相當於執行5條數據,
公交車來了,站點一次性來了50個人但是公交車到了10秒鍾要走了,只有20個上車了,就只帶走這20個乘客,剩下的30個人只能等下一趟公交車了,相當於這一趟只執行20條數據,剩下的數據將會和後來的數據一起在10秒鍾結束的時候一起執行。
未分組按照Processingtime時間執行:
public class Tumbling {
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource lines = env.socketTextStream("localhost", 8888);
SingleOutputStreamOperator upper = lines.map(Integer::parseInt);
//設置發車的時間
AllWindowedStream all = upper.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)));
//將乘客相加
SingleOutputStreamOperator summed = all.sum(0);
summed.print();
env.execute();
}
}
分組按照Processingtime時間執行:
public class Tumbling2 {
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource lines = env.socketTextStream("localhost", 8888);
//設置分組
SingleOutputStreamOperator> wordAndOne = lines.map(new MapFunction>() {
@Override
public Tuple2 map(String value) throws Exception {
return Tuple2.of(value, 1);
}
});
KeyedStream, Tuple> keyed = wordAndOne.keyBy(0);
//設置發車的時間
WindowedStream, Tuple, TimeWindow> window = keyed.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));
//將乘客相加
SingleOutputStreamOperator> summed = window.sum(1);
summed.print();
env.execute();
}
}
⑥ java的一個窗體建立
我已發送郵件到你的郵箱,我的郵箱是[email protected]
Java源程序附後。
本程序的特點是:
(1) 文本框只能輸入純數字;
(2) 界面較美觀;
(3) 代碼可讀性較好,有適當的注釋;
(4) 窗體一出現就在桌面居中。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum; //文本框
JButton btnStart; //按鈕
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();
public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl2 = new JPanel();
pnl2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl3 = new JPanel();
pnl3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl4 = new JPanel();
pnl4.setLayout(new FlowLayout(FlowLayout.LEFT));
txtNum = new JTextField(10);
btnStart = new JButton("開始");
lblPrompt = new JLabel("<html><body>I have a number between 1 and 1000 can you guess my number?<br/>Please enter your first guess.</body></html>");
lblMessage = new JLabel();
pnl1.add(lblPrompt);
pnl2.add(txtNum);
pnl3.add(lblMessage);
pnl4.add(btnStart);
frame.setLayout(new GridLayout(4, 1));
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);
txtNum.addActionListener(this.new TextAction());
txtNum.addKeyListener(this.new KeyAction());
btnStart.addActionListener(this.new ButtonAction());
frame.setSize(400, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
public static void main(String[] args) {
new GuessNumber();
while((source=rand.nextInt(1000))==0);
}
//按鈕單擊後的事件處理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn == btnStart){
while((source=rand.nextInt(1000))==0);
txtNum.setEditable(true);
}
}
}
//文本框按回車後的事件處理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)e.getSource();
if(txt != txtNum){
return;
}
int num = Integer.parseInt(txtNum.getText());
if(num == source){
lblMessage.setText("Correct!");
txtNum.setEditable(false);
txtNum.setBackground(frame.getBackground());
}
else if(num > source){
lblMessage.setText("Too High");
txtNum.setBackground(Color.red);
}
else{
lblMessage.setText("Too Low");
txtNum.setBackground(Color.blue);
}
}
}
//限制文本框只能輸入數字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
int k = e.getKeyChar();
String text = ((JTextField)e.getSource()).getText();
if(!((k>47 && k <58) || (k==8 || k==KeyEvent.VK_PERIOD))){ //限制只能輸入數字
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
if(text.length() > 4){ //限制數值的長度
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
}
}
⑦ JAVA創建一個窗體,3個組件 一個文本框 一個標簽 一個按鈕
Frame
f
=
new
Frame();
//創建一個
窗體
f.setBounds(200,200,400,300);
//設置一個在(200,200)
坐標
為
起點
,寬400高300的窗體
TextField
tf
=
new
TextField();
//創建一個
文本框
Label
l
=
new
label("標簽名");
Button
btn
=
new
Button("按鈕");
//向窗體添加3個
組件
f.add(tf);
f.add(l);
f.add(btn);
f.setVisiable(true);
//設置可見