闲来无事做,不如敲代码。


这几天正好是我的专业java实训。我的选题就是连连看。


在瞻仰了前辈们的代码后发现很多的图形界面有些甚是简陋。


所以本人就对图形界面着重美化了一番。本来感觉没什么好发上来的。不过为了后来人能看到一个稍微美观点的连连看,也就发上来献丑了



废话不多说:先看一下运行效果。








javafx 美化Combobox java图形界面美化_javafx 美化Combobox




javafx 美化Combobox java图形界面美化_java_02







由于本程序着重于图形界面,算法什么也就不多做讲解。毕竟是自己人,讲了你也不定会听



以下将贴上图形界面的代码


package com.yunzhi.lianliankan;
 
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public  class Lianliankanextends JFrameimplements WindowListener,ActionListener{
 
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
JPanel panel;
JButton button;
JLabel label;
ImageIcon img;
Dimension screen;
int SCRW,SCRH;
int COLS,ROWS;
int activity;
int imgw,imgh;
public Lianliankan()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭操作方式
screen=Toolkit.getDefaultToolkit().getScreenSize();//获取显示屏幕宽高
img=new ImageIcon("src/menu.png");//加载菜单背景
SCRH=img.getIconHeight();//以菜单背景图片宽高为游戏窗口宽高
SCRW=img.getIconWidth();
this.setBounds(screen.width/2-SCRW/2,screen.height/2-SCRH/2,SCRW,SCRH);//设置游戏窗口大小并使之居中显示
this.setTitle("连连看");//为窗口设置标题
label=new JLabel(img);//用刚才的背景图片产生标签
label.setBounds(0,0,SCRW,SCRH);//设置标签为窗口大小
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));把游戏窗口的底层panel加上标签
this.setLayout(null);//设置游戏窗口布局为空,方便我们直接设置控件大小
panel=new JPanel();
panel.setLayout(null);//同理
panel.setBounds(0,SCRH/2,SCRW,SCRH/2);//面板大小为游戏下半部
buttonCreated("src/start.png","start",0);//创建开始游戏按钮
buttonCreated("src/more.png","more",1);//创建更多游戏按钮
panel.setOpaque(false);//设置面板背景透明使得底层便签背景显示出来
this.setContentPane(panel);设置面板为当前窗口的面板
this.setVisible(true);//设置窗口可见
}
public Lianliankan(int i)
{
COLS=6;
ROWS=5;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen=Toolkit.getDefaultToolkit().getScreenSize();
img=new ImageIcon("src/youxi.png");
SCRH=img.getIconHeight();
SCRW=img.getIconWidth();
this.setBounds(screen.width/2-SCRW/2,screen.height/2-SCRH/2,SCRW,SCRH);
this.setTitle("连连看");
label=new JLabel(img);
label.setBounds(0,0,SCRW,SCRH);
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
this.setLayout(null);
panel=new JPanel();
panel.setLayout(null);
panel.setBounds(SCRH/2,SCRH/2,SCRW,SCRH/2);
img=new ImageIcon("src/1.png");
imgw=img.getIconWidth()+2;
imgh=img.getIconHeight()+2;
for(int cols = 1;cols < 6;cols++)
{ 
for(int rows = 0;rows < 5;rows++ )
{
buttonCreated2("src/"+cols+".png",cols+"",cols*imgw,rows*imgh,imgw,imgh);
}
}
panel.setOpaque(false);
this.setContentPane(panel);
this.setVisible(true);
}
public void buttonCreated(String file,String command,int y)
{
img=new ImageIcon(file);//加载图片
button=new JButton(img);//用所加载图片产生按钮对象
button.setBounds(SCRW/2-img.getIconWidth()/2,SCRH/2+y*img.getIconHeight(),img.getIconWidth(),img.getIconHeight());设置按钮大小及位置
button.setContentAreaFilled(false);//设置按钮内容区域不可见,使得按钮与我们图片大小相同
button.addActionListener(this);//为按钮监添加监听
button.setActionCommand(command);//为按钮添加命令参数
panel.add(button);//将按钮添加到面板
}
public void buttonCreated2(String file,String command,int x,int y,int w,int h)
{
img=new ImageIcon(file);同上
button=new JButton(img);
button.setBounds(x,y,imgw,imgh);
button.setContentAreaFilled(false);
button.addActionListener(this);
button.setActionCommand(command);
panel.add(button);
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
button=(JButton)e.getSource();
if(button.getActionCommand().equals("start"))
{
this.dispose();
//Lianliankan lianliankan=newLianliankan(1);
Game llk = new Game();
llk.randomBuild(); 
llk.init(); 
}
if(button.getActionCommand().equals("more"))
{
this.dispose();
new Lianliankan(1);
System.out.println("没有更多游戏");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Lianliankan();
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}

上面的代码就是一个图形界面的例子,本人写程序没有写详细注释的习惯,但又考虑到既然发上来就要对大家有所帮助,所以后来又加上了注释。可以说找不到比我更啰嗦的注释了。


我有很多同学在做这个实验的时候,到网上搬运的代码图形界面往往不美观,有的甚至是数字。

但是老师要求必须是图片,所以呢他们为了加图片,改的也有些不伦不类。其实网上什么资源教程都有,只要我们有耐心去搜索,去找方法,并去实践,基本上我们的需求和目的都能达到。但是呢,毕竟呢我们是年轻人,性子急,急于就成。所以呢在这里对于我的一些同学遇到的问题进行汇总一下,希望能对遇到同样问题的同学有所帮助。

一.怎么设置背景

1.重写panel的paint方法

javafx 美化Combobox java图形界面美化_java_03

2.使用我上面代码一种

其实就是在panel里添加panel,底层panel添加可以设置图片的控件如JLabel,表层panel设置透明,这样表层照样可以添加控件而不会受底层影响。

特别说明的JFrame其实有多层panel,除ContentPane,更底层还有LayeredPane。所以我们可以让LayeredPane添加一个JLable来显示背景,而让ContentPane透明化,来添加按钮等控件。

this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

panel.setOpaque(false);

二,如何让按钮大小可调节,位置可任意控制

很多同学看到连连看后,马上就会用网格布局或是网格包布局,这样按钮大小位置调节就会让他们头痛。

其实这个可以设置按钮父布局管理器为空

panel.setLayout(null);


这样我么就可以自由调节按钮大小和位置。


三,如何让图片大小和按钮一样大小


1,你可以把网上的图片用ps(程序员必会)改成你所需要大小


2,利用以下例子改成我们所需要的大小


private ImageIcon  imgi;
private Image  img;
 
imgi=new ImageIcon("src/logo.png");
img=imgi.getImage().getScaledInstance(40, 40, 1);//将图片生成为指定大小的副本,如40,40
imgi.setImage(img);
btn1.setIcon(imgi);

3.消去按钮内容区域,这样也可以让按钮和图片大小一致

button.setContentAreaFilled(false);//设置按钮内容区域不可见,使得按钮与我们图片大小相同

消去按钮边界

button.setBorder(null);

好了就说这么多了

希望能对你们有帮助。

我们不生产代码,我们只是代码的搬运工。


源码仅供参考。