Tag: mouselistener

最后的局部variables不能被赋值

我有一个arrays的席位,数组有两个string(select和空)。 点击鼠标,我想遍历数组并find所选的座位。 当我按下button说: 最后的局部variablesseatno不能被赋值,因为它是在封闭types中定义的。 JButton btnContinue = new JButton("Next"); btnContinue.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { for(int x=0;x<17;x++){ if(anArray[x]=="selected"){ seatno = anArray[x]; } } data page=new data(newfrom,newto,newtime,date2,seatno); page.setVisible(true); setVisible(false); } }); btnContinue.setBounds(358, 227, 62, 23); contentPane.add(btnContinue);

更改JPanelgraphics颜色绘制线

我有一个类似于油漆的程序。 而我试图实现改变笔的颜色,但是当我改变颜色,当前绘制的所有东西都改为颜色红色,例如在我的程序中,我怎样才能使它不会重绘当前绘制到当前改变颜色?下面的代码将编译并运行 JPanel绘图区的类 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.JPanel; //refer to http://jkost.ergoway.gr/jnkjavaconnection/freedraw.html for the algorithm. public class STDrawingArea extends JPanel { /** * */ private static final long serialVersionUID = 1L; ArrayList<Rectangle> dPoint = new ArrayList<Rectangle>(); Point point = new Point(-1,-1); […]

Java swing JComponent“大小”

我正在做一个项目,我需要一些自定义的挥杆组件。 到目前为止,我已经做了一个新的button与一系列的图像(Java的金属外观根本不适合我的用户界面)。 我实现了这个新的组件的MouseListener ,这是我的问题出现的地方。 我的小部件更改hover图像,单击等,除了我的MouseListener拿起鼠标进入整个GridLayout容器,而不是进入图像。 所以我有一个大约200 * 100的图像和周围的容器大约400 * 200和mouseEntered方法是当它进入GridLayout部分(甚至它的空白部分),而不是在图像上触发。 我怎么能这样做,只有当我hover在图像上才被解雇? 我试图设置大小和范围和其他属性无济于事。 编辑:这是我的问题的演示。 正如你所看到的(颜色非常相似),右下angular的button只要inputGridlLayout部分GridlLayout 。 我只想要它突出显示当我超过图像实际,而不是GridLayout部分。 我不会添加MouseListener方法,因为它们只涉及切换显示的图像。 public customWidget() { this.setLayout(new FlowLayout()); try { imageDef=ImageIO.read(new File("/home/x101/Desktop/buttonDef.png")); imageClick=ImageIO.read(new File("/home/x101/Desktop/buttonClick.png")); imageHover=ImageIO.read(new File("/home/x101/Desktop/buttonHover.png")); current=imageDef; } catch (IOException e) { e.printStackTrace(); } this.addMouseListener(this); } protected void paintComponent(Graphics g) { super.paintComponents(g); g.drawImage(current, 0, 0, current.getWidth(), current.getHeight(), null); } 编辑:添加代码部分