java的选择项目 [英] Java select items

查看:110
本文介绍了java的选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始我用java的冒险。我已经写了面板和三个按钮的应用程序。但我有一个问题。我想用鼠标选择添加按钮这个。我的意思是像我们在Windows桌面上。我preSS鼠标左键并用鼠标区域选择越来越大的运动。我有一个问题。是否有一个特定的接口在这还是我有它manualy,请致电事件侦听器适当的方法有画矩形透明?这里有一个画面:

I just began my adventure with java. I have written an application with a panel and three buttons. But I have a problem. I want to add selection this buttons using the mouse. I mean like we have in Windows on the Desktop. I press the left mouse button and with the movement of the mouse the area selection is growing. And I have a question. Is there a specific interface in this or do I have it manualy, call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:

我做出了一些进展,但我有一个问题。一切都OK了,但是当我画的矩形,按钮被重新绘制。我想这个按钮时,我画的矩形不disapear。这是code:

I make a some progress, but i have another problem. Everything is ok but when I paint rectangle, button is repainting. I want to this button don't disapear when i paint rectangle. This is code:

    import java.awt.AlphaComposite;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class zaznacz extends JPanel implements MouseListener, MouseMotionListener
{


     Rectangle newLine=null;
     public zaznacz() 
     {

            addMouseListener(this);
            addMouseMotionListener(this);

        }


        static class Rectangle {

            int x1,y1,x2,y2;

            Rectangle(int _x1, int _y1,int _x2, int _y2){

                x1=_x1;y1=_y1;x2=_x2;y2=_y2;
            }



             void paint(Graphics g)
             {



                 g.drawRect(x1, y1, x2, y2);


            }

        }
        public void mouseClicked(MouseEvent e) {

                }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mousePressed(MouseEvent e) {
            startPoint=e.getPoint();
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();

            Rectangle2D prostokat = new Rectangle2D.Double();
            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);


        }
        @Override
        public void mouseReleased(MouseEvent e) 
        {
            repaint();

        }
        Point startPoint;


        @Override
        public void mouseDragged(MouseEvent e) {
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();
            Rectangle2D prostokat = new Rectangle2D.Double();

            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);
            paintComponent(g2);


        }
        @Override
        public void mouseMoved(MouseEvent e) {


        }
        int rule = AlphaComposite.SRC_OVER;
        float alpha = 0.85F;




    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable()
        {
public void run()
            {
                zaznacz rys = new zaznacz();
                JFrame frame = new JFrame();
                JButton Button = new JButton("1");
                JPanel panel = new JPanel();



                panel.add(Button);
                rys.add(panel);
                frame.setSize(400,300);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                panel.setOpaque(false);

                frame.add(rys);


            }
        });
    }

}

有人能帮帮我吗?

Somebody can help me?

推荐答案

编辑:如果你指的是高亮显示的矩形,比如当你在桌面上拖动鼠标时出现的一个,那么你的办法当然是有效的可行的;我不相信那些在Swing任何功能,能够这个功能的实现。我不知道 JDesktop中是否支持这一点,但它可能是值得一试。

If you are referring to the highlighted rectangle, such as the one that appears as you drag your mouse over the desktop, then your approach would certainly be valid and feasible; I do not believe that are any features in Swing which provide an implementation of this feature. I don't know whether JDesktop supports this, but it may be worth a look.

编辑2:也看看玻璃面板。这将使它更方便的实现你的描述不是在同一窗格会。

Edit 2: Also take a look at GlassPane. It would make it much more convenient to implement what you describe than doing everything on the same pane would.

如果我正确地理解你的问题,当按钮被鼠标悬停或pressed这随之而来的影响是由外观(; F左&AMP)控制。您可以选择的外观和感觉,你会那些可以在主机操作系统上使用 javax.swing.UIManager中的类之间的喜欢。编写自己的外观和感觉是一个平凡的任务,通常需要几个星期的时间,即使是谁在使用图像编辑应用程序中经验丰富,知道如何编程的图形用户界面的人。

If I understand your question correctly, the effects which ensue when the button is being hovered over or pressed are controlled by the Look and Feel (L&F). You can select the Look and Feel you would like among those that are available on the host operating system by using the javax.swing.UIManager class. Writing your own Look and Feel is a nontrivial task and usually takes several weeks of time, even for a person who is experienced in using image-editing applications and knows how to program graphical user-interfaces.

下面是一个例子(从此链接),这表明你如何改变L&放大器; F到Nimbus的:

Here's an example (from this link) which shows you how to change the L&F to Nimbus:

try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        break;
    }
}
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

有许多人可用,但它承担指出,许多开发人员(用户)preFER使用系统默认的外观和感觉,而不是这是默认的Java Swing组件。

There are many others available, but it bears noting that many developers (and users) prefer to use the system's default look and feel rather than that which is default for Java Swing components.

这篇关于java的选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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