导航:首页 > 编程语言 > javagui编程实例

javagui编程实例

发布时间:2023-03-08 23:04:06

㈠ 编写一个java GUI

试一下下面的代码
(如果点击按钮后没有任何变化,将窗口最小化一下就有了)
没有出现这个问题的话,也请告诉我一下~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class painting extends JFrame implements ActionListener{
private JButton round,rectangle,ellipse,beeline;
private JLabel xaxis,yaxis,remain,information;
private JTextField xTF,yTF;
private BorderLayout layout;
private Container cp;
private JPanel pCenter;
Vector<Object> v=new Vector<Object>(); //定义一个集合类用于存储按钮对象

public painting(){ //构造方法 ------------------框架初始化-------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("painting");
setSize(400,500);
layout = new BorderLayout();
cp = getContentPane();
cp.setLayout(layout);

round= new JButton("画圆");
rectangle= new JButton("画矩形");
ellipse= new JButton("画椭圆");
beeline= new JButton("画直线");
xaxis=new JLabel("x坐标");
yaxis=new JLabel("y坐标");
remain=new JLabel("右下角坐标(400,500) ");
xTF=new JTextField("0",5);
yTF=new JTextField("0",5);

JPanel pUp= new JPanel();//第一个面板 在上部
pUp.add(remain);
pUp.add(xaxis);//置两个文本框
pUp.add(xTF);
pUp.add(yaxis);
pUp.add(yTF);
cp.add(pUp, "North");
//pCenter=new JPanel();//第二个面板 在中部
//pCenter.add(information);//置显示说明与画图区
//cp.add(pCenter,"Center");
JPanel pDown= new JPanel();//第三个面板 在下部
pDown.add(round);// 置四个按钮
pDown.add(rectangle);
pDown.add(ellipse);
pDown.add(beeline);
cp.add(pDown, "South");

round.addActionListener(this); //置按钮监听--------------按钮行为监听与响应-------------
rectangle.addActionListener(this);
ellipse.addActionListener(this);
beeline.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {//监听响应
v.add(e.getSource());//将按钮情况存入v中
}

public void paint(Graphics g) { //--------------绘图响应-------------
super.paint(g);
int xx=Integer.parseInt(xTF.getText());//获取位置值
int yy=Integer.parseInt(yTF.getText());
int size=0;
Object o;
//while(v.size()!=size){//当用户点击按钮选择某一种图形时,v的大小就会比size值大1,当绘图完成后,v.size又等于size;效果就是:出现点击 即刻处理
o=v.lastElement();
if(o == round) {g.drawOval(xx,yy,50,50);}
else if (o == rectangle){g.drawRect(xx,yy,100,50);}
else if (o == ellipse) {g.drawOval(xx,yy,100,50);}
else if(o == beeline) {g.drawLine(xx,yy,xx+100,yy);}
size++;
}
}

public static void main(String[] args){ // ------------程序入口-------------
JFrame frame = new painting();
frame.setVisible(true);
}

}

㈡ java 用GUI写一个程序

使用Font类
下边是例子

---------------------------------------------------------------------------------------------
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class FontApp extends JFrame {

public FontApp() {

on(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabel.setFont(new Font(null, Font.ITALIC, 18));
lblNewLabel.setBounds(12, 30, 232, 29);
getContentPane().add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1.setFont(new Font(null, Font.BOLD, 14));
lblNewLabel_1.setBounds(12, 97, 232, 29);
getContentPane().add(lblNewLabel_1);
setVisible(true);
}

public static void main(String[] args) {
new FontApp();
}
}

㈢ 使用Java的GUI图形用户界面编程设计并编写一个计算器程序

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame {
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton jiaButton = new JButton("+");
JButton jianButton = new JButton("-");
JButton chengButton = new JButton("*");
JButton chuButton = new JButton("/");
JButton yuButton = new JButton("%");
JButton jjButton = new JButton("+/-");
JButton sqrtButton = new JButton("sqrt");
JButton dianButton = new JButton(".");
JButton dengButton = new JButton("=");
JButton Button = new JButton("1/x");
JButton backButton = new JButton("Backpace");
JButton cButton = new JButton("C");
public double op1;
public double op2;
public static final int JIA = 0;
public static final int JIAN = 1;
public static final int CHENG = 2;
public static final int CHU = 3;
public static final int JJ = 4;
public static final int DIAN = 5;
public int current0p = 0;
private boolean opEnd = false;

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JTextField result = new JTextField(20);

public Calculator() {
initPanel2();
initPanel3();
panel2.setLayout(new GridLayout(5, 4));
panel1.setLayout(new BorderLayout());
panel1.add(panel3, BorderLayout.NORTH);// 设置位置
panel1.add(panel2, BorderLayout.CENTER);// 设置位置
getContentPane().add(panel1);
addActionListeners();
setSize(260, 260);
setLocation(500, 300);
setVisible(true);
setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("计算器");
}
private void initPanel2() {
// 把组件添加相应panel上

panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(chuButton);

panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(chengButton);

panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(jianButton);

panel2.add(b0);
panel2.add(jjButton);
panel2.add(dianButton);
panel2.add(jiaButton);

panel2.add(Button);
panel2.add(yuButton);
panel2.add(sqrtButton);
panel2.add(dengButton);

}

private void addActionListeners() {
ActionHandler c = new ActionHandler();
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);

jiaButton.addActionListener(c);
dengButton.addActionListener(c);
chengButton.addActionListener(c);
chuButton.addActionListener(c);
jianButton.addActionListener(c);
jjButton.addActionListener(c);
dianButton.addActionListener(c);
sqrtButton.addActionListener(c);
yuButton.addActionListener(c);
Button.addActionListener(c);
backButton.addActionListener(c);
cButton.addActionListener(c);
}

class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (e.getSource() == b0) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "0");

}
if (e.getSource() == b1) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "1");
opEnd = true;
}

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

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "2");
opEnd = true;
}
if (e.getSource() == b3) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "3");
opEnd = true;

}
if (e.getSource() == b4) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "4");
opEnd = true;
}
if (e.getSource() == b5) {

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "5");
opEnd = true;
}
if (e.getSource() == b6) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "6");
opEnd = true;
}
if (e.getSource() == b7) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "7");
opEnd = true;
}
if (e.getSource() == b8) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "8");
opEnd = true;
}
if (e.getSource() == b9) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "9");
opEnd = true;
}
try {
if (e.getSource() == jiaButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIA;
}
if (e.getSource() == chengButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHENG;

}
if (e.getSource() == chuButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHU;

}
if (e.getSource() == jianButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIAN;

}

if (e.getSource() == jjButton) {
String tmp = result.getText();
if (tmp.equals("") || tmp.equals("0")) {
return;
}
if (tmp.charAt(0) == '-') {
tmp = tmp.substring(1);

} else {
tmp = '-' + tmp;
}
result.setText(tmp);
}
if (e.getSource() == dianButton) {
String tmp = result.getText();
if (tmp.equals("")) {
return;
}
if (tmp.indexOf(".") != -1) {
return;
}
tmp = tmp + ".";
result.setText(tmp);

}
if (e.getSource() == sqrtButton) {
String tmp = result.getText();
if (tmp.equals(" ")) {
return;
}
double d;
d = Double.parseDouble(tmp);// 先定义double类型d
if (d < 0) {
result.setText("不能对负数求平方根");
return;
}
op2 = Math.sqrt(d);
result.setText(op2 + "");
}

if (e.getSource() == backButton) {
String s = result.getText();
result.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
result.setText(result.getText() + a);
}

}
if (e.getSource() == cButton) {
result.setText("0");
opEnd = false;
}
if (e.getSource() == dengButton) {
op2 = Double.parseDouble(result.getText());

switch (current0p) {
case JIA:
result.setText(op1 + op2 + "");
break;
case JIAN:
result.setText(op1 - op2 + "");
break;
case CHENG:
result.setText(op1 * op2 + "");
break;
case CHU:
if (op2 == 0) {
result.setText("被除数不能为零");
break;
}
result.setText(op1 / op2 + "");
break;
}
opEnd = false;
}
} catch (Exception e1) {
result.setText("Wrong");
opEnd = false;
}
}

}

private void initPanel3() {

panel3.setLayout(new GridLayout(2, 1));
panel3.add(result);
panel3.add(panel4);
panel4.setLayout(new GridLayout(1, 2));

panel4.add(backButton);
panel4.add(cButton);
// panel3.setPreferredSize(new Dimension(260,65));
}

public static void main(String[] args) {
Calculator c = new Calculator();// 生成类实例

}

}

㈣ 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界面,包括文本域、文本框、按钮等相关功能,实现模拟登陆验证功能。

importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;

publicclassDemoextendsJFrame{

Stringusername;
Stringpassword;
publicDemo(){
username="java";
password="123";

setTitle("#####");
setSize(400,300);
setLocation(400,200);
setLayout(newFlowLayout());

JTextAreajta=newJTextArea(4,30);
jta.setText("请输入用户名密码: 用户名:"+username+" 密码:"+password);
jta.setEditable(false);//设置文本区域不可编辑
add(jta);

JLabelusernameLabel=newJLabel("用户名:");
finalJTextFielsernameText=newJTextField(10);
add(usernameLabel);
add(usernameText);

JLabelpasswordLabel=newJLabel("密码:");
finalJTextFieldpasswordText=newJTextField(10);
add(passwordLabel);
add(passwordText);

JButtonbutton=newJButton("登录");
add(button);

finalJLabelresult=newJLabel();//用来显示登录结果
add(result);

button.addActionListener(newActionListener(){//登录按钮事件

@Override
publicvoidactionPerformed(ActionEvente){
//获取输入的用户名密码
StringtempUserName=usernameText.getText();
StringtempPassWord=passwordText.getText();

if(tempUserName.equals(username)&&tempPassWord.equals(password)){
result.setText("登录成功");
}else{
result.setText("登录失败");
}
}
});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

publicstaticvoidmain(String[]args){
newDemo();
}
}

阅读全文

与javagui编程实例相关的资料

热点内容
怎么样把app添加到小组件 浏览:148
省内顺丰邮文件多少钱 浏览:715
绝密级文件应保留多少年 浏览:701
发文件给同事怎么说 浏览:468
苹果80岁用什么app 浏览:28
顺丰寄快递文件多少钱 浏览:164
消费邦app是怎么反现的 浏览:112
java调用接口方法 浏览:742
微信一种以上绑定关系 浏览:183
word图片编辑大小边框 浏览:468
威迅java培训 浏览:389
linux禅道无法访问 浏览:819
怎么爬取历史疫情数据 浏览:596
linuxjira6破解 浏览:694
哪个网站可以看所有检察杂志 浏览:144
java高并发数据库请求怎么办 浏览:551
win8怎么打开gho文件怎么打开 浏览:732
如何网站内搜索 浏览:362
qq附近的人客服号码 浏览:570
mac怎么把word文件转换为pdf 浏览:6

友情链接