public class Window extends JFrame implements ActionListener{
public static void main(String[] args) {
new Window();
}
public Window() {
ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
JButton exitButton = new JButton("Label");
JLabel text = new JLabel("Luminate Media");
JPanel panel = new JPanel();
setTitle("Luminate");
setIconImage(fileIcon.getImage());
setBounds(0, 688, 1366, 40);
setLayout(null);
setUndecorated(true);
setResizable(false);
setVisible(true);
setAlwaysOnTop(true);
getContentPane().setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
text.setBounds(0, 0, 0, 0);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setSize(0, 0);
text.setForeground(Color.black);
text.setHorizontalAlignment(0);
exitButton.setBorder(null);
exitButton.setBounds(1326, 0, 40, 40);
exitButton.setIcon(exitIcon);
exitButton.addActionListener(this);
panel.setLayout(null);
panel.setBackground(Color.DARK_GRAY);
panel.add(exitButton);
panel.add(text);
add(panel);
}
public static void load() {
//MY PROBLEM IS HERE
//HOW DO I CALL THE public Window(){
//AS THIS IS NOT THE MAIN CLASS
}
@Override
public void actionPerformed(ActionEvent e) {
// ALSO I NEED THE CODE THE END THE ENTIRE APPLICATION HERE
}
}
你可能会看到错误,我不确定它为什么会发生。所以我非常好奇如何将窗口调用到加载方法中,如果可能的话,有人可以告诉我结束整个应用程序的代码吗?
答案 0 :(得分:1)
您想启动该应用程序吗?
创建一个新类(或同一个类,无所谓)。添加main方法。然后使用SwingUtilities.invokeLater()启动您的应用程序。
见这里:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
关于关闭,单击标题栏上的X按钮将自动关闭它。当窗户关闭时,你想要做些什么吗?喜欢通知用户?
SSCCE:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class StartASwingApp extends JFrame{
JButton close = new JButton("Close");
public StartASwingApp(){
getContentPane().add(close,BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
close.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0); // closes the application
}
});
setVisible(true);
}
//------------------------------------------------------------------------------
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new StartASwingApp();
}
});
}
//------------------------------------------------------------------------------
}
听听安德鲁汤普森所说的话。不要扩展Swing顶级容器。我这样做只是为了与您的代码段保持一致。