JFrame:无法启动新的 window
JFrame: Can't launch new window
我正在努力学习 Java 和 JFrames。我将这个简单的应用放在一起,其中 Main class 启动 MainGui class。 MainGui 将包含一堆按钮,每个按钮执行不同的操作(现在它只有 canadaFlagButton)。使用我创建的按钮打开 MainGui 的 window 没问题。
但是,当我单击 canadaFlagButton 时,它应该会启动一个新的 window 但实际上什么也没有发生。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainGui extends JFrame implements ActionListener {
JButton canadaFlagButton;
MainGui() {
canadaFlagButton = new JButton("Canada Flag");
canadaFlagButton.setBounds(100, 100, 200, 50);
canadaFlagButton.setFocusable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 600);
this.setLayout(null);
this.add(canadaFlagButton);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==canadaFlagButton) {
CanadaFlag window1 = new CanadaFlag();
}
}
}
//Can I draw the Canadian flag?
这是加拿大国旗 class:
import javax.swing.*;
import java.awt.*;
public class CanadaFlag extends JFrame {
CanadaFlag() {
//creating label for maple leaf
JLabel leafLabel = new JLabel();
ImageIcon leaf = new ImageIcon("mapleleaf.png");
leafLabel.setIcon(leaf);
leafLabel.setVerticalAlignment(JLabel.CENTER);
leafLabel.setHorizontalAlignment(JLabel.CENTER);
JPanel redLeftPanel = new JPanel();
redLeftPanel.setBackground(Color.red);
redLeftPanel.setBounds(0, 0, 400, 1000);
JPanel redRightPanel = new JPanel();
redRightPanel.setBackground(Color.red);
redRightPanel.setBounds(1000, 0, 400, 1000);
JPanel whiteMiddlePanel = new JPanel();
whiteMiddlePanel.setBackground(Color.white);
whiteMiddlePanel.setBounds(400, 0, 600, 1000);
whiteMiddlePanel.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1400, 1000);
this.setVisible(true);
this.add(redLeftPanel);
this.add(redRightPanel);
this.add(whiteMiddlePanel);
whiteMiddlePanel.add(leafLabel);
}
}
一个应用程序应该只有一个主 JFrame。如果您需要其他 windows 那么您通常会使用 JDialog。
Swing 旨在与布局管理器一起使用。如果您正在尝试学习 Swing,请查看 Layout Managers 上的 Swing 教程以获取更多信息和工作示例。
it is supposed to launch a new window but instead, nothing happens
您永远不会将 ActionListener
添加到按钮。
canadaFlagButton.addActionListener( this );
本教程还有一个关于 How to Use Buttons
的部分,其中包含一个工作示例。我建议您保留对本教程的参考,以获取所有 Swing 基础知识的示例。
我正在努力学习 Java 和 JFrames。我将这个简单的应用放在一起,其中 Main class 启动 MainGui class。 MainGui 将包含一堆按钮,每个按钮执行不同的操作(现在它只有 canadaFlagButton)。使用我创建的按钮打开 MainGui 的 window 没问题。
但是,当我单击 canadaFlagButton 时,它应该会启动一个新的 window 但实际上什么也没有发生。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainGui extends JFrame implements ActionListener {
JButton canadaFlagButton;
MainGui() {
canadaFlagButton = new JButton("Canada Flag");
canadaFlagButton.setBounds(100, 100, 200, 50);
canadaFlagButton.setFocusable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 600);
this.setLayout(null);
this.add(canadaFlagButton);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==canadaFlagButton) {
CanadaFlag window1 = new CanadaFlag();
}
}
}
//Can I draw the Canadian flag?
这是加拿大国旗 class:
import javax.swing.*;
import java.awt.*;
public class CanadaFlag extends JFrame {
CanadaFlag() {
//creating label for maple leaf
JLabel leafLabel = new JLabel();
ImageIcon leaf = new ImageIcon("mapleleaf.png");
leafLabel.setIcon(leaf);
leafLabel.setVerticalAlignment(JLabel.CENTER);
leafLabel.setHorizontalAlignment(JLabel.CENTER);
JPanel redLeftPanel = new JPanel();
redLeftPanel.setBackground(Color.red);
redLeftPanel.setBounds(0, 0, 400, 1000);
JPanel redRightPanel = new JPanel();
redRightPanel.setBackground(Color.red);
redRightPanel.setBounds(1000, 0, 400, 1000);
JPanel whiteMiddlePanel = new JPanel();
whiteMiddlePanel.setBackground(Color.white);
whiteMiddlePanel.setBounds(400, 0, 600, 1000);
whiteMiddlePanel.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1400, 1000);
this.setVisible(true);
this.add(redLeftPanel);
this.add(redRightPanel);
this.add(whiteMiddlePanel);
whiteMiddlePanel.add(leafLabel);
}
}
一个应用程序应该只有一个主 JFrame。如果您需要其他 windows 那么您通常会使用 JDialog。
Swing 旨在与布局管理器一起使用。如果您正在尝试学习 Swing,请查看 Layout Managers 上的 Swing 教程以获取更多信息和工作示例。
it is supposed to launch a new window but instead, nothing happens
您永远不会将 ActionListener
添加到按钮。
canadaFlagButton.addActionListener( this );
本教程还有一个关于 How to Use Buttons
的部分,其中包含一个工作示例。我建议您保留对本教程的参考,以获取所有 Swing 基础知识的示例。