導航:首頁 > 編程語言 > javagui圖形界面編程實例

javagui圖形界面編程實例

發布時間:2021-12-09 07:16:37

『壹』 java GUI編程的一個完整的實例

Java程序

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test35 extends JFrame implements ItemListener{
private static final long serialVersionUID = 1L;
JComboBox cmbGender;
JLabel lblMessage;

public Test35(){
("工業09");
String[] arr = new String[];
cmbGender = new JComboBox(arr);
lblMessage = new JLabel("請選擇你的性別");
this.setLayout(new FlowLayout());
this.add(lblMessage);
this.add(cmbGender);
cmbGender.addItemListener(this);
this.setSize(200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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

@Override
public void itemStateChanged(ItemEvent e) {
JComboBox cmb = (JComboBox)e.getSource();
if(cmb == cmbGender){
lblMessage.setText(cmb.getSelectedItem().toString());
}
}
}

運行測試:

『貳』 怎樣用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);
}
}
}
}

『叄』 使用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編寫一個圖形界面

我覺得還是我的好些。。。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TestSwing extends JFrame{
private JLabel label1, label2, label3, label4,label5,label6,label7;

private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;

private JButton button;

private String filepath="c:\\info.dat";
public TestSwing(){
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void init(){
initPanel();
initLabel();
initTextField();
initButton();
initContent();
}

private void initContent(){
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);

panel2.add(button);

panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);

getContentPane().add(panel);
}

private void initPanel(){
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);

panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);

panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
}
private void initLabel(){
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("埠", SwingConstants.LEFT);
label3 = new JLabel("訪問地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);
}

private void initTextField(){
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);
}
private void initButton(){
button = new JButton("完成");
button.addMouseListener(new MouseAdapter(){
//左鍵點擊
public void mouseClicked(MouseEvent e) {
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");
}

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

private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv){
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
} catch (Exception e) {
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}

『伍』 如何進行Java GUI圖形用戶界面編程

在遠標做過直接使用javax.swing和java.awt兩個包
一個簡單的GUI程序如下:
packagesix;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassMain extendsJFrame{ //類Main繼承自JFrame
privateJPanel pane = null;
privateJPanel p = null;
privateCardLayout card = null;
privateJButton button_1 = null;
privateJButton button_2 = null;
privateJButton b1 = null,b2 = null,b3 = null;
privateJPanel p1 = null,p2 = null,p3 = null;
publicMain() //
{
super("卡片布局管理器測試");
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex){
ex.printStackTrace();
}
//創建新卡片布局
card = newCardLayout(5,5);
pane = newJPanel(card);
p = newJPanel();
button_1 = newJButton("< 上一步");
button_2 = newJButton("下一步 >");
b1 = newJButton("1");b2 = newJButton("2");b3 = newJButton("3");
b1.setMargin(newInsets(2,2,2,2));
b2.setMargin(newInsets(2,2,2,2));
b3.setMargin(newInsets(2,2,2,2));
p.add(button_1);p.add(b1);p.add(b2);p.add(b3);p.add(button_2);
p1 = newJPanel();
p2 = newJPanel();
p3 = newJPanel();
p1.setBackground(Color.RED);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.GREEN);
p1.add(newJLabel("JPanel_1"));
p2.add(newJLabel("JPanel_2"));
p3.add(newJLabel("JPanel_3"));
pane.add(p1,"p1");pane.add(p2,"p2");pane.add(p3,"p3");
//翻轉卡片布局動作
button_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.previous(pane);
}
});
button_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.next(pane);
}
});
b1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p1");
}
});
b2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane,"p2");
}
});
b3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p3");
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
newMain(); //
}
}

『陸』 java編程實現圖形界面

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
public class mi
{
private static String username;
private static String password ;
private static JTextField []t={
new JTextField("賬號:",8),new JTextField(10),
new JTextField("密碼:",8),new JPasswordField(10)};

public static void main (String args[]){
JFrame app=new JFrame("賬號密碼演示程序");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(280,120);
Container c=app.getContentPane();
c.setLayout(new FlowLayout());

t[0].setEditable(false);
t[2].setEditable(false);
for(int i=0;i<4;i++)
c.add(t[i]);t[1].setText("");

JButton[]b={new JButton("確定"),new JButton("重置")};
c.add(b[0]);c.add(b[1]);
app.setLocationRelativeTo(null);
app.setVisible(true);

b[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t[1].setText("");
t[3].setText("");
}
});
// 登錄按鈕加事件監聽器
b[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = t[1].getText();
password = t[3].getText();
//判斷用戶名密碼是否正確
if (username.equals("數字") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "登陸成功!", "消息",
JOptionPane.INFORMATION_MESSAGE);

} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!", "錯誤",
JOptionPane.ERROR_MESSAGE);
}
}
}); }

}

『柒』 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)的計算器應用程序,完成簡單的算術運算

下午發
五分鍾搞定,先給你發個簡單的,照例子自己去做你想要的計算器
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圖形界面編程

如果Panel里的內容是用戶程序繪制出來的,那麼你的繪制過程需要放在都應界面控制項(panel)的paintComponent方法里。

『拾』 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);
}
}

}


運行效果如圖

閱讀全文

與javagui圖形界面編程實例相關的資料

熱點內容
韓國電影 下女 百度雲 瀏覽:111
乳電影 瀏覽:312
大數據選址軟體哪個好用 瀏覽:174
男主是蛇女主懷了蛇蛋 瀏覽:47
楠楠是什麼電影 瀏覽:611
word小箭頭怎麼去掉 瀏覽:709
updatususer默認密碼 瀏覽:841
360補丁安裝包在哪個文件夾里 瀏覽:712
織夢靜態文件保存在哪個文件目錄 瀏覽:567
怎麼在電腦上文件帶圖片 瀏覽:798
探險意外穿越到異界的電影 瀏覽:134
日本影片和韓國影片推薦 瀏覽:932
qq郵件pdf文件如何簽名 瀏覽:19
曰韓中文字幕電影免費看 瀏覽:296
已經下架的電影哪裡可以看 瀏覽:776
java讀取xml配置文件 瀏覽:416
法國電影 一個老頭帶一個小女孩 瀏覽:798
手機如何將app壓縮 瀏覽:3
編程乘法運算怎麼弄 瀏覽:961
深圳市吉屋網路技術有限公司 瀏覽:448

友情鏈接