當前位置:首頁 » 操作系統 » java圖形界面源碼

java圖形界面源碼

發布時間: 2022-06-12 21:55:00

『壹』 java圖形界面設計實驗,求源代碼!

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

public class Calculator01 extends JFrame implements ActionListener {
JPanel jp1,jp2;
JTextField jt1,jt2,jt3;
JButton btn_add,btn_sub,btn_clean;


public Calculator01() {
init();
}

public void init() {
setTitle("簡易計算器");
setLocationRelativeTo(null);
setSize(600, 100);
jp1 = new JPanel();
jp2 = new JPanel();
add(jp1, BorderLayout.NORTH);
add(jp2,BorderLayout.SOUTH);
jt1 = new JTextField(15);
jp1.add(jt1);
jt2 = new JTextField(15);
jp1.add(jt2);
jt3 = new JTextField(15);
jp1.add(jt3);

btn_add = new JButton("+");
btn_add.addActionListener(this);
jp2.add(btn_add);
btn_sub = new JButton("-");
btn_sub.addActionListener(this);
jp2.add(btn_sub);
btn_clean = new JButton("清除");
btn_clean.addActionListener(this);
jp2.add(btn_clean);

setVisible(true);
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn_clean)
{
jt1.setText("");
jt2.setText("");
jt3.setText("");
}
else
{
if (jt1.getText().equals("") || jt2.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "請在前兩個框輸入數字");
}
else
{
double number1=Double.parseDouble(jt1.getText());
double number2=Double.parseDouble(jt2.getText());
double result=0;
if (e.getSource() == btn_add)
{
result = number1 + number2;
}
else
{
result = number1 - number2;
}
jt3.setText(""+result);
}
}
}
}

『貳』 求一個簡單的java代碼:(圖形界面)

直接寫main函數里了
public static void main(String[] args) {

JFrame ck = new JFrame("title");
JPanel mb = new JPanel();
mb.setLayout(null);
String str = "test"; //自己定義要顯示什麼
JTextArea ta = new JTextArea(str);
ta.setBounds(0, 0, 100, 30); //自己定義文本區在窗口中的位置和大小
mb.add(ta);
ck.add(mb);
ck.setVisible(true);
ck.setBounds(200, 200, 500, 500); //自己定義窗口位置和大小

}

『叄』 用java圖形界面(GUI)寫java代碼

package com.test;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class aaa extends JFrame implements ActionListener
{
JButton b1, b2 ; //定義兩
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1 )
{
l1.setVisible(true);
}
if(e.getSource() == b2)
{
this.dispose();
}
}

『肆』 java繪圖代碼

畫筆定義

importjava.awt.*;

/**

*@authorHardneedl

*/

interfaceBrush{

voiddoPaint(Graphicsg);

}

畫筆工廠

importjava.awt.*;

/**

*@authorHardneedl

*/

classBrushFactory{

staticfinalintLINE_BRUSH=0;

staticfinalintELLIPSE_BBRUSH=1;

staticfinalintRECTANGLE_BRUSH=2;

staticfinalintPOLYGON_BRUSH=3;

staticfinalintELLIPSE_FILL_BRUSH=4;

staticfinalintRECTANGLE_FILL_BRUSH=5;

staticfinalBrushNO=newNONE();

staticfinalBrushLINE=newLineBrush();

staticfinalBrushELLIPSE=newEllipseBrush();

staticfinalBrushELLIPSE_FILL=newEllipseFillBrush();

staticfinalBrushRECTANGLE=newRectangleBrush();

staticfinalBrushRECTANGLE_FILL=newRectangleFillBrush();

staticfinalBrushPOLYGON=newPolygonBrush();

BrushgetBrush(intbrushIndex){

switch(brushIndex){

caseLINE_BRUSH:returnLINE;

caseELLIPSE_BBRUSH:returnELLIPSE;

caseRECTANGLE_BRUSH:returnRECTANGLE;

caseRECTANGLE_FILL_BRUSH:returnRECTANGLE_FILL;

casePOLYGON_BRUSH:returnPOLYGON;

caseELLIPSE_FILL_BRUSH:returnELLIPSE_FILL;

default:returnNO;

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLACK);

gg.drawLine(70,70,200,200);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.drawOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.fillOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawPolygon(newint[]{48,50,244,483,310},newint[]{36,192,281,302,77},5);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.drawRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.fillRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawString("Nobrushselected!",20,30);

gg.dispose();

}

}

}

圖形界面

importjavax.swing.*;

importjavax.swing.border.*;

importjava.awt.*;

importjava.awt.event.*;

/**

*@authorHardneedl

*/

finalclassDrawextendsJFrame{

publicStringgetTitle(){return"frametitle";}

=newDimension(600,400);

(){returnsize;}

publicDimensiongetMaximumSize(){returnsize;}

publicDimensiongetMinimumSize(){returnsize;}

publicDimensiongetSize(){returnsize;}

=newLineAction();

=newRectangleAction();

=newRectangleFillAction();

=newEllipseAction();

=newEllipseFillAction();

=newPolygonAction();

=newBrushFactory();

privatestaticBrushbrush;

=newJComponent(){

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

if(brush!=null){

brush.doPaint(g);

}

}

publicBordergetBorder(){returnBorderFactory.createLineBorder(Color.BLACK,2);}

};

Draw()throwsHeadlessException{

init();

attachListeners();

doLay();

}

privatevoidinit(){

JMenuBarmenuBar=newJMenuBar();

menuBar.add(newJMenu(lineAction));

JMenuelp=newJMenu("橢圓");

elp.add(ellipseAction);

elp.add(ellipseFillAction);

menuBar.add(elp);

JMenurct=newJMenu("矩形");

rct.add(rectangleAction);

rct.add(rectangleFillAction);

menuBar.add(rct);

menuBar.add(newJMenu(polygonAction));

setJMenuBar(menuBar);

}

privatevoidattachListeners(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

privatevoiddoLay(){

Containercontainer=getContentPane();

container.add(canvas,BorderLayout.CENTER);

JPanelbuttonsPane=newJPanel(newGridLayout(2,3));

buttonsPane.add(newJButton(lineAction));

buttonsPane.add(newJButton(ellipseAction));

buttonsPane.add(newJButton(rectangleAction));

buttonsPane.add(newJButton(polygonAction));

buttonsPane.add(Box.createHorizontalBox());

buttonsPane.add(newJButton(ellipseFillAction));

buttonsPane.add(newJButton(rectangleFillAction));

container.add(buttonsPane,BorderLayout.SOUTH);

pack();

setVisible(true);

}

{

privateRectangleAction(){super("空心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_BRUSH);

canvas.repaint();

}

}

{

privateRectangleFillAction(){super("實心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseFillAction(){super("實心橢圓");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseAction(){super("空心橢圓");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_BBRUSH);

canvas.repaint();

}

}

{

privatePolygonAction(){super("多邊形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.POLYGON_BRUSH);

canvas.repaint();

}

}

{

privateLineAction(){super("直線");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.LINE_BRUSH);

canvas.repaint();

}

}

publicstaticvoidmain(String[]args){newDraw();}

}

『伍』 急求一個java寫的用戶圖形界面程序源碼,GUI。能夠選擇演算法和升序降序以及數據類型

真不懂

『陸』 求java圖形界面樹類編程源碼舉例。類似windows資源管理器那樣的。如附圖,2層2項即可。

publicvoidinit(){
ContainercontentPane=null;
=newDefaultMutableTreeNode("我的電腦");
1=newDefaultMutableTreeNode("網路");
2=newDefaultMutableTreeNode("硬碟");
1_1=newDefaultMutableTreeNode("無限");
1_2=newDefaultMutableTreeNode("有限");
2_1=newDefaultMutableTreeNode("A");
2_2=newDefaultMutableTreeNode("B");
treeNode.add(treeNode1);
treeNode.add(treeNode2);
treeNode1.add(treeNode1_1);
treeNode1.add(treeNode1_2);
treeNode2.add(treeNode2_1);
treeNode2.add(treeNode2_2);
JTreetree=newJTree(treeNode);
contentPane=getContentPane();
JPaneljp=newJPanel();
jp.add(tree);
contentPane.add(jp);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}

『柒』 java圖形界面代碼

importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;

{
JTextAreajta;
JTextFieldjtf;
JButtonjb;

publicReadBook(){
jta=newJTextArea();
jtf=newJTextField(30);
jtf.setText("文件保存路徑如c:\ab.txt");
jb=newJButton("保存文字");
JPaneljp=newJPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500,100,500,380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

jb.addActionListener(newActionListener(){

@Override
publicvoidactionPerformed(ActionEvente){
//-------------核心代碼---------
Stringpath=jtf.getText();
Filef=newFile(path);
Stringtxt=jta.getText().replaceAll(" "," ");
try{
BufferedWriterbw=newBufferedWriter(newFileWriter(f));
bw.write(txt);//寫入文件中
bw.close();

}catch(Exceptione1){
e1.printStackTrace();
}
//-------------核心代碼---------
}
});

}

publicstaticvoidmain(String[]args){
newReadBook();
}
}

『捌』 JAVA的圖形用戶界面代碼

package hao;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ChatPanel extends JPanel {

private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;

JButton music;
public ChatPanel() {
setLayout(new BorderLayout());

text = new JTextPane();
text.setEditable(false);

doc = text.getStyledDocument();//跟蹤文本和圖片寫到該區域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面對JComboBox的巧用***********************************************************************
String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常規", "斜體", "粗體", "粗斜體" };
String[] str_Color = { "黑色", "紅色", "藍色", "黃色", "綠色" };
String[] str_BackColor = { "無色", "灰色", "淡紅", "淡藍", "淡黃", "淡綠" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);

fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//創建一個可以容納多個Box組件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字體~~"), b2 = new JLabel("樣式~~"),b3 = new JLabel("字型大小~~"),b4 = new JLabel("顏色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);

textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));

inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //設置文本區的換行策略。88888*********************************

send = new JButton("發送");
record=new JButton("顯示記錄");
saveRecord=new JButton("儲存記錄");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設置按鈕間距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置Box的邊框線********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);

inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置輸入窗口邊框線*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設置輸入窗口邊框線*******************

JPanel audioPanel = new JPanel();//最上面的邊框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));

music = new JButton("想聽就聽");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//設置輸入窗口邊框線*******************
audioPanel.add(music);

add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}

void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}

public void insert(MessageStyle attrib) {
try {

doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//寫完後接著換行************

} catch (BadLocationException e) {
e.printStackTrace();
}
}

public MessageStyle getMessageStyle(String line) {

MessageStyle att = new MessageStyle();

att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常規")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗體")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜體")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜體")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}

String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("紅色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("藍色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黃色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("綠色")) {
att.setColor(new Color(0, 255, 0));
}

String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("無色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡紅")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡藍")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黃")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡綠")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}

}

熱點內容
資料庫索引結構 發布:2024-11-03 04:02:14 瀏覽:234
xcode加密 發布:2024-11-03 03:53:45 瀏覽:225
演算法設計王曉東pdf 發布:2024-11-03 03:38:51 瀏覽:20
本地資料庫伺服器 發布:2024-11-03 03:33:07 瀏覽:331
方舟搭建伺服器多少內存 發布:2024-11-03 03:33:07 瀏覽:525
android全屏代碼 發布:2024-11-03 03:30:12 瀏覽:848
鍵入憑據存儲的密碼 發布:2024-11-03 03:30:01 瀏覽:721
設置密碼字元怎麼設置 發布:2024-11-03 03:22:50 瀏覽:26
腳本戰士是什麼意思 發布:2024-11-03 03:22:39 瀏覽:872
php的mysql擴展 發布:2024-11-03 03:22:01 瀏覽:394