❶ java GUI 设计一个身份证信息的用户界面。当用户提交输入信息后,弹出一个消息框,将输入的内容显示在其中
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test extends JFrame {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel l1 = new JLabel("姓名"链宽顷);
JLabel l2 = new JLabel("身份号码");
JTextField f1 = new JTextField();
JTextField f2 = new JTextField();
JButton b1 = new JButton("巧逗提棚陆交");
public test()
{
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,2));
p1.add(l1);
p1.add(f1);
p1.add(l2);
p1.add(f2);
this.add(p1,BorderLayout.NORTH);
p2.add(b1,BorderLayout.CENTER);
this.add(p2,BorderLayout.CENTER);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"姓名:"+f1.getText()+"\n身份证号码:"+f2.getText());
}
});
this.setSize(200,120);
this.setVisible(true);
}
public static void main(String[] args) {
test t = new test();
}
}
❷ 请设计一个GUI界面,参考如下界面原型实现。求大神用JAVA
importjava.awt.Color;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Random;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
{
privateJButton[]btns=newJButton[7];
privateRandomrn=newRandom();
privateboolean[]flags=newboolean[33];//用来判别重复
publicMyPanel(){
init();
}
privatevoidinit(){
setTitle("MyPanel....");
setSize(540,250);
setContentPane(createPane());
}
privateJPanelcreatePane(){
JPanelpane=new脊侍JPanel(null);
pane.setBackground(Color.YELLOW);
for(inti=0;i<btns.length;i++){
btns[i]=newJButton("0");
btns[i].setBounds(20+(i*70),30,54,24);
btns[i].setBackground(Color.PINK);
pane.add(btns[i]);
btns[i].addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
while(true){
intn=rn.nextInt(34);
//如果数字被使用,或为3334就重新选数字
if(n==0||n>33||flags[n-1]){
continue;
}
//把使用了的数字设置为true,即已使用
flags[n-1]=true;
//把不用了的数字设置成未使用状态
if(Integer.parseInt(((JButton)e.getSource())
.getText())!=0){
flags[Integer.parseInt(((JButton)e.getSource())
.getText())-1]=false;
}
((JButton)e.getSource()).setText(String.valueOf(n));
break;
}
}
});
}
JButtoncreNum=蚂野虚newJButton("创建数字");
creNum.setBounds(50,150,100,30);
creNum.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
for(inti=0;i<btns.length;i++){
while(true){
intn=rn.nextInt(34);
//如果数字被使用,或为3334就重新选数字
if(n闷燃==0||n>33||flags[n-1]){
continue;
}
//把使用了的数字设置为true,即已使用
flags[n-1]=true;
//把不用了的数字设置成未使用状态
if(Integer.parseInt(btns[i].getText())!=0){
flags[Integer.parseInt(btns[i].getText())-1]=false;
}
btns[i].setText(String.valueOf(n));
break;
}
}
}
});
pane.add(creNum);
JButtonsort=newJButton("排序");
sort.setBounds(200,150,100,30);
sort.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//排序
String[]strs=newString[7];
//把按钮上面的数字拿出来
for(inti=0;i<btns.length;i++){
strs[i]=btns[i].getText();
}
//开始排序
for(inti=strs.length-1;i>0;i--){
for(intj=0;j<i;j++){
if(Integer.parseInt(strs[i])<Integer
.parseInt(strs[j])){
Stringtemp;
temp=strs[i];
strs[i]=strs[j];
strs[j]=temp;
}
}
}
//排好,在显示到按钮上
for(intj=0;j<strs.length;j++){
btns[j].setText(strs[j]);
}
}
});
pane.add(sort);
JButtonreset=newJButton("重置");
reset.setBounds(350,150,100,30);
reset.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
flags=newboolean[33];
for(inti=0;i<btns.length;i++){
btns[i].setText("0");
}
}
});
pane.add(reset);
returnpane;
}
publicstaticvoidmain(String[]args){
MyPanelmp=newMyPanel();
mp.setVisible(true);
}
}
根据你的要求,数字不会重复,用的是冒泡排序
希望对你有帮助
如图:
❸ 用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计液念算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao="";
public YunSuan()
{
jf=new JFrame("加减唤纤乘除运闹链困算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");
}
public void Jiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args[])
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}
❹ Java实现GUI编程基本方法都有那些
其实无论在什么平台下,GUI应用程序的基本开发方法都是相似的。一般都包括下面这样四个步骤:
①创建容器
首先要创建一个GUI应用程序,需要创建一个用于容纳所有其它GUI组件元素的载体,Java中称为容器。典型的包括窗口(Window)、框架(Frame/JFrame)、对话框(Dialog/JDialog)、面板(Panel/JPanel)等。只有先创建了这些容器,其它界面元素如按钮(Button/JButton)、标签(Label/JLabel)、文本框(TextField/JTextField)等才有地方放。
②添加组件
为了实现GUI应用程序的功能,为了与用户交换,需要在容器上添加各种组件/控件。这需要根据具体的功能要求来决定用什么组件。例如,如果需要提示信息,可用标签(Label/JLabel);如果需要输入少量文本,可用文本框(TextField/JTextField);如果需要输入较多文本,可用文本区域(TextArea/JTextArea);如果需要输入密码,可用密码域(JPasswordField)等等。
③安排组件
与传统的Windows环境下的GUI软件开发工具不同,为了更好地实现跨平台,Java程序中各组件的位置、大小一般不是以绝对量来衡量,而是以相对量来衡量。例如有时候,程序的组件的位置是按"东/East"、"西/West"、"南/South"、"北/North"、"中 /Center"这种方位来标识的。因此,在组织界面时,除了要考虑所需的组件种类外,还需要考虑如何安排这些组件的位置与大小。这一般是通过设置布局管理器(Layout Manager)及其相关属性来实现的。事实上上述按方位来安排组件就是采用了Java中多种布局管理器里的BorderLayout布局管理器。
④处理事件
为了完成一个GUI应用程序所应具备的功能,除了适当地安排各种组件产生美观的界面外,还需要处理各种界面元素事件,以便真正实现与用户的交换,完成程序的功能。在Java程序中这一般是通过实现适当的事件监听者接口来完成的。比如如果需要响应按钮事件,就需要实现 ActionListener监听者接口;如果需要响应窗口事件,就需要实现WindowListener监听者接口。
❺ 编写一个Java多线程GUI程序(GUIThread.java),有两个按钮,点击第一个按钮、启动一个线程开始计时,并把
你要的是这个吧:启雀枝
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUIThread extends JFrame implements java.awt.event.ActionListener{
private JButton jb1 = new JButton("计时");
private JButton jb2 = new JButton("当前时间岁兆");
private JLabel jl1 = new JLabel("");
private JLabel jl2 = new JLabel("");
public GUIThread(){
super("GUIThread");
this.setSize(300,200);
this.setLocation(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2,2));
this.jb1.addActionListener(this);
this.jb2.addActionListener(this);
this.add(jb1);
this.add(jl1);
this.add(jb2);
this.add(jl2);
this.setVisible(true);
}
public static void main(String[] args){
new GUIThread();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object jb = e.getSource();
if(jb == jb1){
new TimingTime(jl1).start();
jb1.setEnabled(false);
}else{
new NowTime(jl2).start();
}
}
}
class NowTime extends Thread{
private JLabel jl;
public NowTime(JLabel jl){
this.jl = jl;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
} catch (InterruptedException e) {
//悄敏 TODO Auto-generated catch block
e.printStackTrace();
}
Date d = new Date() ;
this.jl.setText(d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
}
}
}
class TimingTime extends Thread{
private JLabel jl;
private long l = new Date().getTime();
public TimingTime(JLabel jl){
this.jl = jl;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int emp = 1000;
int i = (int) ((new Date().getTime() - l)/emp);
jl.setText(String.valueOf(i));
}
}
}
❻ JAVA .编写GUI程序,要求:用户在密码框中输入数据,将输入的字符显示在另一个文本框中。
这个和你想要的差不多了
import java.awt.FlowLayout;
import java.awt.TextField;
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.JTextField;
public class Test extends JFrame implements ActionListener {// 继承窗体JFrame,声明借口ActionListener。
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel input = new JLabel(" 请输入密码:");// 实例化一个标签对象。
TextField password = new TextField(13);// 实例化一个文本框对象。
JButton submit = new JButton("提交");// 实例化一个按钮对象。
JButton reset = new JButton("重置歼升");
JLabel output = new JLabel("你输入的密码是:");
JTextField show = new JTextField(10);
Test() {// 构造函数
super("0.0");// 窗体名字。
this.setLayout(new FlowLayout());// 窗体布局。(流式布局)
submit.addActionListener(this);// 给按钮添加事件监听。(给按钮注册监听器)
reset.addActionListener(this);
this.add(input);// 将各组件添加在窗体上。
password.setEchoChar('*');// 设置掩码。
this.add(password);
this.add(output);
this.add(show);
this.add(submit);
this.add(reset);
this.setSize(245, 200);// 设置窗体大小。
this.setVisible(true);// 设哪虚置窗体可见。
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体可关闭,程序可正常退出。
}
public static void main(String[] args) {
new Test();// 实例化类
}
public void actionPerformed(ActionEvent e) {
String str = password.getText();// 将氏缓老password文本框中的字符取出存在str中。
JButton jb = (JButton) e.getSource();// 获得按钮事件的事件源
if (jb == submit) {// 点击了submit按钮
show.setText(str);// 设置show文本框中的内容为str中的内容
}
if (jb == reset) {// 点击了reset按钮
password.setText(null);// 文本框清空
show.setText(null);
}
}
}
❼ java编程中,GUI界面的一般框架有什么希望大神给写个程序实例,程序内容包括布局的应用,多个
java GUI一般,通过AWT,SWING 实现
其余比较流行的是SWT,但是它不是官方库, 并且旦核不能跨平台
//注意:
Swing, 所以很多不涉及界面组件的一些类比如布局 等Swing也可以使用
但是组件,最好不要混用, 容易出现一些组件叠加,等显示错误 比如JButton TextField等混用
//参考代码
下面使用Swing组件来实哗告现
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
//本类实现了ActionListener接口.一个ActionListener可以响应JMenuItem和JButton的动作
//本类实现FocusListener接口,一个FocusListener可以响应JTextField,JButton等
//JButton响应多个事件接口
,FocusListener{
privateJTextFieldjtf1;
privateJTextFieldjtf2;
privateJTextFieldjtf3;
privateJButtonjb1;
publicMyGuiFrame(){
//----------窗口属性乱迟明的设置----------
setTitle("窗口应用程序");//窗口标题
setSize(380,120);//窗口大小
setLocationRelativeTo(null);//窗口居于屏幕中央
setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭窗口后退出jvm虚拟机
getContentPane().setLayout(newBorderLayout(5,3));//边界布局,水平间距5,垂直间距3
//菜单栏组件初始化
initMenu();
//主要面板的初始化
initPanel();
}
privatevoidinitPanel(){
JPaneljp=newJPanel(newFlowLayout(FlowLayout.CENTER));//流式布局
jtf1=newJTextField(8);
jtf1.addFocusListener(this);//添加焦点响应
JLabeljl=newJLabel("+");
jtf2=newJTextField(8);
jtf2.addFocusListener(this);
jb1=newJButton("=");
jb1.addActionListener(this);//添加动作响应
jb1.addFocusListener(this);//添加焦点响应
jtf3=newJTextField(8);
jtf3.setEditable(false);
jp.add(jtf1);
jp.add(jl);
jp.add(jtf2);
jp.add(jb1);
jp.add(jtf3);
getContentPane().add(jp,BorderLayout.CENTER);
}
privatevoidinitMenu(){//菜单栏的初始化和设置
JMenuBarjmb=newJMenuBar();
JMenujm1=newJMenu("系统");
JMenuItemjmi101=newJMenuItem("退出");
jmi101.addActionListener(this);//添加动作响应
JMenujm2=newJMenu("帮助");
JMenuItemjmi201=newJMenuItem("功能说明");
jmi201.addActionListener(this);
jm1.add(jmi101);
jm2.add(jmi201);
jmb.add(jm1);
jmb.add(jm2);
setJMenuBar(jmb);//设置菜单栏
}
//main方法,创建对象窗口,并且设置可见
publicstaticvoidmain(String[]args){
newMyGuiFrame().setVisible(true);
}
//动作响应处理
publicvoidactionPerformed(ActionEvente){
Stringcmd=e.getActionCommand();//根据命令来区分不同的操作
if(cmd.equals("退出")){
System.exit(0);
}
if(cmd.equals("功能说明")){
JOptionPane.showMessageDialog(this,"加法");
}
if(cmd.equals("=")){
Strings1=jtf1.getText().trim();
Strings2=jtf2.getText().trim();
if(s1.equals("")){
s1="0";
}
if(s2.equals("")){
s2="0";
}
doublenum1=Double.parseDouble(s1);//从字符串转小数
doublenum2=Double.parseDouble(s2);
jtf3.setText((num1+num2)+"");//数字类型转字符串类型
}
}
//焦点响应处理
publicvoidfocusGained(FocusEvente){//获得焦点
JComponentcmp=(JComponent)e.getComponent();//根据事件来源组件来区分不同的操作
if(cmp==jtf1||cmp==jtf2){
cmp.setBorder(BorderFactory.createLineBorder(Color.BLUE));//设置边框
}
if(cmp==jb1){
jb1.setForeground(Color.RED);//设置文字颜色
}
}
publicvoidfocusLost(FocusEvente){//失去焦点
JComponentcmp=(JComponent)e.getComponent();
if(cmp==jtf1||cmp==jtf2){
cmp.setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
if(cmp==jb1){
jb1.setForeground(Color.BLACK);
}
}
}
运行效果如图
❽ 使用Java的GUI图形用户界面编程设计并编写一个计算器程序
进口的java.awt.BorderLayout;
进口java.awt.GridLayout中;
进口java.awt.event.ActionEvent;
进口java.awt.event.ActionListener;
进口的javax。 swing.JButton;
进口javax.swing.JFrame中;
进口javax.swing.JPanel;
进口javax.swing.JTextField中;
/ /暂时不考虑这个问题甚至添加
/ /点的第二个操作点的操作员点运算符点=结果
公共类计算器实现的ActionListener {
> JTextField的JTF =新的JTextField(10);
私人布尔追加= FALSE;
私人字符串OP1 =“0”;
私人字符串运算符=“+”;
@覆盖
公共无效的actionPerformed(ActionEvent的E){
字符串COMN = e.getActionCommand();。!
/ /处理数字
如果(“0123456789”的indexOf(COMN)= -1){
如果(追加){/ /追加
字符串温度= jtf.getText() ;
jtf.setText(温度+ COMN);
}其他{/ /替换
jtf.setText(COMN);
追加= TRUE;
/ /处理运营商
否则,如果(“+ - * /”的indexOf(COMN)= -1){
OP1 = JTF。的getText();
运算符= COMN;
追加= FALSE;
的} else if(“=”的indexOf(COMN)= -1。){
字符串OP2 = jtf.getText();
双D1 = Double.parseDouble(OP1);
双D2 = Double.parseDouble(OP2);
如果(“+”等于(操作者)。){
D1 = D1 + D2;
}否则,如果(“ - ”等于(操作员)。){
D1 = D1 - D2;
}否则,如果(“*”的equals(操作员)。){
D1 = D1 * D2;
的} else if(“/”等于(操作者)。){
D1 = D1 / D2;
jtf.setText(D1 +“”);
追加= FALSE;
}否则,如果(等于(COMN)“。”){
字符串温度= jtf.getText();
如果(temp.indexOf(“;”)== -1){
jtf.setText(温度+“。”);
追加= TRUE;
否则,如果(“+ / - ”的equals(COMN)){
字符串温度= jtf.getText();
如果(temp.startsWith(“-1”)){
JTF。的setText(temp.substring(1));
}其他{
jtf.setText(“ - ”+温度);
否则,如果(“退格”。等于(COMN)){
字符串温度= jtf.getText();
如果(temp.length()> 0){
jtf.setText(temp.substring(0,temp.length() - 1)); ..
}
的} else if(“CE”等于(COMN)| |“C”等于(COMN)){
jtf.setText(“0”);
追加= FALSE;
公众计算器(){
JFrame的JF =新的JFrame(“计算器”);
jf.add(JTF,BorderLayout.NORTH);
字串[] S1 = {“退格”,“CE”,“C”,“+”,“7”,“8”,“9”,“/”,“4”,
“5”,“6”,“*”,“1”,“2”,“3”,“ - ”,“0”,“+ / - ”,“”,。 “=”};
的JPanel JP =新的JPanel();
jf.add(JP,使用BorderLayout.CENTER);
网格布局GL =新的网格布局(5,4); jp.setLayout(GL);
JButton的[] JB =新的JButton [s1.length];
为(int i = 0;我<s1.length,我+ +){
JB [我] =新的JButton(S1由[i]);
jp.add(JB由[i]);。
JB [I] addActionListener方法,(这);
} jf.add(JP);
jtf.setEditable(假);
jf.setLocation(400,300);
jf.pack();
jf.setResizable(假);/ /设置窗口不可改变
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(真);
公共静态无效的主要(字串[] args){
新的计算器();
这个函数比较简单,不知道能不能满足要求
❾ 请举例说明Java GUI程序设计中事件的处理过程。
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class StudentApplication implements ActionListener
{
JFrame fMain;
JPanel pan;
JLabel labId,labName,labSex,labAge;
JTextField texId,texName,texSex,texAge;
JButton butQuery,butAdd,butUpdate,butDelete;
Connection con;
PreparedStatement ps;
ResultSet rs;
public StudentApplication()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Hospital","sa","123456");
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Connection con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=Hospital_Table;user=sa;password=123456");
}catch(Exception er)
{
JOptionPane.showMessageDialog(fMain,er.getMessage());
}
formStudent();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butQuery)
{
try{
ps=con.prepareStatement("select * from Hospital_Employee where Employee_ID=?");
ps.setString(1,texId.getText());
ResultSet rs= ps.executeQuery();
if(rs.next())
{
texName.setText(rs.getString(2));
texSex.setText(rs.getString(3));
texAge.setText(Integer.toString(rs.getInt(4)));
}
else
{
JOptionPane.showMessageDialog(fMain,"查询失败!");
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(fMain,e1.getMessage());
}
}
if(e.getSource()==butAdd)
{
try{
ps=con.prepareStatement("insert students values(?,?,?,?)");
ps.setString(1,texId.getText());
ps.setString(2,texName.getText());
ps.setString(3,texSex.getText());
ps.setInt(4,Integer.parseInt(texAge.getText()));
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"增加成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"增加失败!");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butUpdate)
{
try{
ps=con.prepareStatement("update students set stu_name=?,sex=?,age=? where stu_id=?");
ps.setString(1,texName.getText());
ps.setString(2,texSex.getText());
ps.setInt(3,Integer.parseInt(texAge.getText()));
ps.setString(4,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"修改成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"修改失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butDelete)
{
try{
ps=con.prepareStatement("delete students where stu_id=?");
ps.setString(1,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"删除成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"删除失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
}
private void formStudent()
{
fMain=new JFrame("学生信息");
pan=new JPanel();
labId=new JLabel("学号");
labName=new JLabel("名字");
labSex=new JLabel("性别");
labAge=new JLabel("年龄");
texId=new JTextField(15);
texName=new JTextField(15);
texSex=new JTextField(6);
texAge=new JTextField(6);
butQuery=new JButton("查询");
butAdd=new JButton("增加");
butUpdate=new JButton("修改");
butDelete=new JButton("删除");
fMain.add(pan);
pan.setLayout(null);
labId.setBounds(10,10,60,25);
labName.setBounds(10,55,60,25);
labSex.setBounds(10,100,60,25);
labAge.setBounds(10,145,60,25);
texId.setBounds(80,10,100,25);
texName.setBounds(80,55,100,25);
texSex.setBounds(80,100,100,25);
texAge.setBounds(80,145,100,25);
butQuery.setBounds(200,10,80,25);
butAdd.setBounds(200,50,80,25);
butUpdate.setBounds(200,100,80,25);
butDelete.setBounds(200,145,80,25);
pan.add(labId);
pan.add(labName);
pan.add(labSex);
pan.add(labAge);
pan.add(texId);
pan.add(texName);
pan.add(texSex);
pan.add(texAge);
pan.add(butQuery);
pan.add(butAdd);
pan.add(butUpdate);
pan.add(butDelete);
fMain.setSize(320,222);
fMain.setVisible(true);
butQuery.addActionListener(this);
butAdd.addActionListener(this);
butUpdate.addActionListener(this);
butDelete.addActionListener(this);
}
public static void main(String[] args)
{
StudentApplication sa=new StudentApplication();
}
}
❿ java gui怎样在特定窗口中插入图片
1.首先一个小错误,这些代码(包括你建立第一个JFrame的代码)最好都放到Swing的线程去,山模不然可能会偶尔发生意外错误。就是这样:SwingUtilities.invokeLater(new Runnable() {@Override public void run() {...在这里写的代码会在Swing线程又叫做EDT里执行...} });
2.Swing里可以用LayoutManager,个人感觉比你一个一个手动设置Bound方便和灵活。
3.然后,一般改成:fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)而不是exit,exit比较粗鲁会终止整个程序,dispose只是终止窗口并在所有窗口都态唯信结束而且主线程也结束才终止程序。
4.getClass().getResource一般用在做成JAR之后,从同一个JAR里取数据,可以用比较简单的Toolkit.getDefaultToolkit().getImage()
5.你已经知道JLabel可以附带图片了。如果你要的效果是图片之上显示东西,这里你心里有了“层”的概念,而帆轮这个概念在Swing里的一个实现是JLayeredPane,去学习这个容器吧。给你个大概的例子:
JLayeredPane layers = new JLayeredPane();//创建
JPanel paneBackground = new JPanel(new BorderLayout());//背景层
paneBackground.add(new JLabel(Toolkit.getDefaultToolkit().getImage("C:/1.jpg")), BorderLayout.CENTER);//加入背景图片,BorderLayout.CENTER单独使用表示填满。
JPanel paneDetails = new JPanel(new FlowLayout());//这层你往里面加东西,就在背景上面了。
paneDetails.add(new JButton("ABC"));//加个按钮试试。
layers.add(paneBackground);layers.add(paneDetails);//把层按照从背后向前的顺序加进去。
fr.add(layers, BorderLayout.CENTER);//把layers加进frame里面去,填满。
纯手写代码笔误难免哦~