[英]MouseListener doesn't work the first time
我是Java新手,正在創建一個簡單的GUI。 我在JFrame
中的Java中有一個標簽,當我單擊它時,該程序應該顯示另一個框架並隱藏當前框架。 我也將其打印出來,以檢查標簽(像按鈕一樣)是否工作。 第一次它沒有在所有的工作。 從第二次單擊開始,它將在下一次嘗試中起作用,但不會隱藏當前幀。
我的代碼是:
private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {
MainFrame mf = new MainFrame();
jLabel4.addMouseListener(new MouseAdapter (){
@Override
public void mousePressed(MouseEvent e){
System.out.println("It works.");
mf.setVisible(true);
NewJFrame2 n2 = new NewJFrame2();
n2.setVisible(false);
}
});
有人知道如何修復它才能從第一次單擊開始工作並隱藏當前框架嗎?
與其單擊JLabel
,不如創建一個JButton
,它已經使用ActionListener
處理了單擊,並使它看起來像JLabel
,如對該問題的多個答案所示。
但它不會隱藏當前的JFrame
好了,您需要在偵聽器上調用JFrame#dispose()
方法,但還請看看“使用多個JFrame:好做法還是壞做法?”。 ,最好使用卡片布局,或者看一下如何使用對話框的教程
使用n2.dispose()
代替n2.setVisible(false);
這對您來說是一個簡單的示例,但是正如其他人所說,在同一應用程序中使用多個JFrame
並不好。 而不是嘗試使用具有適當布局的JFrame
和JPanel
。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
JFrame MainFrame;
JFrame ChildFrame;
JLabel label;
public Main(){
MainFrame = new JFrame("Example");
MainFrame.setSize(300, 300);
label = new JLabel("Click me");
labelMousePressed();
MainFrame.add(label);
MainFrame.setVisible(true);
}
private void labelMousePressed() {
label.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
System.out.println("It works.");
MainFrame.dispose();
ChildFrame = new JFrame("Child");
ChildFrame.setSize(300, 300);
ChildFrame.setVisible(true);
}
});
}
public static void main(String[] args) {
Main m = new Main();
}
}
更新
如果不重寫JFrame
的方法,則無需extends
(繼承) JFrame
類。 而不是從JFrame
創建對象並使用它。 閱讀此問題以了解更多信息 。
Java標簽無法接收ActionListener事件,您應該使用按鈕替換標簽。 您不必單擊按鈕上的標簽,可能對標簽有用的可能是屬性更改偵聽器。
運行並分析此代碼,它將清楚地指導您……祝您好運,您選擇了世界上最好的語言,是個Java專家,兩個
class MainFrame extends JFrame {
JButton button2 = new JButton("Go to Frame 2");
public MainFrame() {
setSize(500, 500);
getContentPane().setBackground(Color.RED);
setLayout(new FlowLayout());
add(button2);
button2.addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*
* @param e
*/
@Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
new Sample2().setVisible(true);
}
});
}}
public class Sample2 extends JFrame {
JButton button4;
public Sample2() {
setSize(500, 600);
setLayout(new FlowLayout());
getContentPane().setBackground(Color.YELLOW);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame mf = new MainFrame();
button4 = new JButton("Button 4");
add(button4);
button4.addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*
* @param e
*/
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("It works.");
mf.setVisible(true);
setVisible(false);
}
});
}
public static void main(String[] args) {
Sample2 sample2 = new Sample2();
sample2.setVisible(true);
}}
Java標簽無法接收ActionListener事件,您應該使用按鈕替換標簽。 您不必單擊按鈕上的標簽,可能對標簽有用的可能是屬性更改偵聽器。
在此答案中,按鈕具有圖像,只記得創建一個文件夾unser src,名稱為res,然后添加按鈕要顯示的圖像。 您可以在以下位置用我的文件名替換圖像文件名
//new ImageIcon(getClass().getResource("/res/image-file_name"));**
package StackOverflowProblemSets;
import sun.applet.Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* Created by HACKER on 05/06/2017.
* https://stackoverflow.com/questions/44370545/mouselistener-doesnt-work-
the-first-time-and-there-are-other-errors
*/
class MainFrame extends JFrame {
JButton button2 = new JButton("Go to Frame 2", new
ImageIcon(getClass().getResource("/res/ic_action_maps_blue.png")));
public MainFrame() {
setSize(500, 500);
getContentPane().setBackground(Color.RED);
setLayout(new FlowLayout());
add(button2);
button2.addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*
* @param e
*/
@Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
new Sample2().setVisible(true);
}
});}}
public class Sample2 extends JFrame {
JButton button4;
public Sample2() {
setSize(500, 600);
setLayout(new FlowLayout());
getContentPane().setBackground(Color.YELLOW);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame mf = new MainFrame();
button4 = new JButton("Button 4", new
ImageIcon(getClass().getResource("/res/ic_action_alpha_icon_D.png")));
add(button4);
button4.addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*
* @param e
*/
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("It works.");
mf.setVisible(true);
setVisible(false);
}
});
}
public static void main(String[] args) {
Sample2 sample2 = new Sample2();
sample2.setVisible(true);
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.