「java搭建窗口」java怎么创建窗口
本篇文章给大家谈谈java搭建窗口,以及java怎么创建窗口对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
用java创建窗口
我试一下硬盘文件存储吧,首先在C盘根目录下创建个login.swing的文件,在里面写上 tom##123lydia##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写一个窗口
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyFrame extends JFrame {
private JLabel lab_name;
private JLabel lab_number;
private JLabel lab_amount;
private JTextField jtf_name;
private JTextField jtf_number;
private JTextField jtf_amount;
private JButton but_1;
private JButton but_2;
private JButton but_3;
public MyFrame(){
setSize(245, 260);
setTitle("JLabel Demo");
Toolkit tk = Toolkit.getDefaultToolkit();
setLocation((tk.getScreenSize().height - this.HEIGHT)/2,
(tk.getScreenSize().width - this.WIDTH)/2);
this.setLayout(null);
lab_name = new JLabel("姓名");
lab_number = new JLabel("学号");
lab_amount = new JLabel("金额");
jtf_name = new JTextField(13);
jtf_number = new JTextField(13);
jtf_amount = new JTextField(13);
JPanel jp_top = new JPanel();
jp_top.setBounds(0, 20, 245, 100);
jp_top.setLayout(new BorderLayout());
JPanel jp_top_name = new JPanel();
JPanel jp_top_number = new JPanel();
JPanel jp_top_amount = new JPanel();
jp_top_name.add(lab_name);
jp_top_name.add(jtf_name);
jp_top_number.add(lab_number);
jp_top_number.add(jtf_number);
jp_top_amount.add(lab_amount);
jp_top_amount.add(jtf_amount);
jp_top.add(jp_top_name,BorderLayout.NORTH);
jp_top.add(jp_top_number,BorderLayout.CENTER);
jp_top.add(jp_top_amount,BorderLayout.SOUTH);
JPanel jp_bottom = new JPanel();
jp_bottom.setBounds(0,150, 245, 90);
but_1 = new JButton("提交");
but_2 = new JButton("取消");
but_3 = new JButton("重写");
jp_bottom.add(but_1);
jp_bottom.add(but_2);
jp_bottom.add(but_3);
this.add(jp_top,BorderLayout.NORTH);
this.add(jp_bottom,BorderLayout.CENTER);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyFrame();
}
}
LZ看下合适不。。。按钮想要美化的话 到网上再搜下。。。美化我不在行。。。
Java语言里怎么创建一个窗口?
package com.swing;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
/**
* @Fields serialVersionUID :
*/
private static final long serialVersionUID = 1L;
public JLabel j = null;
public JLabel j1 = null;
public JTextField jTextField = null;
public JTextField jTextField2 = null;
public JButton jButton=null;
public Login() {
//setLayout(new FlowLayout());
setLayout(new FlowLayout(FlowLayout.LEFT,20,40));
setFont(new Font("Helvetica", Font.PLAIN, 14));
j = new JLabel("登录名");
j1 = new JLabel("密码");
jTextField = new JTextField(10);
jTextField2 = new JTextField(10);
jButton=new JButton();
//Dimension preferredSize = new Dimension(300,100);//设置尺寸
//jButton.setPreferredSize(preferredSize );
jButton.setBounds(0, 0, 85, 82);
ImageIcon icon = new ImageIcon("E:\\workspace\\weixin-admin-pom\\src\\main\\java\\com\\swing\\dl.png");
Image temp = icon.getImage().getScaledInstance(jButton.getWidth(),
jButton.getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(temp);
jButton.setIcon(icon);
getContentPane().add(j);
getContentPane().add(jTextField);
getContentPane().add(j1);
getContentPane().add(jTextField2);
getContentPane().add(jButton);
jButton.addActionListener(this);
jButton.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// CTRL+C按下
if ( e.getKeyCode()==KeyEvent.VK_ENTER) {
System.err.println("回车了");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public static void main(String[] args) {
Login window = new Login();
window.setTitle("登录");
// 该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null); // 让窗体居中显示
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jButton) {
JOptionPane.showMessageDialog(this, "这是一个简单的消息框");
}
}
}
JAVA如何给程序设置一个窗口
首先,绘制一个默认的窗体,创建好工程,包,类,命名类为Window.很简单,在类中添加一个私有属性JFrame,这么写:private JFrame f = new JFrame("欢迎来到本自助银行");Window的构造方法中,只写 f.setVisible(true);以及窗体的初始位置和初始大小:f.setLocation(300, 200);f.setSize(800, 500);
然后在同一个构造函数中跟进一行f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);此为设置关闭图标即退出程序紧接着,写f.setResizable(false);此为设置不可更改窗体大小。如图,的确没办法更改了。
最后,便是画龙点睛的一笔,给窗体添加一个图标,显得更专业了一些:f.setIconImage(Toolkit.getDefaultToolkit().createImage("E:\\a.jpg"));
这里有一点比较重要,重申一句。构造器中的设置比静态属性初始化设置更有直接影响力。而且,程序是执行向上覆盖的。也就是说,如果之后有过更改,那么更改之后就显示更改后的结果,比如,在这个构造函数中再写f.setTitle("好好活着");那么,请注意窗体的文字。如图:
java搭建窗口的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java怎么创建窗口、java搭建窗口的信息别忘了在本站进行查找喔。
发布于:2022-12-27,除非注明,否则均为
原创文章,转载请注明出处。