Java 从菜单到JPanel的图像/面板/按钮有困难
我希望能够动态添加新的60x80图像,这些图像将用作打开新帧的按钮。然而,由于某种奇怪的原因,我的createFrame()方法无法将任何类型的组件添加到jpanel中。我已经试着解决这个问题好几个小时了,我不确定到底出了什么问题。我尝试过添加普通面板、标签、按钮。。。但一切都不起作用。我的主JPanel将对所有图像使用FlowLayout,我的主JFrame使用BorderLayout,这样我以后可以在主JPanel下定位另一个特定内容 这是我的代码(包括我编辑时的revalidate(),它修复了它): 知道我代码中的错误是什么吗Java 从菜单到JPanel的图像/面板/按钮有困难,java,swing,jframe,jpanel,layout-manager,Java,Swing,Jframe,Jpanel,Layout Manager,我希望能够动态添加新的60x80图像,这些图像将用作打开新帧的按钮。然而,由于某种奇怪的原因,我的createFrame()方法无法将任何类型的组件添加到jpanel中。我已经试着解决这个问题好几个小时了,我不确定到底出了什么问题。我尝试过添加普通面板、标签、按钮。。。但一切都不起作用。我的主JPanel将对所有图像使用FlowLayout,我的主JFrame使用BorderLayout,这样我以后可以在主JPanel下定位另一个特定内容 这是我的代码(包括我编辑时的revalidate(),它
编辑:我可以使用main\u frame.revalidate()修复它。。。这是最合适的方式吗?使用validate()似乎完成了同样的事情,但不幸的是,即使在阅读了javadoc之后,我也不理解其中的区别。也许您可以澄清一下,但您已经声明
类TestIt扩展了JFrame
,因此您不需要主框架=新的JFrame(“测试程序”)代码>在构造函数中
它可以被这样的东西所取代:
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
?用Image和ActionListener创建动态JButton-Java Swing
使用Image和ActionListener动态创建JButton。您可以在一个位置更改按钮高度、宽度、水平间隙和垂直间隙
我创建了一个虚拟数据库类,它将返回主菜单项和子菜单项。您将在JFrame中看到主菜单项。如果从按钮面板中选择主项(食物),它将从虚拟数据库类(食物的子项)加载子项
private void addMainMenue(){
pnl_按钮。移除所有();
重新油漆();
图像img,sub;
图像图标;
字符串imagePath,imag=“/com/images/”;
ArrayList菜单=新建ArrayList();
ArrayList itemName=新的ArrayList();
对于(int size=0;size=bLength){
中断L1;
}
}
hGap+=按钮高度+水平间隙;
}
包装();
}
私有void addSubmenue(字符串选择){
pnl_按钮。移除所有();
重新油漆();
图像img,sub;
图像图标;
字符串imagePath,imag=“/com/images/”;
ArrayList菜单=新建ArrayList();
ArrayList itemName=新的ArrayList();
ArrayList=ItemDB.getSubMenu(选项);
字符串子代码[]=(字符串[])列表。获取(0);
String subDesc[]=(String[])list.get(1);
对于(int size=0;sizepublic 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();
}
private void addMainMenue() {
pnl_button.removeAll();
repaint();
Image img, sub;
ImageIcon icon;
String imagePath, imag = "/com/images/";
ArrayList menue = new ArrayList();
ArrayList itemName = new ArrayList();
for (int size = 0; size < ItemDB.mainMenuCodes.length; size++) {
menue.add(ItemDB.mainMenuCodes[size]);
itemName.add(ItemDB.mainMenuDesc[size]);
}
JButton[] buttons = new JButton[menue.size()];
for (int i = 0; i < buttons.length; i++) {
imagePath = imag + menue.get(i).toString() + ".jpeg";
URL url = getClass().getResource(imagePath);
// System.out.println(imagePath +" Get Res : "
// +getClass().getResource(imagePath));
if (url != null) {
img = Toolkit.getDefaultToolkit().getImage(url);
sub = img.getScaledInstance(button_width - 8,
button_height - 30, Image.SCALE_FAST);
icon = new ImageIcon(sub);
} else
icon = new ImageIcon();
buttons[i] = new JButton(itemName.get(i).toString(), icon);
buttons[i].setVerticalTextPosition(AbstractButton.BOTTOM);
buttons[i].setHorizontalTextPosition(AbstractButton.CENTER);
buttons[i]
.setBorder(javax.swing.BorderFactory.createEtchedBorder());
buttons[i].setFont(new java.awt.Font("Tahoma", 1, 13));
buttons[i].setForeground(new java.awt.Color(0, 51, 255));
buttons[i].setActionCommand(menue.get(i).toString());
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String choice = e.getActionCommand();
addSubmenue(choice);
}
});
}
int b = 0;
int vGap = verticalGap;
int hGap = horizontalGap;
int bLength = buttons.length;
int bRows = bLength / numberOfColumns + 1;
L1: for (int j = 0; j < bRows; j++) {
vGap = 10;
for (int k = 0; k < numberOfColumns; k++) {
pnl_button.add(buttons[b],
new org.netbeans.lib.awtextra.AbsoluteConstraints(vGap,
hGap, button_width, button_height));
repaint();
vGap += button_width + verticalGap;
b++;
if (b >= bLength) {
break L1;
}
}
hGap += button_height + horizontalGap;
}
pack();
}
private void addSubmenue(String choice) {
pnl_button.removeAll();
repaint();
Image img, sub;
ImageIcon icon;
String imagePath, imag = "/com/images/";
ArrayList menue = new ArrayList();
ArrayList itemName = new ArrayList();
ArrayList list = ItemDB.getSubMenu(choice);
String subCode[] = (String[]) list.get(0);
String subDesc[] = (String[]) list.get(1);
for (int size = 0; size < subCode.length; size++) {
menue.add(subCode[size]);
itemName.add(subDesc[size]);
}
JButton[] buttons = new JButton[menue.size()];
for (int i = 0; i < buttons.length; i++) {
imagePath = imag + menue.get(i).toString() + ".jpeg";
URL url = getClass().getResource(imagePath);
// System.out.println(imagePath +" Get Reso : "
// +getClass().getResource(imagePath));
if (url != null) {
img = Toolkit.getDefaultToolkit().getImage(url);
sub = img.getScaledInstance(button_width - 8,
button_height - 30, Image.SCALE_FAST);
icon = new ImageIcon(sub);
} else
icon = new ImageIcon();
buttons[i] = new JButton(itemName.get(i).toString(), icon);
buttons[i].setVerticalTextPosition(AbstractButton.BOTTOM);
buttons[i].setHorizontalTextPosition(AbstractButton.CENTER);
buttons[i]
.setBorder(javax.swing.BorderFactory.createEtchedBorder());
buttons[i].setFont(new java.awt.Font("Tahoma", 1, 13));
buttons[i].setForeground(new java.awt.Color(0, 51, 255));
buttons[i].setActionCommand(menue.get(i).toString());
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String choice = e.getActionCommand();
addItems(choice);
}
});
}
int b = 0;
int vGap = verticalGap;
int hGap = horizontalGap;
int bLength = buttons.length;
int bRows = bLength / numberOfColumns + 1;
L1: for (int j = 0; j < bRows; j++) {
vGap = 10;
for (int k = 0; k < numberOfColumns; k++) {
pnl_button.add(buttons[b],
new org.netbeans.lib.awtextra.AbsoluteConstraints(vGap,
hGap, button_width, button_height));
repaint();
vGap += button_width + verticalGap;
b++;
if (b >= bLength) {
break L1;
}
}
hGap += button_height + horizontalGap;
}
pack();
}
private void addItems(String choice) {
if (choice.equals("P"))
choice = "PIZZA";
else if (choice.equals("B"))
choice = "BURGER";
else if (choice.equals("FJ"))
choice = "FRUIT JUICE";
else if (choice.equals("HB"))
choice = "HOT BEVERAGES";
JOptionPane.showMessageDialog(this, "You have select " + choice);
}