导致问题的java Mouslistener
我已经在我的代码中创建了一个GoInvisible类,它实现了一个鼠标侦听器,我正在尝试使用鼠标按下和鼠标释放的方法使我的框架变透明,然后在按下和释放框架上的按钮后恢复正常。我在一个内部类中调用这些方法,该类实现了一个处理按钮事件的操作侦听器,但由于某种原因,当我运行应用程序时,框架从未显示
这是帧代码
public class FNAFrame extends JFrame {
public FNAFrame()
{
super ("FNA Comments Generator");
setLayout(new BorderLayout());
setResizable(false);
TextFrame comps = new TextFrame();
add(comps);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
//
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
new FNAFrame();
}
});
}
} // end of class FNA Frame
这里是components类
public class TextFrame extends JPanel
{
private JButton Go_Shadow;
public TextFrame()
{
super(new GridBagLayout());
setPreferredSize(new Dimension(300,200));
setBackground(Color.white);
init();
} // end of class constructor
private void init()
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
// button to display date in textarea
Go_Shadow = new JButton("Shadow");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
add(Go_Shadow, gbc);
// adding listeners to components
// registering all components with their respective listeners
CompHandler compHandler = new CompHandler();
Go_Shadow.addActionListener(compHandler);
}
// class to handle text fields
private class CompHandler implements ActionListener
{
private MouseEvent me;
@Override
public void actionPerformed(ActionEvent e)
{
Object button_command = e.getActionCommand();
if (button_command.equals("Go_Shadow"))
{
GoInvisible invisy = new GoInvisible();
invisy.mousePressed(me);
invisy.mouseReleased(me);
}
}
} // end component handler class
} // end of TextFrame class
这是鼠标侦听器类
public class GoInvisible implements MouseListener {
FNAFrame Parentpane = new FNAFrame();
TextFrame compPanel = new TextFrame();
@Override
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mousePressed(MouseEvent e) {
Parentpane.setUndecorated(true);
Parentpane.setOpacity(0.5f);
compPanel.setOpaque(true);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
Parentpane.setUndecorated(false);
compPanel.setOpaque(true);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
# 1 楼答案
有一系列的问题
throw new UnsupportedOperationException("Not supported yet.");
,这将导致问题并停止代码的执行actionCommand
不是"Go_Shadow"
,它将是按钮的文本,除非您另行指定李>MouseListener
或ActionListener
添加,而是应该监视ButtonModel
的状态GoInvisible
处理程序中创建FNAFrame
和TextFrame
的新实例,它们与屏幕上实际显示的实例没有关系李>相反,您应该监视
ButtonModel
的状态,例如但是,您仍然会发现这会导致问题,因为帧边框的状态在显示后无法更改
例如,更好的解决方案可能是使用
JToggleButton
您可能希望有一个通读Code Conventions for the Java TM Programming Language,它将使人们更容易阅读您的代码,您也更容易阅读其他代码