㈠ 編寫一個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();
}
}