Java 静态JFrame ActionListener
在这段代码中,我为什么要Java 静态JFrame ActionListener,java,swing,static,Java,Swing,Static,在这段代码中,我为什么要 private STATIC int??? 哪种方法是静态的 如果我没有静态编译它,它会在MyAppEv app=newmyappev(“皇家马德里”+g+:“+h+”巴塞罗那”)中出错 有人能给我解释一下JFrame和Actionlistener吗?在上面的代码中使用static的唯一原因是因为您从static上下文中设置了标题 相反,您不能在构造函数中获取应用程序标题的值,而只是创建一个更新框架标题的updateTitle方法 import java.awt.*;
private STATIC int???
哪种方法是静态的
如果我没有静态编译它,它会在MyAppEv app=newmyappev(“皇家马德里”+g+:“+h+”巴塞罗那”)中出错代码>
有人能给我解释一下JFrame和Actionlistener吗?在上面的代码中使用static
的唯一原因是因为您从static
上下文中设置了标题
相反,您不能在构造函数中获取应用程序标题的值,而只是创建一个更新框架标题的updateTitle
方法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyAppEv extends JFrame implements ActionListener {
private static int h = 0, g = 0;
JButton b1 = new JButton("Real");
JButton b2 = new JButton("Barcelona");
public MyAppEv(String title) {
super(title);
b1.addActionListener(this);
b2.addActionListener(this);
JPanel myPanel = new JPanel();
myPanel.add(b1);
myPanel.add(b2);
setContentPane(myPanel);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == b1) {
++g;
setTitle("Real Madrid " + g + ":" + h + " Barcelona");
} else if (source == b2) {
++h;
setTitle("Real Madrid " + g + ":" + h + " Barcelona");
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Can't set look and feel: " + e);
}
MyAppEv app = new MyAppEv("Real Madrid " + g + ":" + h + " Barcelona");
WindowListener L = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
app.addWindowListener(L);
app.setBounds(300, 200, 350, 250);
app.setVisible(true);
}
}
事实上,对于您试图实现的目标,我将避免使用静态,因为如果每个JVM有多个帧的一个实例,这将产生影响
如果您想提供一个标题并对其进行更新,您可以覆盖setTitle
方法并附加其他内容,例如
import java.awt.event.*;
import javax.swing.*;
public class MyAppEv extends JFrame implements ActionListener {
private int h = 0, g = 0;
JButton b1 = new JButton("Real");
JButton b2 = new JButton("Barcelona");
public MyAppEv() {
updateTitle();
b1.addActionListener(this);
b2.addActionListener(this);
JPanel myPanel = new JPanel();
myPanel.add(b1);
myPanel.add(b2);
setContentPane(myPanel);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == b1) {
++g;
updateTitle();
} else if (source == b2) {
++h;
updateTitle();
}
}
protected void updateTitle() {
setTitle("Real Madrid " + g + ":" + h + " Barcelona");
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MyAppEv app = new MyAppEv();
WindowListener L = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
app.addWindowListener(L);
app.setBounds(300, 200, 350, 250);
app.setVisible(true);
}
});
}
}
你不能不知道如何拼写你要问的Java关键字。在问新问题之前,先接受前面问题的答案。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyAppEv extends JFrame implements ActionListener {
private int h = 0, g = 0;
private String realTitle;
JButton b1 = new JButton("Real");
JButton b2 = new JButton("Barcelona");
public MyAppEv(String title) {
// super(title) won't call setTitle and the
// the method used is to initialise the title
// is private...awesome...
setTitle(title);
b1.addActionListener(this);
b2.addActionListener(this);
JPanel myPanel = new JPanel();
myPanel.add(b1);
myPanel.add(b2);
setContentPane(myPanel);
}
@Override
public void setTitle(String title) {
System.out.println("Real title = " + title);
realTitle = title;
if (title == null) {
title = "";
}
super.setTitle(title + ": Real Madrid " + g + ":" + h + " Barcelona");
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == b1) {
++g;
updateTitle();
} else if (source == b2) {
++h;
updateTitle();
}
}
protected void updateTitle() {
setTitle(realTitle);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MyAppEv app = new MyAppEv("This is an example title");
WindowListener L = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
app.addWindowListener(L);
app.setBounds(300, 200, 350, 250);
app.setVisible(true);
}
});
}
}