0
我有一個擴展JFrame的類,它有一個菜單欄和菜單項。在菜單欄下,我想添加一個JPanel,在其中添加組件並繪製形狀。如何在此類中添加JPanel?對不起,如果這是一個簡單的問題,我是一個初學者。如何在Jframe中創建一個JPanel?
import java.awt.FlowLayout;
import javax.swing.*;
public class theMenu extends JFrame {
static JMenuBar menubar;
JMenu shape, color;
JCheckBox fill;
JButton btn1,btn2;
JMenuItem circle, rectangle, line,triangle;
JMenuItem red, green, blue, yellow;
public theMenu(){
super("Using JMenus");
menubar=new JMenuBar();
shape=new JMenu ("Shape");
add(menubar);
setJMenuBar(menubar); // add menu bar to application
shape=new JMenu ("Shape");
color=new JMenu ("Color");
fill=new JCheckBox("fill");
btn1=new JButton("save");
btn2=new JButton("import");
circle=new JMenuItem ("Circle");
rectangle=new JMenuItem ("Rectangle");
line=new JMenuItem ("Line");
triangle = new JMenuItem ("Triangle");
red=new JMenuItem ("Red");
green=new JMenuItem ("Green");
blue=new JMenuItem ("Blue");
yellow=new JMenuItem ("Yellow");
shape.add (circle);
shape.add (rectangle);
shape.add (line);
shape.add (triangle);
color.add (red);
color.add (green);
color.add (blue);
color.add (yellow);
menubar.add (shape);
menubar.add(color);
menubar.add(fill);
menubar.add(btn1);
menubar.add(btn2);
}
}
原理與你目前一樣...... –
快速的方法是在netbeans或eclipse中使用gui builder –
除此之外:閱讀關於java命名約定。類名稱爲CamelCase。總是。 – GhostCat