❶ 用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图形界面编程
//这是哪个背景类
import java.awt.Image;
import java.awt.*;
import javax.swing.*;
public class ImageBack extends JPanel{
Image im = null;
public imageback(Image im){
this.im=im;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(width,height);
this.setVisible(true);
}
public static void main(String args[]){
}
public void paintComponent(Graphics g){
//清屏
super.paintComponent(g);
g.drawImage(im, 0, 0, this.getWidth(),this.getHeight(),this);
}
}
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.*;
/**
* 学生管理系统主界面
*/
public class StuMain extends JFrame{
//背景图片
Image image=null;
public StuMain() {
try {
image = ImageIO.read(new File("images/start.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把你要的背景图片放到背景面板里
imageback im = new imageback(image);
//把有背景的图片的JPanel放到JFrame里面
this.add(im);
//添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
//设置框架的大小
//设置标题
this.setTitle("学生管理系统");
this.setVisible(true);
}
public static void main(String args[]){
new StuMain();
}
}
如果那不懂随时米我 qq896182794
❸ 求一个Java程序代码要运用图形界面
package gussNumber;
import java.awt.*;
import java.awt.event.*;
public class GussNumber extends Frame{
private static GussNumber mainFrame = new GussNumber();
private static Button yes = new Button("确定输入");
private static Button reTry = new Button("重新测试");
private static Label lb = new Label("请输入你猜测的数字(0~100):");
private static Label ts = new Label("欢迎使用......");
private static Double r_num = new Double(Math.random()*100);
private static Integer num = new Integer(r_num.intValue());
private static int t_num;
private static TextField tf = new TextField("",5);
private static String ct_img = "welcome.jpg";
public void paint(Graphics g){
Image img = getToolkit().getImage(ct_img);
g.drawImage(img,100,30,this);
}
public GussNumber(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main (String[] args) {
yes.addActionListener(new ButtonListener());
reTry.addActionListener(new ButtonListener());
System.out.println("Starting GussNumber...");
mainFrame.setBackground(Color.WHITE);
mainFrame.setSize(400,200);
mainFrame.setLayout(null);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("猜数字");
mainFrame.add(lb);
lb.setBounds(70,150,200,20);
mainFrame.add(tf);
tf.setBounds(270,150,50,20);
mainFrame.add(yes);
yes.setBounds(240,170,60,25);
mainFrame.add(ts);
ts.setBounds(70,130,200,20);
mainFrame.add(reTry);
reTry.setBounds(100,170,60,25);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==yes){
try{
t_num = Integer.parseInt(tf.getText());
}catch(NumberFormatException e){
Graphics g = mainFrame.getGraphics();
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
ct_img = "welcome.jpg";
ts.setText("请输入正常的数字!");
g.drawImage(img,100,30,mainFrame);
}
if(t_num == num){
ct_img = "right.jpg";
ts.setText("答对啦! 恭喜你~");
Image img = mainFrame.getToolkit().getImage("right.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num > num){
ct_img = "tooBig.jpg";
ts.setText("大了点哦! 继续努力~");
Image img = mainFrame.getToolkit().getImage("tooBig.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num < num){
ct_img = "tooSmall.jpg";
ts.setText("试试换个大点的数字~");
Image img = mainFrame.getToolkit().getImage("tooSmall.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
}
if(event.getSource()==reTry){
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
r_num = new Double(Math.random()*100);
num = new Integer(r_num.intValue());
tf.setText("");
}
}
}
}
记得将图片换成自己的图片;
并且布局修改得美观点。。。。
❹ 求一个简单的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图形界面设计实验,求源代码,谢谢大佬!
代码:
import java.awt.*;
import javax.swing.*;
public class W extends JFrame
{
public static void main(String[] args)
{
new W();
}
W()
{
JPanel p=new JPanel();
JButton b=new JButton("ok");
b.addActionListener(e->
{
b.setForeground(Color.BLUE);
b.setText("鼠标点击!");
//setTitle("鼠标点击!");
});
p.add(b);
add(p,BorderLayout.NORTH);
setTitle("事件测试窗口");
setSize(200,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
运行结果截图:
❻ java图形界面代码解惑
一般来说.比较常使用的是JTextArea文本框的无参数构造器进行构造
然后可以使用setText("文字")来设置文本,或者getText()方法来获得文本
JTextAreajta=newJTextArea();
jta.setText("你好啊");
Stringtxt=jta.getText();
具体例子
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);
if(f.exists()&&!f.isDirectory()){//存在该文件且不是文件夹
try{
StringBuildersb=newStringBuilder();
BufferedReaderbr=newBufferedReader(newFileReader(f));
Stringtemp;
while((temp=br.readLine())!=null){
sb.append(temp+" ");
}
br.close();
jta.setText(sb.toString());//setText方法可以给jta添加文字
}catch(Exceptione1){
e1.printStackTrace();
}
}else{
jtf.setText("路径错误.重新填写");
}
}
});
}
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;
}
}
❽ java图形界面谁能做出这样的界面只需要一个界面求源代码
用javaFX更简单。拖一拖就可以了,不过需要JavaFX Scene Builder 2.0
用NetBeans IDE创建swing项目也是拖一拖就可以了
❾ 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图形界面设计实验,求源代码!
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);
}
}
}
}