我希望能够动态添加新的 60x80 图像,这些图像将充当打开新框架的按钮。但是,由于某些奇怪的原因,我的 createFrame() 方法无法将任何类型的组件添加到 jpanel 中。我已经尝试解决这个问题几个小时了,但我不确定出了什么问题。我尝试过添加普通面板、标签、按钮...但没有任何效果。我的主 JPanel 将为所有图像使用 FlowLayout,而我的主 JFrame 使用 BorderLayout,这样我就可以稍后在主 JPanel 下放置另一个特定内容 JPanel。
这是我的代码(包括我编辑后的 revalidate(),它修复了它):
package testit;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;
class TestIt extends JFrame implements ActionListener {
//Main window frame and content panel
private JFrame main_frame;
private JPanel main_panel;
//Screen size variable
private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public TestIt() {
//Set up the main frame
main_frame = new JFrame("Test Program");
main_frame.setLayout(new BorderLayout());
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_frame.setIconImage(
new ImageIcon("src/testit/resources/img/app_icon.gif").getImage());
//Set up the inner content panel
main_panel = new JPanel();
main_panel.setLayout(new FlowLayout());
main_panel.setPreferredSize(
new Dimension((screen.width / 10) * 6,(screen.height / 10) * 6));
//Add the menu bar and the main panel to the main frame
main_frame.add(main_panel, BorderLayout.CENTER);
main_frame.setJMenuBar(createMenuBar());
//Display the main GUI
main_frame.pack();
main_frame.setLocationRelativeTo(null);
main_frame.setVisible(true);
}
//Create an instance of the program
private static void runIt() {
TestIt program = new TestIt();
}
private JMenuBar createMenuBar() {
//Create the top menu bar
JMenuBar top_menu_bar = new JMenuBar();
//Create the menu
JMenu main_menu = new JMenu ("Menu");
main_menu.setMnemonic(KeyEvent.VK_M);
top_menu_bar.add(main_menu);
//Create the menu items and add them to the menu
JMenuItem menu_item;
menu_item = new JMenuItem("Add New");
menu_item.setMnemonic(KeyEvent.VK_N);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
menu_item.setActionCommand("new");
menu_item.addActionListener(this);
main_menu.add(menu_item);
menu_item = new JMenuItem("Save");
menu_item.setMnemonic(KeyEvent.VK_S);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
menu_item.setActionCommand("save");
menu_item.addActionListener(this);
main_menu.add(menu_item);
menu_item = new JMenuItem("Exit");
menu_item.setMnemonic(KeyEvent.VK_X);
menu_item.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));
menu_item.setActionCommand("exit");
menu_item.addActionListener(this);
main_menu.add(menu_item);
//Return the assembled menu bar
return top_menu_bar;
}
public void actionPerformed(ActionEvent e) {
if("new".equals(e.getActionCommand())) {
createFrame();
} else if("save".equals(e.getActionCommand())) {
//save();
} else {
quit();
}
}
private void createFrame() {
/*
ImageIcon image = new ImageIcon("src/testit/resources/img/test.gif");
JLabel label = new JLabel("", image, JLabel.CENTER);
main_panel.add(label);
*/
JButton frame = new JButton("test");
frame.setBorder(new LineBorder(Color.BLACK));
frame.setPreferredSize(new Dimension(60, 80));
frame.setVisible(true);
main_panel.add(frame);
main_frame.revalidate();
}
private void quit() {
System.exit(0);
}
public static void main(String [] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runIt();
}
});
}
}
知道我的代码中的错误是什么吗?
编辑:我能够通过使用 main_frame.revalidate() 来修复它...这是最合适的方法吗?看起来使用 validate() 完成了同样的事情,但不幸的是,即使在阅读了 javadoc 后我也不明白其中的区别。
最佳答案
也许你可以澄清一下,但你已经声明 class TestIt extends JFrame
因此你不需要在构造函数中使用 main_frame = new JFrame("Test Program");
.
它可以被这样的东西取代:
public testIt() {
super("Test Program");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
// whatever code goes here such as...
// this.add(somePanel);
// this.add(new JButton("Click Me!"));
// etc..
this.setVisible(true);
this.pack();
}
除非该框架有明确的原因。另外,您的做法并没有错,只是以不同的方式进行操作,但我提出它的原因是因为如果您不利用它,为什么要子类 JFrame
呢?
关于java - 从菜单到 JPanel 的图像/面板/按钮有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500976/