javajfram按钮的简单介绍

博主:adminadmin 2023-01-09 03:33:08 579

本篇文章给大家谈谈javajfram按钮,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么样通过按钮关闭一个JFrame

import javax.swing.JFrame;

public class FrameTest extends JFrame {

public static void main(String[] args) {

new FrameTest("frame 1");

new FrameTest("frame 2");

new FrameTest("frame 3");

}

public FrameTest(String title) {

this.setTitle(title);

this.setSize(800, 600);

// this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

// this.setDefaultCloseOperation(HIDE_ON_CLOSE);

// this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

this.setVisible(true);

}

}

只要在每个Frame里设定this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);即可。

EXIT_ON_CLOSE,关闭程序。(所有窗口和进程都会关闭)

DISPOSE_ON_CLOSE,只关闭本窗口。

HIDE_ON_CLOSE,只隐藏本窗口,不关闭。

DO_NOTHING_ON_CLOSE,不做任何事,点击关闭无效。

java中JFrame按钮添加事件,选择路径放到文本框里面

无语了,这个破知道,提交代码不过,改成11,就成功了!代码提交中

你需要的方法在按钮事件方法中,有问题追问,good luck!

通过选中文件获得绝对路径,点确定读取文件

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

public class Test2 extends JFrame implements ActionListener {

JButton button;

JButton Select;

JButton btnOK;

JTextField textfield;

JPanel p;

JFileChooser fc = new JFileChooser();

TextArea area;

public Test2() {

p = new JPanel(); // 建立一个面板

this.getContentPane().add(p);// 把面板添加到框架

p.add(new JButton("文本"));// 把一个文本按钮添加到面板

textfield = new JTextField(10);

p.add(textfield); // 把一个文本框添加到面板

Select = new JButton("浏览");

p.add(Select); // 把一个浏览按钮添加到面板

Select.addActionListener(this);

btnOK = new JButton("确定");

p.add(btnOK);// 把一个确定按钮添加到面板

btnOK.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

// 当按下选择按钮,打开一个文件选择,文本框显示文件路径

if (e.getSource() == Select) {

int intRetVal = fc.showOpenDialog(this);

if (intRetVal == JFileChooser.APPROVE_OPTION) {

textfield.setText(fc.getSelectedFile().getPath());

}

} else if (e.getSource() == btnOK) { // 当按下确定按钮,生成一个新框架,框架里面有一个文本域,显示打开文件的内容

JFrame f = new JFrame();

f.setSize(400, 400);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String extensionName = getExtensionName(textfield.getText());

if ("txt".equals(extensionName)) {

f.setTitle("显示文本");

area = new TextArea();

//获取文本值

String text = readTxt(textfield.getText());

area.setText(text);

f.add(area);

f.setVisible(true);

} else if ("jpg".equals(extensionName) || "png".equals(extensionName) || "gif".equals(extensionName)) {

f.setTitle("显示图片");

Icon img = new ImageIcon(textfield.getText());

JLabel label = new JLabel(img);

//添加滚动条

JScrollPane jsp = new JScrollPane(label);

f.add(jsp);

f.setVisible(true);

} else {

JOptionPane.showMessageDialog(null, "请选择txt/jpg/png/gif格式的文件!");

}

}

}

/**

* @Description:获取文件后缀名

* @param filename

* @return

* @throws

*/

private String getExtensionName(String filename) {

if ((filename != null) (filename.length() 0)) {

int dot = filename.lastIndexOf('.');

if ((dot -1) (dot (filename.length() - 1))) {

return filename.substring(dot + 1);

}

}

return filename;

}

/**

* @Description:读取文件

* @param path - 文件地址

* @return

* @throws

*/

private String readTxt(String path) {

if (path == null || "".equals(path)) {

return "";

}

StringBuffer sb = new StringBuffer();

File file = new File(path);

InputStreamReader read = null;

BufferedReader reader = null;

try {

read = new InputStreamReader(new FileInputStream(file), "gb2312");

reader = new BufferedReader(read);

String line;

while ((line = reader.readLine()) != null) {

sb.append(line);

sb.append("\n");

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (read != null) {

try {

read.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return sb.toString();

}

public static void main(String[] args) {

Test2 frame = new Test2();

frame.setSize(400, 400);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

java Frame按钮乱码怎么办

字符集问题,Eclipse和CMD

DOS窗口默认字符集都是GBK,如果你的程序是用其他字符集编写的,就会产生乱码了。把你的字符集都改成默认的GBK字符集就OK了。

如果你的程序是在Eclipse下编写的,请查看Eclipse的

窗口---首选项---常规---工作空间---文本文件编码是不是缺省值GBK,如不是请改回来就行了。

java中如何在jframe标题栏中添加按钮

让窗口全屏幕显示,并且不显示标题栏的情况在常用的软件开发中不是非常多见,但是对于开发如视频播放器这样的软件时,这样的功能就变得不可缺少了,那么如何实现这两个功能呢?代码示例如下: JFrame f = new JFrame(test frame); f.setUndecorated(true); f.setSize(300, 300); f.setVisible(true); 运行如上程序,您将会发现一个没有任何边框和标题栏的窗口显示在界面上了。但是这里需要注意的是,setUndecroated方法必须在 setVisible之前被执行,一定要确保Frame窗口是新创建并且没有做过任何显示,甚至是pack动作也不能做过,否则你会得到一个异常

java实现简单登录界面,就是按个按钮就能从一个JFRAME跳转到另一个,基本就是按登录就能跳转

java实现的简单登录页面,从一个按钮点击后跳转的页面的jframe写法:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class jb{

public static void main(String args[]){

JFrame f=new JFrame("点我跳转");

Container contentPane=f.getContentPane();

contentPane.setLayout(new GridLayout(1,2));

Icon icon=new ImageIcon("b.jpg");

JLabel label2=new JLabel("a",icon,JLabel.CENTER);

label2.setHorizontalTextPosition(JLabel.CENTER);

contentPane.setLayout(new FlowLayout( FlowLayout.CENTER,10,10));

JButton bb=new JButton("图片");

bb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JFrame bf=new JFrame("新窗体");

Icon icon=new ImageIcon("enter.jpg");

JLabel label2=new JLabel(icon);

bf.getContentPane().add(label2);

bf.setSize(300,360);

bf.show();

}});

contentPane.add(label2);

contentPane.add(bb);

f.pack();

f.show();

}}

关于javajfram按钮和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。