導航:首頁 > 編程語言 > java圖形界面gui設計

java圖形界面gui設計

發布時間:2021-03-04 02:07:44

1. java圖形界面(GUI)

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Window extends Frame{
Button button1 = new Button("開始");
Button button2 = new Button("結束");
TextField textfield = new TextField();
static Random random = new Random();
String string = null;
boolean bstart = false;
Thread thread;

public void launchFrame() {
.setLocation(500, 200);
this.setLayout(new BorderLayout());
this.add(button1, new BorderLayout().WEST);
this.add(button2, new BorderLayout().EAST);
this.add(textfield, new BorderLayout().NORTH);
button1.addActionListener(new Button1Listener());
button2.addActionListener(new Button2Listener());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setVisible(true);

}

private class Button1Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
bstart = true;
thread = new Thread(new NumProce());
thread.start();
}
}

private class Button2Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
bstart = false;
}
}

private class NumProce implements Runnable {

private void numproce() {
string = Integer.toString(random.nextInt()); /*nextint的括弧中處可以修改產生隨機數的范圍*/
textfield.setText(string);
}

public void run() {
while(bstart) {
try {
numproce();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
new Window().launchFrame();
}
}

2. java界面設計的GUI是什麼東西

如果要寫桌面基於Swing窗體的程序,建議用netbeans.
NB的Swing插件比eclipse好用很多。也可以在NB裡面畫好了窗體復制到eclipse里去用。

3. 怎樣用JAVA的GUI(圖形用戶界面)來設計一個計算機呢

你只能做一個計算器而不是一個計算機,計算機跟計算器是兩種不同的東西,特別是對於計算機專業而言,具體的網路一下JAVA計算器,肯定大把

4. 用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;
}
}

5. 怎樣用JAVA的GUI(圖形用戶界面)來設計一個程序!求程序和詳解。

//正好以前注釋過一個
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.awt.image.MemoryImageSource;
importjava.util.Random;
importjavax.swing.JDialog;
importjavax.swing.JPanel;
importjavax.swing.Timer;
{
=1L;
privateRandomrandom=newRandom();
privateDimensionscreenSize;
privateJPanelgraphicsPanel;
privatefinalstaticintgap=20;
privateint[]posArr;
privateintlines;
privateintcolumns;
publicstaticvoidmain(String[]args){
newCharacterRain();
}
publicCharacterRain(){
initComponents();
}
privatevoidinitComponents(){
setLayout(newBorderLayout());
graphicsPanel=newGraphicsPanel();
add(graphicsPanel,BorderLayout.CENTER);
ToolkitdefaultToolkit=Toolkit.getDefaultToolkit();
Imageimage=defaultToolkit.createImage(newMemoryImageSource(0,0,
null,0,0));
CursorinvisibleCursor=defaultToolkit.createCustomCursor(image,
newPoint(0,0),"cursor");
setCursor(invisibleCursor);
=newKeyPressListener();
this.addKeyListener(keyPressListener);
this.setAlwaysOnTop(true);
this.setUndecorated(true);
this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
lines=screenSize.height/gap;//用gap來分行
columns=screenSize.width/gap;//用gap來分列
posArr=newint[columns+1];
random=newRandom();
for(inti=0;i<posArr.length;i++){
posArr[i]=random.nextInt(lines);//隨機行數
}
newTimer(100,this).start();
}
privatechargetChr(){
return(char)(random.nextInt(94)+33);//返回隨機字元
}
publicvoidactionPerformed(ActionEvente){
graphicsPanel.repaint();//Timer事件,graphicsPanel重繪
}
@SuppressWarnings("serial")
{
publicvoidpaint(Graphicsg){
Graphics2Dg2d=(Graphics2D)g;
g2d.setFont(getFont().deriveFont(Font.BOLD));
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,screenSize.width,screenSize.height);//設置背景色為黑色
intcurrentColumn=0;
for(intx=0;x<screenSize.width;x+=gap){
intendPos=posArr[currentColumn];//獲得開始行位置
g2d.setColor(Color.GREEN);
g2d.drawString(String.valueOf(getChr()),x,endPos*gap);//畫出隨機開始行的字元
intcg=0;//初始黑色
intlength=25;//字元列的長度
for(intj=endPos-length;j<endPos;j++){//隨機行逐列向上length行,為循環開始行
cg+=255/(length+1);//漸變色遞增,0,255,0為green
if(cg>255){
cg=255;
}
g2d.setColor(newColor(0,cg,0));
g2d.drawString(String.valueOf(getChr()),x,j*gap);//畫出隨機行後的15行字元,顏色從黑色漸變成綠色,逐行增加
}
posArr[currentColumn]+=random.nextInt(5);//下落距離最快為4,會產生加速下落的感覺
if((posArr[currentColumn]-5)*gap>getHeight()){//如果行數位置大於屏幕高度,從新獲取合適的行
posArr[currentColumn]=random.nextInt(lines);
}
currentColumn++;//下一列獲取隨機行
}
}
}
{
publicvoidkeyPressed(KeyEvente){
if(e.getKeyCode()==KeyEvent.VK_ESCAPE){//監聽鍵盤事件,按ESC按鍵退出
System.exit(0);
}
}
}
}

6. 救急!怎樣用JAVA的GUI(圖形用戶界面)來設計一個小程序!求程序和詳解。

給你找了一個,我試過,可以用
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;//GUi之前要吧這兩個都引進來
public class Computer extends JFrame implements ActionListener
{
JButton a1,a2,a3,a4,a5,a6,a7,a8,a9,a0;
JButton b1,b2,b3,b4;
JButton c1,c2,c3,c4;
JTextField t1,t2;
JPanel p1,p2;
JLabel bq1,bq2;
String fuhao;
Double count,count2;
boolean chose=false,cliks;

public static void main(String[] args){
Computer l = new Computer();
}
public Computer(){
Font font = new Font("宋體", Font.BOLD, 36);
Font font2 = new Font("宋體", Font.BOLD, 20);

a1 = new JButton("1");
a1.setFont(font);
a1.addActionListener(this);
a2 = new JButton("2");
a2.setFont(font);
a2.addActionListener(this);
a3 = new JButton("3");
a3.setFont(font);
a3.addActionListener(this);
a4 = new JButton("4");
a4.setFont(font);
a4.addActionListener(this);
a5 = new JButton("5");
a5.setFont(font);
a5.addActionListener(this);
a6 = new JButton("6");
a6.setFont(font);
a6.addActionListener(this);
a7 = new JButton("7");
a7.setFont(font);
a7.addActionListener(this);
a8 = new JButton("8");
a8.setFont(font);
a8.addActionListener(this);
a9 = new JButton("9");
a9.setFont(font);
a9.addActionListener(this);
a0 = new JButton("0");
a0.setFont(font);
a0.addActionListener(this);

b1 = new JButton("清空");
b1.addActionListener(this);
b2 = new JButton("返回");
b2.addActionListener(this);
b3 = new JButton(".");
b3.addActionListener(this);

b4 = new JButton("=");
b4.addActionListener(this);

c1 = new JButton("+");
c1.addActionListener(this);

c2 = new JButton("-");
c2.addActionListener(this);
c3 = new JButton("x");
c3.addActionListener(this);
c4 = new JButton("÷");
c4.addActionListener(this);

t1 = new JTextField(25);
t2 = new JTextField(35);
t1.setFont(font2);
t2.setFont(font2);

p1 = new JPanel();
p2 = new JPanel();

bq1 = new JLabel("結");
bq2 = new JLabel("果");

p1.setLayout(new GridLayout(2,3));
p2.setLayout(new GridLayout(4,4));

p1.add(t1);p1.add(b1);p1.add(b2);
p1.add(t2);p1.add(bq1);p1.add(bq2 );

p2.add(a1);p2.add(a2);p2.add(a3);p2.add(c1);
p2.add(a4);p2.add(a5);p2.add(a6);p2.add(c2);
p2.add(a7);p2.add(a8);p2.add(a9);p2.add(c3);
p2.add(b3);p2.add(a0);p2.add(b4);p2.add(c4);

this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);

this.setSize(460,380);
this.setTitle("簡易計算器");
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object temp = e.getSource();
if(temp == a1){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"1");
chose=false;
}
if(temp == a2){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"2");
chose=false;

}
if(temp == a3){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"3");
chose=false;

}
if(temp == a4){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"4");
chose=false;
}
if(temp == a5){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"5");
chose=false;
}
if(temp == a6){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"6");
chose=false;
}
if(temp == a7){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"7");
chose=false;
}
if(temp == a8){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"8");
chose=false;
}
if(temp == a9){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"9");
chose=false;
}
if(temp == a0){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"0");
chose=false;
}
if(temp==b3){
cliks=true;
for(int i=0;i<t1.getText().length();i++){
if('.'==t1.getText().charAt(i)){
cliks=false;
break;
}
if(cliks==true){
t1.setText(t1.getText()+".");
}
}
}

if(temp== c1){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "+";
}
if(temp== c2){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "-";
}
if(temp== c3){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "*";
}

if(temp== c4){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "÷";
}
if(temp==b1){
t1.setText("");
t2.setText("");
}
if(temp==b2){
String s=t1.getText();
t1.setText("");
for(int i=0;i<s.length()-1;i++){
char a = s.charAt(i);
t1.setText(t1.getText()+a);
}
}
if(temp== b4){
count2=Double.parseDouble(t1.getText());
t1.setText("");
if(fuhao=="+"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count+count2+"");
chose=true;
}
if(fuhao=="-"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count-count2+"");
chose=true;
}
if(fuhao=="*"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count*count2+"");
chose=true;
}
if(fuhao=="÷"){
//int sum=count+count2;
if(count2==0){
t1.setText(count+""+fuhao+""+count2+"");
t2.setText("除數不能為0");
return;
}
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count/count2+"");
chose=true;
}

}
}
}

7. 使用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();// 生成類實例

}

}

8. java做圖形界面程序,GUI寫出來怎麼那麼丑啊,java做界面...

java做圖形界面程序,GUI寫出來怎麼那麼丑啊,java做界面...
請在珍惜的時候,好好去愛

閱讀全文

與java圖形界面gui設計相關的資料

熱點內容
蘋果充電寶哪個牌子好 瀏覽:119
測序數據里fast是什麼 瀏覽:576
壓縮文件怎麼改名不會損壞 瀏覽:188
互相看手機的一部電影 瀏覽:842
篩選文件內容怎麼篩 瀏覽:884
工業機器人示教編程是指什麼 瀏覽:566
在線能看的資源電影 瀏覽:308
734什麼錯誤代碼 瀏覽:220
淘寶傳文件軟體 瀏覽:310
韓國電影國語發音 瀏覽:423
第365章天資榜葉凌天 瀏覽:864
大胸乳房電影 瀏覽:200
開網路醫院需要什麼手續 瀏覽:595
2014世界500強word 瀏覽:867
羅蘭曾志偉主演鬼電影全集 瀏覽:961
jsregexpmatch替換 瀏覽:848
飛盧小說vip章節破解方法 瀏覽:497
主角叫陳凡外號屠夫 瀏覽:305
列印文件隱藏軟體 瀏覽:431
js中時分秒正則表達式 瀏覽:569

友情鏈接