我是 eclipse 和 java 的新手,我正在尝试使用按钮在两个面板之间切换。我想测试我的项目,但是当我尝试运行它时,它启动,没有给出错误,然后什么也没有发生。
我是否错过了之前需要做的事情才能运行?
这是我拥有的版本,我今天早些时候下载了它。
面向 Java 开发人员的 Eclipse IDE
版本:2019-09 R (4.13.0) 版本号:20190917-1200
下面是我的代码,如果有什么东西阻止它在那里工作。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Music_Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Music_Test window = new Music_Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Music_Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel Menu = new JPanel();
Menu.setBounds(6, 6, 438, 266);
frame.getContentPane().add(Menu);
Menu.setLayout(null);
Menu.setVisible(true);
JPanel Select_Level = new JPanel();
Select_Level.setBounds(6, 6, 438, 266);
frame.getContentPane().add(Select_Level);
Select_Level.setVisible(false);
JButton btnSelectLevel = new JButton("Select Level");
btnSelectLevel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Menu.setVisible(false);
Select_Level.setVisible(true);
}
});
btnSelectLevel.setBounds(158, 45, 117, 29);
Menu.add(btnSelectLevel);
JLabel lblMenu = new JLabel("Menu");
lblMenu.setFont(new Font("Comic Sans MS", Font.BOLD, 22));
lblMenu.setBounds(187, 17, 61, 29);
Menu.add(lblMenu);
JLabel lblSelectLevel = new JLabel("Select Level");
lblSelectLevel.setFont(new Font("Comic Sans MS", Font.BOLD, 22));
lblSelectLevel.setBounds(187, 17, 61, 29);
Select_Level.add(lblSelectLevel);
}
}
最佳答案
首先使用不同的布局管理器 FlowLayout
或GridBagLayout
可能效果更好
JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
这些布局将遵循您的按钮的首选尺寸
至于打开另一个窗口,嗯,您已经创建了一个窗口,因此过程几乎相同。话虽如此,您可能会考虑看看 The Use of Multiple JFrames: Good or Bad Practice?在你做出 promise 之前。
更好的方法可能是使用 JMenuBar
和JMenuItems
充当“打开”和“退出” Action 。看看How to Use Menus那么你可以使用 CardLayout
例如,在 View 之间切换
从纯粹的设计角度来看(我知道这只是练习,但熟能生巧),我不会从 JFrame
扩展任何内容。相反,将依赖于围绕 JPanel
之类的内容构建主 GUI相反。
这使您可以灵活地决定如何使用这些组件,因为您可以将它们添加到框架、小程序或其他组件中...
并且
尽管您已经实现了actionPerformed
方法按照ActionListener
接口(interface),您的类不属于该类型,因为您尚未实现该接口(interface)。一旦您实现该接口(interface)并将其注册到 JButton
btnAdd
,
btnAdd.addActionListener(this);
该方法将被调用。
更紧凑的替代方案可能是使用匿名接口(interface):
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// handle button ActionEvent & display dialog...
}
});
旁注:
Using more than one JFrame in an application creates a lot of overhead for managing updates that may need to exist between frames. The preferred approach is to use a modal JDialog if another window is required. This is discussed more Here
关于java - Eclipse项目无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072990/