「Java设定画面」java常用设计模式及场景
本篇文章给大家谈谈Java设定画面,以及java常用设计模式及场景对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何在Eclipse中进行Java的可视化界面设计
- 2、用Java语言设计一个界面,
- 3、java添加一个欢迎页面
- 4、Java Swing 怎么自定义界面背景图片
- 5、Java图形用户界面中,怎样将一幅图设置成整个窗口界面的的背景?具体用什么类的什么方法实现?
- 6、java全是代码,怎么会做出非常漂亮的画面
如何在Eclipse中进行Java的可视化界面设计
准备工作:
1、下载JDK;
2、下载Eclipse;
3、下载相应的EMF、GEF、VE;
注意:三个插件的版本要和Eclipse的版本配套才能正确使用,至于配套的说明请参考下载页面的说明
开始安装:
1、安装JDK;
这个比较容易,标准的Windows安装程序,下一步,再下一步即可,安装完成后,也不需要重启。
2、安装Eclipse;
Eclipse是绿色软件,不需要安装,只需要解压缩,然后即可运行。
为了说明方便,我把它解压缩到C盘根目录下,得到C:\eclipse目录,运行C:\eclipse\eclipse.exe即可。
注意:下面的安装,需要先关闭eclipse程序。
3、安装EMF、GEF、VE;
对于Eclipse来说,这些都是它的插件,所以,安装方法都是一样的。插件下载地址
A、在C:\eclipse目录下,建立四个子目录:C:\eclipse\emf、C:\eclipse\gef、C:\eclipse\ve、C:\eclipse\links;
B、把下载的EMF、GEF、VE都解压缩到相应的目录中,即:把EMF压缩包解压缩到C:\eclipse\emf中,得到C:\eclipse\emf\eclipse目录,以此类推,完成GEF、VE的解压缩;
C、在C:\eclipse\links目录下,新建一个文本文件,名字可随便取,如:link.txt。
然后在link.txt文件中,加入以下三行文字:
path=emf
path=gef
path=VE
注意:这里用的是相对路径,也可以把EMF、GEF、VE解压缩到其它地方,再用绝对路径即可。
再注意:绝对路径是用 “/” 或者 “\\” 分隔,而不是“\”
补充知识:为什么ve需要emf和gef:
Visual Editor 所利用的最为明显的工具是 GEF,即图形化编辑框架(Graphical Editing
Framework)。GEF 建立于本地 Eclipse 图形化工具集 SWT
之上,以使得开发一个图形化编辑器或者所见即所得文本编辑器更为容易。如果您熟悉 SWT (或者
AWT/Swing,在这方面它们是类似的)中的图形原语,那么您会知道绘制和处理任意的形状(比如矩形、箭头和椭圆)是比较困难的,更不用说管理它们之间的关系以及它们所代表的数据模型了。
GEF 被划分为两个部分:第一部分是 Draw2D 插件,这是一个轻量级的绘图和呈现包,用于帮助您绘制图形。第二部分是 GEF 插件,除了其他工具以外,这一部分中还增加了选择和创建工具、工具选项板,以及用于在数据模型和视图之间进行映射的控制器框架。
GEF
是一个模型无关的框架,但是作为 Visual Editor (以及其他生成代码的图形化工具)的一部分,它在后台使用 Eclipse
建模框架(Eclipse Modeling Framework, EMF),以在模型、Java 类和图形化表示之间进行映射,其中模型是使用
XML 元数据交换(XML Metadata Interchange, XMI)在内部存储的。EMF
的重要特性之一是它确保所有这些映射都是一对一的;所以尽管 XMI
可以被认为是模型的标准表示,但是在代码和图形之间来回切换并不会丢失任何信息。这就是为什么 Visual Editor
只需要保存模型的一种表示(即 Java 源代码),以及开发人员可以自由地在图形化编辑器之外编辑该源代码的原因。
4、安装完毕,再次运行Eclipse,它启动时会自动查找links目录下所有的文本文件,并加载其中path指向的所有插件,这里当然包括VE。
A、如果不能确定是否已经加载,可选择菜单Help-Software Updates-Manage Configuration,打开Product Configuration窗体,在其左边的树形栏里,可以看到已加载的插件。
B、如果加载没有成功,可以试试用Eclipse -clear来运行程序。
C、如果加载仍没有成功,可以试试删除C:\eclipse\configuration下的内容,仅保留config.ini文件。然后再用Eclipse来运行程序。
================================================================
启动eclipse平台。刚开始,你可能觉得eclipse并没有什么变化。先别急,请新建一个项目,点击"文件"菜单下的"新建"子菜单,选择并建立一个新的"java项目"。然后在工具栏上的"新建Java类" 图标上,点击右边的小箭头,将展开如下图的菜单:
在此菜单上,多了一个"Visual Calss"的子菜单,这就是进入Visual Editor的入口之一。点击"Visual Class",就会弹出创建GUI程序的对话框,选择完毕后,就会进入可视化编辑画面:
用VE写个Hello:
1、新建一个项目;
File-New-Project...,选择“Java Project”,然后Next,输入一个项目名字:Hello,其它全部默认,最后回车,或者点击“Finish”完成。
2、设置SWT库;
A、选择项目Hello,右键菜单,打开Properties对话框。
B、选择左边树形中的Java Build Path,在其设置页中,选择Libraries页,点击“Add Library...”;
C、在打开的Add Library对话框里,选择Standard Widget Toolkit(SWT),Next;
D、在打开的SWT Library Options对话框中,勾选上“Include support for JFace library”,Finish即可。
E、回到Properties对话框,OK即可。
3、新建Hellworld.java文件;
A、选择项目Hello,右键菜单,New-Other...(或者点击工具栏里新建图标),打开Select a wizard对话框,选择Java/Visual Class,Next;
B、在打开的Java Visual Class对话框中,
在Package里,输入:com.cnblogs.pan;
在Name里,输入:Helloworld;
在Style里,选择SWT/Shell;
勾选上public static void main(String[] args);
最后Finish。
4、到了这一步,应该就可以看到窗体了,那些控件都在Palette里,点开就能看到了。
A、在窗体上右击,选择Set Layout,再选择null;(此处为个人习惯)
B、从Palette里,选中一个Button,并在窗体任意位置画一个Button,在下面的Properties窗口里,设置其text为ClickMe;
C、选中该按钮,右击选择Events-Add Event,在打开的对话框中,选择左边的widgetSelected,Finish。
D、这时,光标会自动定位到程序相应的位置,我们在程序里加入下面语句:
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
System.out.println("widgetSelected()");
MessageDialog.openInformation(null, "Hello", "Hello World!");//加入这条语句
}
E、保存程序,点击菜单Run-Run As-Java Application,呵呵,跟全世界打个招呼吧!
用Java语言设计一个界面,
首先:采用什么技术实现
java语言可以使用awt 和swing等技术实现图形界面
推荐使用Swing,因为Swing比AWT更专业,更漂亮,组件更丰富,功能更强大。
2. 其次:分析采用什么布局
边界布局BorderLayout,配合表格布局GridLayout,既简单又美观
3. 最后:分析需求中需要用的组件
学生姓名 学号 显示信息 需要用到文本框JTextField
单选按钮 需要用到组件 JRadioButton
复选框 需要用到组件 JCheckBox
组合框 需要用到组件 JComboBox
图片效果
参考代码如下
//导入所需要的包
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.*;
public class ClassFrame extends JFrame {// 写一个类继承自JFrame 窗体
// 定义组件
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfName, tfNum, allInfo;
private JRadioButton rb1, rb2;
private JCheckBox cb1, cb2, cb3;
private JComboBoxString t1, t2, t3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClassFrame frame = new ClassFrame();// 创建一个窗口实例
frame.setVisible(true);// 让该窗口实例可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 窗口属性的设置,内部组件的初始化
*/
public ClassFrame() {
setTitle("选课ing...");//标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭是退出JVM
setSize(450, 339);// 设置窗体大小
setLocationRelativeTo(null);// 窗体居中
contentPane = new JPanel();// 内容面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));// 设置布局
setContentPane(contentPane);
JPanel panel = new JPanel(new GridLayout(5, 1, 5, 10));//5行1列的表格布局
panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
contentPane.add(panel, BorderLayout.CENTER);//给panel添加边框
JPanel panel_1 = new JPanel();
panel.add(panel_1);
JLabel label = new JLabel("姓名");
panel_1.add(label);
tfName = new JTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabel label_2 = new JLabel("学号");
panel_1.add(label_2);
tfNum = new JTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1 = new JRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//设置单选按钮中,默认选择的按钮
rb2 = new JRadioButton("女");
panel_1.add(rb2);
ButtonGroup bts = new ButtonGroup();//单选按钮需要加入同一个ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanel panel_2 = new JPanel();
panel.add(panel_2);
cb1 = new JCheckBox("高等数学");
panel_2.add(cb1);
t1 = new JComboBoxString();
t1.setModel(new DefaultComboBoxModelString(new String[] { "林老师", "赵老师", "孙老师" }));
panel_2.add(t1);
JPanel panel_3 = new JPanel();
panel.add(panel_3);
cb2 = new JCheckBox("世界经济");
panel_3.add(cb2);
t2 = new JComboBoxString();
t2.setModel(new DefaultComboBoxModelString(new String[] { "张老师", "刘老师" }));
panel_3.add(t2);
JPanel panel_4 = new JPanel();
panel.add(panel_4);
cb3 = new JCheckBox("音乐赏析");
panel_4.add(cb3);
t3 = new JComboBoxString();
t3.setModel(new DefaultComboBoxModelString(new String[] { "王老师", "周老师" }));
panel_4.add(t3);
JPanel panel_5 = new JPanel();
panel.add(panel_5);
JButton jbOk = new JButton("确定");
panel_5.add(jbOk);
JButton jbRest = new JButton("重填");
panel_5.add(jbRest);
JPanel panelSouth = new JPanel();
contentPane.add(panelSouth, BorderLayout.SOUTH);
JLabel labe = new JLabel("选课信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo = new JTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanel panelNorth = new JPanel();
contentPane.add(panelNorth, BorderLayout.NORTH);
JLabel labelTitle = new JLabel("学生选课界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(new Font("宋体", Font.BOLD, 20));
panelNorth.add(labelTitle);
//给确定按钮添加事件处理代码
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuilder info = new StringBuilder();
String name = tfName.getText();
String num = tfNum.getText();
String sex;
if (rb1.isSelected()) {
sex = "男";
} else {
sex = "女";
}
info.append(name + num + sex);
if (cb1.isSelected()) {
String c = cb1.getText();
String t = t1.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb2.isSelected()) {
String c = cb2.getText();
String t = t2.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb3.isSelected()) {
String c = cb3.getText();
String t = t3.getSelectedItem().toString();
info.append(" " + c + t);
}
allInfo.setText(info.toString());//把学生信息和选课信息放到文本框
}
});
//给重填按钮 设置事件处理代码
jbRest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}
java添加一个欢迎页面
一本电子书上说的,你可以试下
1、新建一个启动画面Window类
java.awt.Window windowSplash;
2、调用prepareSplash()函数,初始化启动界面
private void prepareSplash()
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
windowSplash = new Window( this );
Image image = toolkit.getImage( "images" + File.separator + "splash.gif" );
ImageCanvas canvas = new ImageCanvas( image );
windowSplash.add( canvas, "Center" );
Dimension scmSize = toolkit.getScreenSize();
int imgWidth = image.getWidth( this );
int imgHeight = image.getHeight( this );
windowSplash.setLocation( scmSize.width/2 - (imgWidth/2), scmSize.height/2 - (imgHeight/2) );
windowSplash.setSize( imgWidth, imgHeight );
}
3、在Application的JFrame类(主界面)中调用startSplash(),显示启动界面,然后初试化JFrame的各个可视化组件,初始化后台数据库等(如数据
库的连接)
private void startSplash()
{
windowSplash.setVisible( true );
windowSplash.toFront();
}
4、在所有的初始化工作完成之后,调用stopSplash()函数,停止显示启动画面
private void stopSplash()
{
windowSplash.dispose();
}
Java Swing 怎么自定义界面背景图片
在java swing中需要为容器添加自定义图片或者背景图片。提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel,代码如下:
1、JPanel(源代码)
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景图片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jframe.setSize(icon.getIconWidth(), icon.getIconHeight());
}
};
jframe.setTitle("测试背景图片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Drawing();
}
}
2、JLabel源代码
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** 利用JLabel来构建图片 */
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 1366, 768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366, 768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
public static void main(String args[]) {
new Drawing2();
}
}
添加控件:
jlpic.setIcon(icon);
Container c = new Container();
JLabel user = new JLabel("用户:");
JLabel password = new JLabel("密码:");
JTextField txf_userName = new JTextField();
JPasswordField pwd_password = new JPasswordField()
{
public void paste(){
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
};
user.setBounds(200,25,50,25);
password.setBounds(200,52,50,25);
txf_userName.setBounds(300,25,150,25);
pwd_password.setBounds(300,52,150,25);
c.add(user);
c.add(txf_userName);
c.add(password);
c.add(pwd_password);
c.add(jlpic);
jframe.setSize(1366, 768);
// jframe.add();
jframe.add(c);
Java图形用户界面中,怎样将一幅图设置成整个窗口界面的的背景?具体用什么类的什么方法实现?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.net.*;
/**
* 人事管理系统主界面
*/
public class HrMain extends JFrame implements ActionListener,TreeSelectionListener{
//框架的大小
Dimension faceSize = new Dimension(650, 450);
//程序图标
Image buaa;
//建立Jtree菜单
JTree tree;
DefaultMutableTreeNode root; //人事管理系统
DefaultMutableTreeNode node1; //人员基本信息维护
DefaultMutableTreeNode node2; //部门信息管理
DefaultMutableTreeNode node3; //人员调动管理
DefaultMutableTreeNode node4; //人员考核管理
DefaultMutableTreeNode node5; //劳资管理
DefaultMutableTreeNode leafnode;
TreePath treePath;
//主界面面板
public static JSplitPane splitPane;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JLabel welcome = new JLabel();
JScrollPane scrollPane;
/**
* 程序初始化函数
*/
public HrMain() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
//设置框架的大小
this.setSize(faceSize);
//设置标题
this.setTitle("人事管理系统");
//程序图标
buaa = getImage("buaa.jpg");
this.setIconImage(buaa); //设置程序图标
//设置自定义大小
this.setResizable(false);
try {
Init();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* 程序初始化函数
*/
private void Init() throws Exception {
//Container contentPane = this.getContentPane();
//contentPane.setLayout(new BorderLayout());
//添加Jtree菜单
root = new DefaultMutableTreeNode("人事管理系统");
node1 = new DefaultMutableTreeNode("基本信息管理");
node2 = new DefaultMutableTreeNode("人员调动管理");
node3 = new DefaultMutableTreeNode("人员考核管理");
node4 = new DefaultMutableTreeNode("劳资管理");
//人员基本信息
root.add(node1);
leafnode = new DefaultMutableTreeNode("添加人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("修改人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("删除人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("查询人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("部门管理");
node1.add(leafnode);
//人员调动管理
root.add(node2);
leafnode = new DefaultMutableTreeNode("人员调动");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("调动历史查询");
node2.add(leafnode);
//人员考核管理
root.add(node3);
leafnode = new DefaultMutableTreeNode("人员考核");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("考核历史查询");
node3.add(leafnode);
//劳资管理
root.add(node4);
leafnode = new DefaultMutableTreeNode("劳资分配管理");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("劳资历史查询");
node4.add(leafnode);
//生成左侧的JTree
tree = new JTree(root);
scrollPane = new JScrollPane(tree);
scrollPane.setPreferredSize(new Dimension(150,400));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//生成JPanel
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.add(scrollPane);
welcome.setText("欢迎使用人事管理系统");
welcome.setFont(new Font("Dialog",0,16));
panel3.add(welcome);
//生成JSplitPane并设置参数
splitPane = new JSplitPane();
splitPane.setOneTouchExpandable(false);
splitPane.setContinuousLayout(true);
splitPane.setPreferredSize(new Dimension(150, 400));
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(panel1);
splitPane.setRightComponent(panel3);
splitPane.setDividerSize(2);
splitPane.setDividerLocation(161);
//生成主界面
this.setContentPane(splitPane);
this.setVisible(true);
//添加事件侦听
tree.addTreeSelectionListener(this);
//关闭程序时的操作
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e) {
}
/**
* JTree事件处理
*/
public void valueChanged(TreeSelectionEvent tse) {
DefaultMutableTreeNode dnode =
(DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
System.out.println("dnode="+dnode);
String node_str = dnode.toString();
if (node_str == "人事管理系统") {
splitPane.setRightComponent(panel3);
}
//人员基本信息管理树
else if (node_str == "基本信息管理") {
//当选中后展开或关闭叶子节点
treePath = new TreePath(node1.getPath());
if(tree.isExpanded(treePath))
tree.collapsePath(treePath);
else
tree.expandPath(treePath);
}
else if (node_str == "添加人员信息") {
Node11Panel node11Panel = new Node11Panel();
splitPane.setRightComponent(node11Panel);
}
else if (node_str == "修改人员信息") {
Node12Panel node12Panel = new Node12Panel();
splitPane.setRightComponent(node12Panel);
}
else if (node_str == "删除人员信息") {
Node13Panel node13Panel = new Node13Panel();
splitPane.setRightComponent(node13Panel);
}
else if (node_str == "查询人员信息") {
Node14Panel node14Panel = new Node14Panel();
splitPane.setRightComponent(node14Panel);
}
else if (node_str == "部门管理") {
Node15Panel node15Panel = new Node15Panel();
splitPane.setRightComponent(node15Panel);
}
//人员调动管理树
else if (node_str == "人员调动管理") {
//当选中后展开或关闭叶子节点
treePath = new TreePath(node2.getPath());
if(tree.isExpanded(treePath))
tree.collapsePath(treePath);
else
tree.expandPath(treePath);
}
else if (node_str == "人员调动") {
Node21Panel node21Panel = new Node21Panel();
splitPane.setRightComponent(node21Panel);
}
else if (node_str == "调动历史查询") {
Node22Panel node22Panel = new Node22Panel();
splitPane.setRightComponent(node22Panel);
}
//人员考核管理树
else if (node_str == "人员考核管理") {
//当选中后展开或关闭叶子节点
treePath = new TreePath(node3.getPath());
if(tree.isExpanded(treePath))
tree.collapsePath(treePath);
else
tree.expandPath(treePath);
}
else if (node_str == "人员考核") {
Node31Panel node31Panel = new Node31Panel();
splitPane.setRightComponent(node31Panel);
}
else if (node_str == "考核历史查询") {
Node32Panel node32Panel = new Node32Panel();
splitPane.setRightComponent(node32Panel);
}
//劳资管理树
else if (node_str == "劳资管理") {
//当选中后展开或关闭叶子节点
treePath = new TreePath(node4.getPath());
if(tree.isExpanded(treePath))
tree.collapsePath(treePath);
else
tree.expandPath(treePath);
}
else if (node_str == "劳资分配管理") {
Node41Panel node41Panel = new Node41Panel();
splitPane.setRightComponent(node41Panel);
}
else if (node_str == "劳资历史查询") {
Node42Panel nod42Panel = new Node42Panel();
splitPane.setRightComponent(nod42Panel);
}
}
/**
* 通过给定的文件名获得图像
*/
Image getImage(String filename) {
URLClassLoader urlLoader = (URLClassLoader)this.getClass().
getClassLoader();
URL url = null;
Image image = null;
url = urlLoader.findResource(filename);
image = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mediatracker = new MediaTracker(this);
try {
mediatracker.addImage(image, 0);
mediatracker.waitForID(0);
}
catch (InterruptedException _ex) {
image = null;
}
if (mediatracker.isErrorID(0)) {
image = null;
}
return image;
}
}
java全是代码,怎么会做出非常漂亮的画面
唉,给你看看小Case的,自己运行一下
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TranslucentButton extends JButton {
BufferedImage buttonImage = null;
/** Creates a new instance of TranslucentButton */
public TranslucentButton(String label) {
super(label);
setOpaque(false);
}
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
if (buttonImage == null || buttonImage.getWidth() != getWidth() ||
buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());//返回一个数据布局和颜色模型与此 GraphicsConfiguration 兼容的 BufferedImage
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());//设置获取的当前的剪贴区域
// Have the superclass render the button for us
super.paint(gButton);
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D)g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
}
private static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
JPanel checkerboard = new Checkerboard();
checkerboard.add(new TranslucentButton("Translucent Button"));
f.add(checkerboard);
f.setVisible(true);
}
public static void main(String args[]) {
Runnable doCreateAndShowGUI = new Runnable() {
public void run() {
createAndShowGUI();
}
};
SwingUtilities.invokeLater(doCreateAndShowGUI);//swing线程
}
private static class Checkerboard extends JPanel {
static final int CHECKER_SIZE = 60;
public void paintComponent(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
for (int stripeX = 0; stripeX getWidth(); stripeX += CHECKER_SIZE) {
for (int y = 0, row = 0; y getHeight(); y += CHECKER_SIZE/2, ++row) {
int x = (row % 2 == 0) ? stripeX : (stripeX + CHECKER_SIZE/2);
g.fillRect(x, y, CHECKER_SIZE/2, CHECKER_SIZE/2);
}
}
}
}
}
关于Java设定画面和java常用设计模式及场景的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。