导航:首页 > 编程语言 > javagui画面设计

javagui画面设计

发布时间:2023-05-28 22:56:17

『壹』 使用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界面

效果图

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
{
JLabeljlkey;
publicKeyFrame(){
=newJLabel("请输入字母或者数字,其他字符不显示");
add(jlkey);
addKeyListener(this);
setLayout(newFlowLayout());
setSize(260,160);
setTitle("输入...");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newKeyFrame();
}
publicvoidkeyTyped(KeyEvente){//敲击键盘
c=e.getKeyChar();//得到键入的字符
repaint();//重绘
}

publicvoidkeyPressed(KeyEvente){//按下键盘
// c=e.getKeyChar();
// repaint();
}
publicvoidkeyReleased(KeyEvente){//释放键盘

}
charc;
@Override
publicvoidpaint(Graphicsg){
super.paint(g);

//如果只能显示输入的是字母或者数字,那么需要if判断下
if((c<='Z'&&c>='A')||(c<='z'&&c>='a')||(c<='9'&&c>='0')){//注意比较的是字符'9'和字符'0'
Fontfont=g.getFont();
g.setColor(Color.BLUE);
g.setFont(newFont(font.getName(),font.getStyle(),20));
g.drawString(c+"",100,100); //绘制
}
}
}

『叁』 java GUI界面的设计工具有哪些

Eclipse开发环境下Java可视化编程。
首先打开eclipse
Help→Instal
New
Software
在Work
with输入
http://download.eclipse.org/windowbuilder/WB/release/R201506241200-1/4.4/
这里把4.4改成你自己的eclipse版本号,如果搜索不到适合版本的,再选择4.4版本
将下方出现的全部安装。
等安装完成后,要重启eclipse。
启动eclipse
在包名上右键→New→Other→WindowBuilder
文件夹下,就是实现可视化编程功能
然后输入类名,完成创建。
创建完成会直接出现基本代码
在代码最下,有两个按键
Source就是当前页面的代码
Design就是可视化编程设计界面
点击Design后,稍等一会。
当出现设计界面,就可以设计图形界面了。
原文:http://blog.csdn.net/dkbnull/article/details/48368913

『肆』 请设计一个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=newJPanel(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设计QQ那样的界面

代码没法给你写,但是道理是很简单的.其实QQ的列表原理非常简单,其界面就是一颗版JTree,设置树根不可见权,树根的没给子节点就是每个分组.而列表内容的实现就更简单了,自己写一个实现了TableCellRenderer的渲染器,然后给设置为分组下每个节点的渲染器就行了.
数据结构也超简单,就是三层树,第一层是根root,设置为不可见,所以只能看到他的几个节点;第二层是根root的节点,也就是分组,有几个分组就有几个节点,新建一个分组就是在root上添加一个新的子节点;第三层就是每个分组的内容了,这就是QQ列表的实现原理.
别告诉我你不知道渲染器是什么,如果还没学到,等学到了你就能做出来.

『陆』 设计一个java程序,实现gui界面和数组的操作!

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class SortRandomNumber extends JPanel {
private int[] numbers = new int[33];
private Random r = new Random();
public SortRandomNumber() {
setPreferredSize(new Dimension(400, 100));
setBackground(new Color(249, 249, 0));
final JButton[] buttons = { new JButton("0"), new JButton("0"),
new JButton("0"), new JButton("0"), new JButton("0"),
new JButton("0"), new JButton("0") };
JPanel btnPanel = new JPanel();
btnPanel.setOpaque(false);
setLayout(new BorderLayout());
add(btnPanel, BorderLayout.NORTH);
for (JButton b : buttons) {
b.setBackground(new Color(205, 151, 249));
b.setFocusPainted(false);
btnPanel.add(b);
}
JPanel opPanel = new JPanel();
opPanel.setOpaque(false);
add(opPanel, BorderLayout.SOUTH);
final JButton[] opButtons = { new JButton("创建数字"), new JButton("排序"),
new JButton("重置") };
for (JButton b : opButtons) {
b.setBackground(new Color(152, 205, 249));
b.setFocusPainted(false);
b.setFont(new Font("微软雅黑", Font.PLAIN, 16));
opPanel.add(b);
}
opButtons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingWorker<int[], Object> worker = new SwingWorker<int[], Object>() {
@Override
protected int[] doInBackground() throws Exception {
Arrays.fill(numbers, 0);
for (int i = 0; i < numbers.length; i++) {
int temp = r.nextInt(33);
if (numbers[temp] != 0) {
i--;
continue;
}
numbers[temp] = i + 1;
}
return numbers;
}

阅读全文

与javagui画面设计相关的资料

热点内容
网络加载视频失败是怎么回事 浏览:805
传奇账号在哪个文件夹里 浏览:346
百度app在哪里安装 浏览:587
如何设置路由器网络不断网 浏览:471
传到qq群里的文件怎么删除 浏览:861
索尼安卓71更新日志 浏览:234
怎么找手机里的垃圾app 浏览:540
2015蓝桥杯代码填空 浏览:698
安卓数据库dbexecSQL 浏览:227
doc重命名文件格式 浏览:728
getscreen截图工具下载 浏览:719
共识数据是什么时候开始的 浏览:96
数码管显示电压程序 浏览:479
数据库文件有哪个 浏览:543
途强储存在哪个文件夹 浏览:172
如何恢复被覆盖文件 浏览:611
iphone5用哪个版本最好 浏览:327
extjsgrid禁用 浏览:426
如何查找国外论文的编程代码 浏览:366
暗金颜色代码 浏览:789

友情链接