这是我的代码,但框架显示一个空窗口,没有错误(语法或其他)。我认为这是JPanel或GridLayout中的一些错误。当我将鼠标放在p1.setLayout(new GridLayout(2,2));
上的GridLayout上时,它会显示"Note:This element has no attached source and the Javadoc could not be found in the attached javadoc"
。
这是测试
import javax.swing.JFrame;
public class test {
public static void main(String[] args) {
menu frame = new menu();
frame.setTitle("menu");
frame.setSize(400,400);
frame.setLocationRelativeTo(null);// Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我的代码
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class menu extends JFrame{
JButton jbtAdd = new JButton("add");
JButton jbtPrint = new JButton("print");
ArrayList<Product> manu = new ArrayList<Product>();
public void menu(){
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,2));
p1.add(new JLabel("add new product"));
p1.add(jbtAdd);
p1.add(new JLabel("print menu"));
p1.add(jbtPrint);
JPanel p2=new JPanel(new BorderLayout());
p2.add(new JTextField("MENU"), BorderLayout.NORTH);
p2.add(p1,BorderLayout.CENTER );
add(p2, BorderLayout.SOUTH);
jbtAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPaneMultiInput input=new JOptionPaneMultiInput();
}
});
};
public class JOptionPaneMultiInput {
public void JOptionPaneMultiInput(){
JTextField productField = new JTextField(5);
JTextField priceField = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("enter product"));
myPanel.add(productField);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("enter price"));
myPanel.add(priceField);
int result= JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter product and price Values", JOptionPane.OK_CANCEL_OPTION);
String name1= productField.getText();
double price1=Double.parseDouble(priceField.getText());
if (result == JOptionPane.OK_OPTION) {
Product name = new Product(name1, price1);
manu.add(name);
}
}
}
private class Product{
private String name = "noname";
private Double price=new Double(100);
public Product(String name1, double price1) {
// TODO Auto-generated constructor stub
}
public void Product(){
};
public void Product(String name,double price)
{
this.name=name;
this.price=price;
};
public double getPrice(){
return price;}
public void setPrice(Double p){
price=p;}
public String getName(){
return name;}
public void setName(String n){
name=n;}
}
}
答案 0 :(得分:4)
您的构造函数中目前有一个额外的void
关键字,实际上是一个方法,因此永远不会调用它。取代
public void menu() {
带
public menu() {
同样适用于Product
,请替换
public void Product() {
带
public Product() {
除此之外: Java Code Conventions表示类名以大写字母开头,为您提供Menu
类。