如何创建专业定制的Java Swing Gui? [英] How to create a professional customized Java Swing Gui?

查看:144
本文介绍了如何创建专业定制的Java Swing Gui?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近被赋予完全修改我公司的一个软件Gui外观的任务。
直到现在,我只做了基本的外观变化。主要是通过一些GridBagConstraints处理将Gui从默认的Metal L& F升级到System L& F。
但是这个任务是不同的:



我们有一位图形设计师提请设计:



我知道如何创建这种功能

STRONG>。我可能会使用双JTabbedPanes或只是添加Jpanels。



我的问题是如何将图形与gui对象合并。
为了使Java Swing应用程序看起来像这样 - 使用四舍五入的制表符和合并选项卡面板颜色,我应该请设计师给我什么?
我如何让JPanels或JTabbedPanes穿上这件华丽的服装,当我现在所有的东西都是上面这幅画?

解决方案

大多数自定义 Swing GUI 都是使用带ImageIcons的JLabel和自定义处理代码制作的。例如:

  public class CustomGUI extends JFrame {

private JLabel frame,border,grip,exit , 最小化;
私人点初始点击;

public CustomGUI(){
initComponents();


private void initComponents(){
setTitle(CustomGUI);
setMinimumSize(new Dimension(640,480));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);
setBackground(new Color(0,0,0,0));
setLayout(null);
pack();

frame = new JLabel();
frame.setBounds(0,0,640,480);
frame.setIcon(new ImageIcon(getClass()。getResource(frame.png));

border = new JLabel();
border.setBounds(0, 0,640,480);
border.setIcon(new ImageIcon(getClass()。getResource(border.png));

grip = new JLabel();
grip.setBounds(215,0,376,25);
grip.setIcon(new ImageIcon(getClass()。getResource(grip.png));
grip.addMouseListener(new MouseAdapter( ){
@Override
public void mousePressed(MouseEvent evt){
initialClick = evt.getPoint();
}
});
grip。 addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseDragged(MouseEvent evt){
int thisX = getLocation()。x;
int thisY = getLocation()。y ;
int xMoved =(thisX + evt.getX()) - (thisX + initialClick.x);
int yMoved =(thisY + evt.getY()) - (thisY + initialClick.y) ;
int posX = thisX + xMoved;
int posY = thisY + yMoved;
setLocation(posX,posY);
}
});

exit = new JLabel();
exit.setBounds(620,4,32,16);
exit.setIcon(new ImageIcon(getClass()。getResource(exit.png));
exit.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
dispatchEvent(new WindowEvent(new UI(),WindowEvent.WINDOW_CLOSING));
}
@Override
public void mouseEntered(MouseEvent evt){
exit.setIcon(new ImageIcon(getClass()。getResource(exitGlow.png));
}
@Override
public void mouseExited(MouseEvent evt){
exit.setIcon(new ImageIcon(getClass()。getResource(exit.png));
}
});

minimize = new JLabel();
minimize.setBounds(600,4,16,16);
minmize.setIcon(new ImageIcon(getClass()。getResource(minimize.png));
minimize.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
setState(Fr ame.ICONIFIED);
}
@Override
public void mouseEntered(MouseEvent evt){
minimize.setIcon(new ImageIcon(getClass()。getResource(minimizeGlow.png));

@Override
public void mouseExited(MouseEvent evt){
minimize.setIcon(new ImageIcon(getClass()。getResource(minimize.png));
} b


$ b $ add(exit);
add(exit);
add(grip);
add(border);


$ b public static void main(String [] args){
EventQueue.invokeLater(() - > {
new CustomGUI()。setVisible(true);
});
}
}

这段代码创建了一个自定义的图形用户界面,其中包含一个自定义的框架,边框和一个用于拖动窗口的手柄,包括退出和最小化鼠标进入时发光的按钮,使它们看起来非常专业。需要成为项目的根源)如果要创建自定义选项卡式窗格,请使用 ImageIcons 创建 JLabels 你希望它们看起来像,每个都带有相应的文本,并添加处理,通过改变颜色或在其周围添加边框,使其看起来被选中,并将相应的面板放在前面,隐藏所有其他面板。 / p>

或者,您可以创建一个自定义的外观和感觉,但这很难做到,并且不给你使用 JLabels的自由 with ImageIcons does。



您也可以使用 JavaFX ,一个丰富的GUI平台,旨在取代Swing,但迄今为止正如Swing所说,上述代码是Swing应用程序的最佳选择n。

I was recently given with the task of completely revising one of my company's software Gui appearance. Up until now I only done basic look-n-feel changes. Mostly upgrading the Gui from the default Metal L&F to System L&F with some GridBagConstraints handling. But this task is something different:

We have a graphic designer who drew the desired design:

I know how to create this kind of functionality. I might use a double JTabbedPanes or just add Jpanels all around.

My problem is how to incorporate the graphics with gui objects. What should I ask the designer to give me in order to make a Java Swing application that looks like this - with the rounded tabs and the merging of tab-panel colors? How can I make JPanels or a JTabbedPanes wear this fancy costume when all I am given right now is this drawing above?

解决方案

Most custom Swing GUIs are made using JLabels with ImageIcons and custom handling code. For example:

public class CustomGUI extends JFrame {

private JLabel frame, border, grip, exit, minimize;
private Point initialClick;

public CustomGUI() {
    initComponents();
}

private void initComponents() {
    setTitle("CustomGUI");
    setMinimumSize(new Dimension(640, 480));
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setUndecorated(true);
    setBackground(new Color(0, 0, 0, 0));
    setLayout(null);
    pack();

    frame = new JLabel();
    frame.setBounds(0, 0, 640, 480);
    frame.setIcon(new ImageIcon(getClass().getResource("frame.png"));

    border = new JLabel();
    border.setBounds(0, 0, 640, 480);
    border.setIcon(new ImageIcon(getClass().getResource("border.png"));

    grip = new JLabel();
    grip.setBounds(215, 0, 376, 25);
    grip.setIcon(new ImageIcon(getClass().getResource("grip.png"));
    grip.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent evt) {
            initialClick = evt.getPoint();
        }
    });
    grip.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent evt) {
            int thisX = getLocation().x;
            int thisY = getLocation().y;
            int xMoved = (thisX + evt.getX()) - (thisX + initialClick.x);
            int yMoved = (thisY + evt.getY()) - (thisY + initialClick.y);
            int posX = thisX + xMoved;
            int posY = thisY + yMoved;
            setLocation(posX, posY);
        }
    });

    exit = new JLabel();
    exit.setBounds(620, 4, 32, 16);
    exit.setIcon(new ImageIcon(getClass().getResource("exit.png"));
    exit.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt) {
            dispatchEvent(new WindowEvent(new UI(), WindowEvent.WINDOW_CLOSING));
        }
        @Override
        public void mouseEntered(MouseEvent evt) {
            exit.setIcon(new ImageIcon(getClass().getResource("exitGlow.png"));
        }
        @Override
        public void mouseExited(MouseEvent evt) {
            exit.setIcon(new ImageIcon(getClass().getResource("exit.png"));
        }
    });

    minimize = new JLabel();
    minimize.setBounds(600, 4, 16, 16);
    minmize.setIcon(new ImageIcon(getClass().getResource("minimize.png"));
    minimize.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt) {
            setState(Frame.ICONIFIED);
        }
        @Override
        public void mouseEntered(MouseEvent evt) {
            minimize.setIcon(new ImageIcon(getClass().getResource("minimizeGlow.png"));
        }
        @Override
        public void mouseExited(MouseEvent evt) {
            minimize.setIcon(new ImageIcon(getClass().getResource("minimize.png"));
        }
    });

    add(minimize);
    add(exit);
    add(grip);
    add(border);
    add(frame);
}

public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        new CustomGUI().setVisible(true);
    });
}
}

This code creates a custom GUI with a custom frame, border, and a grip for dragging the window, including exit and minimize buttons that glow when your mouse enters them, making them look proffesional. (The images for each component need to be in the root of the project for this to work.) If you want to create custom tabbed panes, create JLabels with ImageIcons that look like rounded tabs or anything that you want them to look like, each with its corresponding text on it, and add handling that will make it look selected by changing its color or adding a border around it and bring its corresponding panel to the front, hiding all other panels.

Alternatively, you can create a custom Look and Feel, but this is harder to do and does not give you the freedom that using JLabels with ImageIcons does.

You can also use JavaFX, a rich GUI platform that was designed to replace Swing, but as far as Swing goes, the above code is your best bet for a Swing application.

这篇关于如何创建专业定制的Java Swing Gui?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆