『壹』 使用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;
}