
正文
java窗体代码笔记 java窗口编程
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java中怎么新建窗口?我是新手麻烦代码中主要语句解释一下
不知道是不是java窗体代码笔记你说java窗体代码笔记的窗口
使用javaswing JFrame设计窗口 + 布局就可实现java窗体代码笔记,java窗体代码笔记,如下例(添加了详细注释):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class JFrameTest extends JFrame implements ActionListener {
private static final long serialVersionUID = -2829899643559384548L;
private JButton b1 = null;//按钮
private JTextArea jta = null;//文本
public JFrameTest() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout());//设置布局方式java窗体代码笔记,BorderLayout东西南北中布局
b1 = new JButton("点击");
b1.addActionListener(this);//为按钮添加监听
c.add(b1, BorderLayout.SOUTH);//添加按钮到c容器中,并分配在容器南(下)方
jta = new JTextArea();
c.add(jta, BorderLayout.CENTER);//添加文本区到c容器中,并分配在居中位置
this.setTitle("按钮事件");//设置窗口标题
this.setSize(300, 300);//设置窗体大小
this.setVisible(true);//窗体设置为显示
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
//常用的一种关闭窗体的方法
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
//使用判断按钮名称的方法触发事件
if("点击".equals(e.getActionCommand())) {
jta.setText("按钮被点击了!");
}
//也可以获取对象名实现判断
// if(e.getSource() == b1) {
// jta.setText("按钮使用getSource方法被点击了!");
// }
}
public static void main(String[] args) {
new JFrameTest();
}
}
相关问答
Q1: java 满足查询条件,自动跳出窗体,代码该怎么写
给你核心代码:
JFrame jf;
if(条件==true){
jf=new JFrame():
jf.setVesiable(true);
}
为啥没分数呢?没分他人都不愿意理的。
Q2: java ,我想做一个窗体,窗体上有一个按钮,单击这个按钮后,在旁边再生成一个新的按钮或者是标签
假如窗体类名称为MainFrame,按钮的ActionListener如下:
Button newBtn = new Button("新按钮");
MainFrame.this.add(newBtn);
另外,MainFrame的布局方式最好是FlowLayout,在MainFrame的构造方法中这样写:
setLayout(new FlowLayout());
楼主可以试试
Q3: Java记事本代码,窗体关闭功能代码,
如果这个窗体是JFrame或JDialogjava窗体代码笔记,那就在构造方法里写
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
如果是Frame或Dialogjava窗体代码笔记,那就在WindowListenerjava窗体代码笔记的windowClosing(WindowEvent)方法里写
this.dispose();
两句话java窗体代码笔记的意思是窗体关闭时释放自身,,,而不是退出,不要设成EXIT_ON_CLOSE或System.exit(0);,那样java窗体代码笔记的话窗体关闭会导致整个程序退出。
Q4: 求一个用Java编写的简易笔记本代码。跪求
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener
{
private JFrame frame=new JFrame("新记事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("开始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替换为");
private JButton jba=new JButton("全部替换");
private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替换");
@Override
public void actionPerformed(ActionEvent e)
{
String comm=e.getActionCommand();
if("新建".equals(comm))
{
if(!(frame.getTitle().equals("新记事本")))
{
if(!flag)
{
write();
newNew();
}
else
{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION)
{//选择文件后再执行下面java窗体代码笔记的语句java窗体代码笔记,保证java窗体代码笔记了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}
else if(!(jta.getText().isEmpty()))
{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION)
{//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}
else
{
newNew();
}
}
else if("打开".equals(comm))
{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm))
{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag)
{
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION)
{//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
else
{
write();
}
}
else if("另存".equals(comm))
{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION)
{//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}
else if("退出".equals(comm))
{
System.exit(0);
}
else if("撤销".equals(comm))
{
jta.setText(value);
}
else if("剪切".equals(comm))
{
value=jta.getText();
jta.cut();
}
else if("复制".equals(comm))
{
jta.copy();
}
else if("粘贴".equals(comm))
{
value=jta.getText();
jta.paste();
}
else if("删除".equals(comm))
{
value=jta.getText();
jta.replaceSelection(null);
}
else if("全选".equals(comm))
{
jta.selectAll();
}
else if("查找".equals(comm))
{
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
else if("替换".equals(comm))
{
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
else if("版本".equals(comm))
{
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
else if("开始".equals(comm)||"下一个".equals(comm))
{
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1)
{
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一个");
// value=value.substring(s+temp.length());//不能截取字串
}
else
{
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}
else if("替换为".equals(comm))
{
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1)
{
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}
else
{
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}
else if("全部替换".equals(comm))
{
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read()
{
String temp="";
try
{
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true)
{
int num=fis.read(b);
if(num==-1)
break;
temp=temp+new String(b,0,num);
}
fis.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
return temp;
}
public void write()
{
try
{
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void newNew()
{
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad()
{
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","编辑","帮助"};
String[][] menuItemLab={{"新建","打开","保存","另存","退出"},
{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},
{"版本"}};
for(int i=0;imenuLab.length;i++)
{
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++)
{
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自动换行
JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new MyNotepad();
}
}
Q5: java窗体猜数字代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caishuzi extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel textpanel, buttonpanel, fopanel, downpanel;
private JButton btn;
private JTextField jtinput, jttishi;
private JLabel lbnum0, lbnum, lbtishi, lbinput;
private JTextArea tafo;
private Boolean start = false, jisuan = false, jinru = false;
MenuBar mb = new MenuBar();
Menu filemenu = new Menu("文件"), systemmenu = new Menu("系统"),
helpmenu = new Menu("帮助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),
closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),
inputcishu = new MenuItem("输入限制次数");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜数字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 设置菜单
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.addSeparator();
filemenu.add(closefile);
newfile.setEnabled(false);
openfile.setEnabled(false);
closefile.setEnabled(false);
inputsecret.addActionListener(this);
inputcishu.addActionListener(this);
inputcishu.setEnabled(false);
systemmenu.add(inputsecret);
systemmenu.add(inputcishu);
systemmenu.add(quit);
quit.addActionListener(this);
help.addActionListener(this);
helpmenu.add(help);
mb.add(filemenu);
mb.add(systemmenu);
mb.add(helpmenu);
this.setMenuBar(mb);
Container c = this.getContentPane();
BorderLayout b = new BorderLayout();
b.setVgap(10);
c.setLayout(b);
settextpanel();
c.add(textpanel, BorderLayout.NORTH);
setbuttonpanel();
setfopanel();
setdownpanel();
c.add(downpanel, BorderLayout.CENTER);
this.setSize(680,350);
//this.setResizable(false);
//this.pack();
this.setVisible(true);
}
void settextpanel()
{
textpanel = new JPanel();
textpanel.setLayout(new FlowLayout());
lbinput = new JLabel("输入java窗体代码笔记:");
jtinput = new JTextField("", 10);
jtinput.setEditable(false);
jttishi = new JTextField(30);
lbnum = new JLabel("8");
lbnum0 = new JLabel("剩余次数:");
lbtishi = new JLabel("提示:");
jttishi.setEditable(false);
textpanel.add(lbinput);
textpanel.add(jtinput);
textpanel.add(lbtishi);
textpanel.add(jttishi);
textpanel.add(lbnum0);
textpanel.add(lbnum);
}
void setbuttonpanel()
{
String[] str =
{ "7", "8", "9", "4", "5", "6", "1", "2", "3", "确定", "0", "退格" };
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(4, 3, 4, 8));
for (int i = 0; i str.length; i++)
{
btn = new JButton(str[i]);
btn.addActionListener(this);
buttonpanel.add(btn);
}
}
void setfopanel()
{
JButton btnright = new JButton("正确答案");
JPanel p = new JPanel();
btnright.addActionListener(this);
fopanel = new JPanel();
fopanel.setLayout(new BorderLayout());
btn = new JButton("开始");
btn.addActionListener(this);
tafo = new JTextArea(15,12);
p.setLayout(new GridLayout(1, 2));
p.add(btn);
p.add(btnright);
fopanel.add(p, BorderLayout.NORTH);
tafo.setEditable(false);
fopanel.add(tafo, BorderLayout.CENTER);
}
void setdownpanel()
{
downpanel = new JPanel();
downpanel.setLayout(new GridLayout(1, 2, 30, 0));
downpanel.add(buttonpanel);
downpanel.add(fopanel);
}
public static void main(String[] args)
{
new caishuzi();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("退出"))
{
int i = JOptionPane.showConfirmDialog(this, "确认退出java窗体代码笔记?", "",
JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if (i == 0)
{
System.exit(0);
}
}
if (s.equals("帮助"))
{
JOptionPane.showMessageDialog(this,
"猜数字游戏java窗体代码笔记,可以输入限制次数,密码为123,\n在菜单:系统——输入密码 中输入java窗体代码笔记!");
}
if (s.equals("输入密码"))
{
while (true)
{
String inputValue = "";
inputValue = JOptionPane.showInputDialog(this, "输入密码");
// System.out.println(inputValue);
if (inputValue == null)
return;
if (inputValue.equals(strsecret))
{
jinru = true;
jttishi.setText("密码正确,点击开始");
inputsecret.setEnabled(false);
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "密码错误,重新输入?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
if (!jinru)
{
jttishi.setText("请输入密码:");
return;
}
if (s.equals("开始") || s.equals("重玩"))
{
StringBuffer sb = new StringBuffer("0123456789");
for (int i = 0; i c.length; i++)
{
int n = (int) (Math.random() * sb.length());
c[i] = sb.charAt(n);
sb.deleteCharAt(n);
}
btn.setText("重玩");
start = true;
lbnum.setText(String.valueOf(xianzhi));
jisuan = false;
//System.out.println(c);
jttishi.setText("请输入四不重复的数:");
jtinput.setText("");
tafo.setText("");
}
if (s.compareTo("0") = 0 s.compareTo("9") = 0)
{
jisuan = false;
if (!start)
jttishi.setText("点击开始!");
else
{
if (jtinput.getText().length() 4)
{
if (jtinput.getText().indexOf(s) 0)
{
jtinput.setText(jtinput.getText() + s);
if (jtinput.getText().length() 4)
jttishi.setText("还差"
+ (4 - jtinput.getText().length()) + "个数");
else
{
jttishi.setText("输入正确,点击确定");
jisuan = true;
}
} else
jttishi.setText("不能输入重复的数");
} else
{
jttishi.setText("不能超过四个数,点击确定");
jisuan = true;
}
}
}
if (s.equals("退格") start)
{
if (jtinput.getText().length() 0)
{
jtinput.setText(jtinput.getText().substring(0,
jtinput.getText().length() - 1));
jttishi.setText("还差" + (4 - jtinput.getText().length()) + "个数");
}
jisuan = false;
}
if (s.equals("确定"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
if (!jisuan)
{
jttishi.setText("输入不正确!");
return;
}
int numa = 0, numb = 0;
char[] c1 = jtinput.getText().toCharArray();
// System.out.println(c1);
for (int i = 0; i c1.length; i++)
{
for (int j = 0; j c.length; j++)
{
if (c1[i] == c[j] i != j)
{
numb++;
}
if (c1[i] == c[j] i == j)
{
numa++;
}
}
}
tafo.append(jtinput.getText() + "\t" + numa + "A" + numb + "B"
+ "\n");
jtinput.setText("");
jisuan = false;
if (numa == 4)
{
jttishi.setText("答对java窗体代码笔记了,再来一局吧!");
start = false;
btn.setText("开始");
tafo.append("恭喜,答对了! ");
} else
{
lbnum.setText(String
.valueOf(Integer.valueOf(lbnum.getText()) - 1));
if (Integer.valueOf(lbnum.getText()) == 0)
{
String str = "";
jttishi.setText("机会没有了!很遗憾");
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("超过限制次数,失败!" + "正确答案是:" + str);
start = false;
btn.setText("开始");
} else
jttishi.setText("输入四个数:");
}
}
if (s.equals("正确答案"))
{
if (!start)
{
jttishi.setText("还没开始呢!");
return;
}
String str = "";
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("正确答案是:" + str);
jttishi.setText("重新开始吧!");
start = false;
jisuan = false;
btn.setText("开始");
}
if (!start)
{
inputcishu.setEnabled(true);
} else
inputcishu.setEnabled(false);
if (s.equals("输入限制次数"))
{
while (true)
{
try
{
String str = JOptionPane.showInputDialog(this,
"输入小于15的整数数字:");
if (str == null)
return;
if (Integer.valueOf(str) = 15 Integer.valueOf(str) 0)
{
xianzhi=Integer.valueOf(str);
lbnum.setText(String.valueOf(xianzhi));
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
} catch (Exception ex)
{
int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?", "",
JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
}
}
我做课程设计的最初代码 参考下吧 运行时先看帮助~
java窗体代码笔记的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java窗口编程、java窗体代码笔记的信息别忘了在本站进行查找喔。







