对于我项目中每种不同类型的窗口,我创建了一个不同的类。例如,我的主窗口是 MainWindow 的一个实例。项目设置窗口是 ProjectSettingsWindow 的一个实例。我还创建了一个名为 CustomWindow 的类。我会把它命名为 Window,但那已经被采用了。嘎。这个类包含我所有的窗口共享的东西,比如初始化方法和 JPanel。它扩展了 JFrame,我所有的其他窗口类都扩展了 CustomWindow。
对不起,这太长了。但这是 SSCCE:(这是我在这里的第一个问题,请耐心等待)
主类:
package beat;
public class Main {
public static StartWindow start = new StartWindow();
public static void main(String[] args) {
start.init(300, 100, "choices, choices");
start.display();
}
public static void close() {
//does other things
System.exit(0);
}
}
开始窗口类:
package beat;
import javax.swing.*;
public class StartWindow extends CustomWindow {
public StartWindow() {
eventHandler = new StartWindowEvents(this);
}
JButton newButton = new JButton();
JButton loadButton = new JButton();
//initialize
public void initBranch() {
initButtons();
//other classes have a few groups to initialize, not just one
}
private void initButtons() {
newButton.setText("new project");
newButton.setSize(120,49);
newButton.setLocation(10,10);
newButton.addActionListener(eventHandler);
loadButton.setText("load project");
loadButton.setSize(120,49);
loadButton.setLocation(164,10);
loadButton.addActionListener(eventHandler);
content.add(newButton);
content.add(loadButton);
}
}
StartWindowEvents 类:
package beat;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class StartWindowEvents extends CustomWindowEvents {
public StartWindowEvents(CustomWindow w) {
super(w);
}
//if a button is pressed
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Main.start.newButton)
newButton();
else if (e.getSource() == Main.start.loadButton)
loadButton();
}
private void newButton(){
//do the newButton stuff
}
private void loadButton() {
//do the loadButton stuff
}
}
自定义窗口类:
package beat;
import javax.swing.*;
public class CustomWindow extends JFrame {
JPanel content = new JPanel(null);
CustomWindowEvents eventHandler;
public void display() {
//whatever you want to refresh, usually nothing
setVisible(true);
}
public void init(int width, int height, String title) {
pack();
setVisible(false);
setResizable(false);
setLocationRelativeTo(null); //center on screen, but it doesnt work
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setContentPane(content);
addWindowListener(eventHandler);
setSize(width, height);
setTitle(title);
initBranch();
}
public void initBranch() {
//whatever you want to do after the window is initialized, usually branch to groups of JComponents
}
}
自定义窗口事件类:
package beat;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class CustomWindowEvents extends WindowAdapter implements ActionListener {
CustomWindow source;
public CustomWindowEvents(CustomWindow w) {
source = w;
}
public void actionPerformed(ActionEvent e) {}
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(source,
"DONT DO IT",
"are you sure?",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION)
doClose();
}
public void doClose() {
//whatever you want to do after the window is confirmed closed, usually exit the program
Main.close();
}
}